1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
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");
|
---|
14 |
|
---|
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 */
|
---|
20 |
|
---|
21 | const getWebAssemblyGenerator = memoize(() =>
|
---|
22 | require("./WebAssemblyGenerator")
|
---|
23 | );
|
---|
24 | const getWebAssemblyJavascriptGenerator = memoize(() =>
|
---|
25 | require("./WebAssemblyJavascriptGenerator")
|
---|
26 | );
|
---|
27 | const getWebAssemblyParser = memoize(() => require("./WebAssemblyParser"));
|
---|
28 |
|
---|
29 | class WebAssemblyModulesPlugin {
|
---|
30 | constructor(options) {
|
---|
31 | this.options = options;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Apply the plugin
|
---|
36 | * @param {Compiler} compiler the compiler instance
|
---|
37 | * @returns {void}
|
---|
38 | */
|
---|
39 | apply(compiler) {
|
---|
40 | compiler.hooks.compilation.tap(
|
---|
41 | "WebAssemblyModulesPlugin",
|
---|
42 | (compilation, { normalModuleFactory }) => {
|
---|
43 | compilation.dependencyFactories.set(
|
---|
44 | WebAssemblyImportDependency,
|
---|
45 | normalModuleFactory
|
---|
46 | );
|
---|
47 |
|
---|
48 | compilation.dependencyFactories.set(
|
---|
49 | WebAssemblyExportImportedDependency,
|
---|
50 | normalModuleFactory
|
---|
51 | );
|
---|
52 |
|
---|
53 | normalModuleFactory.hooks.createParser
|
---|
54 | .for("webassembly/sync")
|
---|
55 | .tap("WebAssemblyModulesPlugin", () => {
|
---|
56 | const WebAssemblyParser = getWebAssemblyParser();
|
---|
57 |
|
---|
58 | return new WebAssemblyParser();
|
---|
59 | });
|
---|
60 |
|
---|
61 | normalModuleFactory.hooks.createGenerator
|
---|
62 | .for("webassembly/sync")
|
---|
63 | .tap("WebAssemblyModulesPlugin", () => {
|
---|
64 | const WebAssemblyJavascriptGenerator =
|
---|
65 | getWebAssemblyJavascriptGenerator();
|
---|
66 | const WebAssemblyGenerator = getWebAssemblyGenerator();
|
---|
67 |
|
---|
68 | return Generator.byType({
|
---|
69 | javascript: new WebAssemblyJavascriptGenerator(),
|
---|
70 | webassembly: new WebAssemblyGenerator(this.options)
|
---|
71 | });
|
---|
72 | });
|
---|
73 |
|
---|
74 | compilation.hooks.renderManifest.tap(
|
---|
75 | "WebAssemblyModulesPlugin",
|
---|
76 | (result, options) => {
|
---|
77 | const { chunkGraph } = compilation;
|
---|
78 | const { chunk, outputOptions, codeGenerationResults } = options;
|
---|
79 |
|
---|
80 | for (const module of chunkGraph.getOrderedChunkModulesIterable(
|
---|
81 | chunk,
|
---|
82 | compareModulesByIdentifier
|
---|
83 | )) {
|
---|
84 | if (module.type === "webassembly/sync") {
|
---|
85 | const filenameTemplate =
|
---|
86 | outputOptions.webassemblyModuleFilename;
|
---|
87 |
|
---|
88 | result.push({
|
---|
89 | render: () =>
|
---|
90 | codeGenerationResults.getSource(
|
---|
91 | module,
|
---|
92 | chunk.runtime,
|
---|
93 | "webassembly"
|
---|
94 | ),
|
---|
95 | filenameTemplate,
|
---|
96 | pathOptions: {
|
---|
97 | module,
|
---|
98 | runtime: chunk.runtime,
|
---|
99 | chunkGraph
|
---|
100 | },
|
---|
101 | auxiliary: true,
|
---|
102 | identifier: `webassemblyModule${chunkGraph.getModuleId(
|
---|
103 | module
|
---|
104 | )}`,
|
---|
105 | hash: chunkGraph.getModuleHash(module, chunk.runtime)
|
---|
106 | });
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | return result;
|
---|
111 | }
|
---|
112 | );
|
---|
113 |
|
---|
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);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | for (const module of initialWasmModules) {
|
---|
127 | compilation.errors.push(
|
---|
128 | new WebAssemblyInInitialChunkError(
|
---|
129 | module,
|
---|
130 | compilation.moduleGraph,
|
---|
131 | compilation.chunkGraph,
|
---|
132 | compilation.requestShortener
|
---|
133 | )
|
---|
134 | );
|
---|
135 | }
|
---|
136 | });
|
---|
137 | }
|
---|
138 | );
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | module.exports = WebAssemblyModulesPlugin;
|
---|