mirror of https://github.com/webpack/webpack.git
50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
/*
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
Author Tobias Koppers @sokra
|
|
*/
|
|
"use strict";
|
|
|
|
const { traverse } = require("@webassemblyjs/ast");
|
|
const { decode } = require("@webassemblyjs/wasm-parser");
|
|
|
|
const { Tapable } = require("tapable");
|
|
const WebAssemblyImportDependency = require("./dependencies/WebAssemblyImportDependency");
|
|
|
|
const decoderOpts = {
|
|
ignoreCodeSection: true,
|
|
ignoreDataSection: true
|
|
};
|
|
|
|
class WebAssemblyParser extends Tapable {
|
|
constructor(options) {
|
|
super();
|
|
this.hooks = {};
|
|
this.options = options;
|
|
}
|
|
|
|
parse(binary, state) {
|
|
// flag it as ESM
|
|
state.module.buildMeta.exportsType = "namespace";
|
|
|
|
// parse it
|
|
const ast = decode(binary, decoderOpts);
|
|
|
|
// extract imports and exports
|
|
const exports = (state.module.buildMeta.providedExports = []);
|
|
traverse(ast, {
|
|
ModuleExport({ node }) {
|
|
exports.push(node.name);
|
|
},
|
|
|
|
ModuleImport({ node }) {
|
|
const dep = new WebAssemblyImportDependency(node.module, node.name);
|
|
state.module.addDependency(dep);
|
|
}
|
|
});
|
|
|
|
return state;
|
|
}
|
|
}
|
|
|
|
module.exports = WebAssemblyParser;
|