2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
8 const Generator = require("../Generator");
9 const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
10 const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
11 const { compareModulesByIdentifier } = require("../util/comparators");
12 const memoize = require("../util/memoize");
13 const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError");
15 /** @typedef {import("webpack-sources").Source} Source */
16 /** @typedef {import("../Compiler")} Compiler */
17 /** @typedef {import("../Module")} Module */
18 /** @typedef {import("../ModuleTemplate")} ModuleTemplate */
19 /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
21 const getWebAssemblyGenerator = memoize(() =>
22 require("./WebAssemblyGenerator")
24 const getWebAssemblyJavascriptGenerator = memoize(() =>
25 require("./WebAssemblyJavascriptGenerator")
27 const getWebAssemblyParser = memoize(() => require("./WebAssemblyParser"));
29 class WebAssemblyModulesPlugin {
30 constructor(options) {
31 this.options = options;
36 * @param {Compiler} compiler the compiler instance
40 compiler.hooks.compilation.tap(
41 "WebAssemblyModulesPlugin",
42 (compilation, { normalModuleFactory }) => {
43 compilation.dependencyFactories.set(
44 WebAssemblyImportDependency,
48 compilation.dependencyFactories.set(
49 WebAssemblyExportImportedDependency,
53 normalModuleFactory.hooks.createParser
54 .for("webassembly/sync")
55 .tap("WebAssemblyModulesPlugin", () => {
56 const WebAssemblyParser = getWebAssemblyParser();
58 return new WebAssemblyParser();
61 normalModuleFactory.hooks.createGenerator
62 .for("webassembly/sync")
63 .tap("WebAssemblyModulesPlugin", () => {
64 const WebAssemblyJavascriptGenerator =
65 getWebAssemblyJavascriptGenerator();
66 const WebAssemblyGenerator = getWebAssemblyGenerator();
68 return Generator.byType({
69 javascript: new WebAssemblyJavascriptGenerator(),
70 webassembly: new WebAssemblyGenerator(this.options)
74 compilation.hooks.renderManifest.tap(
75 "WebAssemblyModulesPlugin",
76 (result, options) => {
77 const { chunkGraph } = compilation;
78 const { chunk, outputOptions, codeGenerationResults } = options;
80 for (const module of chunkGraph.getOrderedChunkModulesIterable(
82 compareModulesByIdentifier
84 if (module.type === "webassembly/sync") {
85 const filenameTemplate =
86 outputOptions.webassemblyModuleFilename;
90 codeGenerationResults.getSource(
98 runtime: chunk.runtime,
102 identifier: `webassemblyModule${chunkGraph.getModuleId(
105 hash: chunkGraph.getModuleHash(module, chunk.runtime)
114 compilation.hooks.afterChunks.tap("WebAssemblyModulesPlugin", () => {
115 const chunkGraph = compilation.chunkGraph;
116 const initialWasmModules = new Set();
117 for (const chunk of compilation.chunks) {
118 if (chunk.canBeInitial()) {
119 for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
120 if (module.type === "webassembly/sync") {
121 initialWasmModules.add(module);
126 for (const module of initialWasmModules) {
127 compilation.errors.push(
128 new WebAssemblyInInitialChunkError(
130 compilation.moduleGraph,
131 compilation.chunkGraph,
132 compilation.requestShortener
142 module.exports = WebAssemblyModulesPlugin;