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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
9 | const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule");
|
---|
10 |
|
---|
11 | /** @typedef {import("../Compiler")} Compiler */
|
---|
12 |
|
---|
13 | // TODO webpack 6 remove
|
---|
14 |
|
---|
15 | class FetchCompileWasmPlugin {
|
---|
16 | constructor(options) {
|
---|
17 | this.options = options || {};
|
---|
18 | }
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Apply the plugin
|
---|
22 | * @param {Compiler} compiler the compiler instance
|
---|
23 | * @returns {void}
|
---|
24 | */
|
---|
25 | apply(compiler) {
|
---|
26 | compiler.hooks.thisCompilation.tap(
|
---|
27 | "FetchCompileWasmPlugin",
|
---|
28 | compilation => {
|
---|
29 | const globalWasmLoading = compilation.outputOptions.wasmLoading;
|
---|
30 | const isEnabledForChunk = chunk => {
|
---|
31 | const options = chunk.getEntryOptions();
|
---|
32 | const wasmLoading =
|
---|
33 | options && options.wasmLoading !== undefined
|
---|
34 | ? options.wasmLoading
|
---|
35 | : globalWasmLoading;
|
---|
36 | return wasmLoading === "fetch";
|
---|
37 | };
|
---|
38 | const generateLoadBinaryCode = path =>
|
---|
39 | `fetch(${RuntimeGlobals.publicPath} + ${path})`;
|
---|
40 |
|
---|
41 | compilation.hooks.runtimeRequirementInTree
|
---|
42 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
43 | .tap("FetchCompileWasmPlugin", (chunk, set) => {
|
---|
44 | if (!isEnabledForChunk(chunk)) return;
|
---|
45 | const chunkGraph = compilation.chunkGraph;
|
---|
46 | if (
|
---|
47 | !chunkGraph.hasModuleInGraph(
|
---|
48 | chunk,
|
---|
49 | m => m.type === "webassembly/sync"
|
---|
50 | )
|
---|
51 | ) {
|
---|
52 | return;
|
---|
53 | }
|
---|
54 | set.add(RuntimeGlobals.moduleCache);
|
---|
55 | set.add(RuntimeGlobals.publicPath);
|
---|
56 | compilation.addRuntimeModule(
|
---|
57 | chunk,
|
---|
58 | new WasmChunkLoadingRuntimeModule({
|
---|
59 | generateLoadBinaryCode,
|
---|
60 | supportsStreaming: true,
|
---|
61 | mangleImports: this.options.mangleImports
|
---|
62 | })
|
---|
63 | );
|
---|
64 | });
|
---|
65 | }
|
---|
66 | );
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | module.exports = FetchCompileWasmPlugin;
|
---|