UNPKG

1.68 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");
10
11/** @typedef {import("../Compiler")} Compiler */
12
13class 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 AsyncWasmLoadingRuntimeModule({
52 generateLoadBinaryCode,
53 supportsStreaming: true
54 })
55 );
56 });
57 }
58 );
59 }
60}
61
62module.exports = FetchCompileAsyncWasmPlugin;