| 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 StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
|
|---|
| 10 | const ImportScriptsChunkLoadingRuntimeModule = require("./ImportScriptsChunkLoadingRuntimeModule");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 13 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 14 | /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
|
|---|
| 15 |
|
|---|
| 16 | const PLUGIN_NAME = "ImportScriptsChunkLoadingPlugin";
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Enables worker-side chunk loading via `importScripts` and wires in the
|
|---|
| 20 | * runtime helpers needed for startup, loading, and hot updates.
|
|---|
| 21 | */
|
|---|
| 22 | class ImportScriptsChunkLoadingPlugin {
|
|---|
| 23 | /**
|
|---|
| 24 | * Registers compilation hooks that attach the `importScripts` chunk-loading
|
|---|
| 25 | * runtime and its supporting globals to chunks using that backend.
|
|---|
| 26 | * @param {Compiler} compiler the compiler instance
|
|---|
| 27 | * @returns {void}
|
|---|
| 28 | */
|
|---|
| 29 | apply(compiler) {
|
|---|
| 30 | new StartupChunkDependenciesPlugin({
|
|---|
| 31 | chunkLoading: "import-scripts",
|
|---|
| 32 | asyncChunkLoading: true
|
|---|
| 33 | }).apply(compiler);
|
|---|
| 34 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 35 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
|---|
| 36 | /**
|
|---|
| 37 | * Determines whether the chunk resolves additional chunks through the
|
|---|
| 38 | * worker-side `importScripts` backend.
|
|---|
| 39 | * @param {Chunk} chunk chunk
|
|---|
| 40 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
|---|
| 41 | */
|
|---|
| 42 | const isEnabledForChunk = (chunk) => {
|
|---|
| 43 | const options = chunk.getEntryOptions();
|
|---|
| 44 | const chunkLoading =
|
|---|
| 45 | options && options.chunkLoading !== undefined
|
|---|
| 46 | ? options.chunkLoading
|
|---|
| 47 | : globalChunkLoading;
|
|---|
| 48 | return chunkLoading === "import-scripts";
|
|---|
| 49 | };
|
|---|
| 50 | /** @type {WeakSet<Chunk>} */
|
|---|
| 51 | const onceForChunkSet = new WeakSet();
|
|---|
| 52 | /**
|
|---|
| 53 | * Adds the `importScripts` chunk-loading runtime module to a chunk once
|
|---|
| 54 | * and records the globals it depends on.
|
|---|
| 55 | * @param {Chunk} chunk chunk
|
|---|
| 56 | * @param {RuntimeRequirements} set runtime requirements
|
|---|
| 57 | */
|
|---|
| 58 | const handler = (chunk, set) => {
|
|---|
| 59 | if (onceForChunkSet.has(chunk)) return;
|
|---|
| 60 | onceForChunkSet.add(chunk);
|
|---|
| 61 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 62 | const withCreateScriptUrl = Boolean(
|
|---|
| 63 | compilation.outputOptions.trustedTypes
|
|---|
| 64 | );
|
|---|
| 65 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 66 | set.add(RuntimeGlobals.hasOwnProperty);
|
|---|
| 67 | if (withCreateScriptUrl) {
|
|---|
| 68 | set.add(RuntimeGlobals.createScriptUrl);
|
|---|
| 69 | }
|
|---|
| 70 | compilation.addRuntimeModule(
|
|---|
| 71 | chunk,
|
|---|
| 72 | new ImportScriptsChunkLoadingRuntimeModule(set, withCreateScriptUrl)
|
|---|
| 73 | );
|
|---|
| 74 | };
|
|---|
| 75 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 76 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 77 | .tap(PLUGIN_NAME, handler);
|
|---|
| 78 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 79 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 80 | .tap(PLUGIN_NAME, handler);
|
|---|
| 81 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 82 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 83 | .tap(PLUGIN_NAME, handler);
|
|---|
| 84 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 85 | .for(RuntimeGlobals.baseURI)
|
|---|
| 86 | .tap(PLUGIN_NAME, handler);
|
|---|
| 87 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 88 | .for(RuntimeGlobals.onChunksLoaded)
|
|---|
| 89 | .tap(PLUGIN_NAME, handler);
|
|---|
| 90 |
|
|---|
| 91 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 92 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 93 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 94 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 95 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 96 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
|---|
| 97 | });
|
|---|
| 98 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 99 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 100 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 101 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 102 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 103 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
|---|
| 104 | set.add(RuntimeGlobals.moduleCache);
|
|---|
| 105 | set.add(RuntimeGlobals.hmrModuleData);
|
|---|
| 106 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 107 | });
|
|---|
| 108 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 109 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 110 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 111 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 112 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 113 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
|---|
| 114 | });
|
|---|
| 115 | });
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | module.exports = ImportScriptsChunkLoadingPlugin;
|
|---|