webpack/lib/WebAssemblyParser.js

48 lines
1003 B
JavaScript
Raw Normal View History

2017-10-30 20:56:57 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
2018-02-21 21:22:43 +08:00
const Tools = require("webassembly-interpreter/lib/tools");
2017-10-30 20:56:57 +08:00
2017-11-28 23:54:26 +08:00
const Tapable = require("tapable").Tapable;
const WebAssemblyImportDependency = require("./dependencies/WebAssemblyImportDependency");
2017-10-30 20:56:57 +08:00
class WebAssemblyParser extends Tapable {
constructor(options) {
super();
2017-11-28 23:54:26 +08:00
this.hooks = {};
2017-10-30 20:56:57 +08:00
this.options = options;
}
parse(source, state) {
// flag it as ESM
state.module.buildMeta.exportsType = "namespace";
// parse it
const ast = Tools.parsers.parseWASM(source);
// extract imports and exports
const exports = state.module.buildMeta.providedExports = [];
Tools.traverse(ast, {
ModuleExport({
node
}) {
exports.push(node.name);
},
ModuleImport({
node
}) {
const dep = new WebAssemblyImportDependency(node.module, node.name);
state.module.addDependency(dep);
}
});
return state;
2017-10-30 20:56:57 +08:00
}
}
module.exports = WebAssemblyParser;