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