2019-05-24 18:30:43 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const AsyncWasmChunkLoadingRuntimeModule = require("../wasm-async/AsyncWasmChunkLoadingRuntimeModule");
|
|
|
|
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
|
|
|
class FetchCompileAsyncWasmPlugin {
|
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
2019-05-24 18:30:43 +08:00
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.thisCompilation.tap(
|
|
|
|
"FetchCompileAsyncWasmPlugin",
|
|
|
|
compilation => {
|
|
|
|
const generateLoadBinaryCode = path =>
|
|
|
|
`fetch(${RuntimeGlobals.publicPath} + ${path})`;
|
|
|
|
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.instantiateWasm)
|
|
|
|
.tap("FetchCompileAsyncWasmPlugin", (chunk, set) => {
|
|
|
|
const chunkGraph = compilation.chunkGraph;
|
|
|
|
if (
|
|
|
|
!chunkGraph.hasModuleInGraph(
|
|
|
|
chunk,
|
2019-07-15 21:03:29 +08:00
|
|
|
m => m.type === "webassembly/async"
|
2019-05-24 18:30:43 +08:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set.add(RuntimeGlobals.publicPath);
|
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
2019-08-27 02:21:07 +08:00
|
|
|
new AsyncWasmChunkLoadingRuntimeModule({
|
2019-05-24 18:30:43 +08:00
|
|
|
generateLoadBinaryCode,
|
|
|
|
supportsStreaming: true
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FetchCompileAsyncWasmPlugin;
|