[6a3a178] | 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 CreateScriptUrlRuntimeModule = require("../runtime/CreateScriptUrlRuntimeModule");
|
---|
| 10 | const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
|
---|
| 11 | const ImportScriptsChunkLoadingRuntimeModule = require("./ImportScriptsChunkLoadingRuntimeModule");
|
---|
| 12 |
|
---|
| 13 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 14 |
|
---|
| 15 | class ImportScriptsChunkLoadingPlugin {
|
---|
| 16 | /**
|
---|
| 17 | * Apply the plugin
|
---|
| 18 | * @param {Compiler} compiler the compiler instance
|
---|
| 19 | * @returns {void}
|
---|
| 20 | */
|
---|
| 21 | apply(compiler) {
|
---|
| 22 | new StartupChunkDependenciesPlugin({
|
---|
| 23 | chunkLoading: "import-scripts",
|
---|
| 24 | asyncChunkLoading: true
|
---|
| 25 | }).apply(compiler);
|
---|
| 26 | compiler.hooks.thisCompilation.tap(
|
---|
| 27 | "ImportScriptsChunkLoadingPlugin",
|
---|
| 28 | compilation => {
|
---|
| 29 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
| 30 | const isEnabledForChunk = chunk => {
|
---|
| 31 | const options = chunk.getEntryOptions();
|
---|
| 32 | const chunkLoading =
|
---|
| 33 | options && options.chunkLoading !== undefined
|
---|
| 34 | ? options.chunkLoading
|
---|
| 35 | : globalChunkLoading;
|
---|
| 36 | return chunkLoading === "import-scripts";
|
---|
| 37 | };
|
---|
| 38 | const onceForChunkSet = new WeakSet();
|
---|
| 39 | const handler = (chunk, set) => {
|
---|
| 40 | if (onceForChunkSet.has(chunk)) return;
|
---|
| 41 | onceForChunkSet.add(chunk);
|
---|
| 42 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 43 | const withCreateScriptUrl = !!compilation.outputOptions.trustedTypes;
|
---|
| 44 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
| 45 | set.add(RuntimeGlobals.hasOwnProperty);
|
---|
| 46 | if (withCreateScriptUrl) set.add(RuntimeGlobals.createScriptUrl);
|
---|
| 47 | compilation.addRuntimeModule(
|
---|
| 48 | chunk,
|
---|
| 49 | new ImportScriptsChunkLoadingRuntimeModule(set, withCreateScriptUrl)
|
---|
| 50 | );
|
---|
| 51 | };
|
---|
| 52 | compilation.hooks.runtimeRequirementInTree
|
---|
| 53 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
| 54 | .tap("ImportScriptsChunkLoadingPlugin", handler);
|
---|
| 55 | compilation.hooks.runtimeRequirementInTree
|
---|
| 56 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
| 57 | .tap("ImportScriptsChunkLoadingPlugin", handler);
|
---|
| 58 | compilation.hooks.runtimeRequirementInTree
|
---|
| 59 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
| 60 | .tap("ImportScriptsChunkLoadingPlugin", handler);
|
---|
| 61 | compilation.hooks.runtimeRequirementInTree
|
---|
| 62 | .for(RuntimeGlobals.baseURI)
|
---|
| 63 | .tap("ImportScriptsChunkLoadingPlugin", handler);
|
---|
| 64 | compilation.hooks.runtimeRequirementInTree
|
---|
| 65 | .for(RuntimeGlobals.createScriptUrl)
|
---|
| 66 | .tap("RuntimePlugin", (chunk, set) => {
|
---|
| 67 | compilation.addRuntimeModule(
|
---|
| 68 | chunk,
|
---|
| 69 | new CreateScriptUrlRuntimeModule()
|
---|
| 70 | );
|
---|
| 71 | return true;
|
---|
| 72 | });
|
---|
| 73 |
|
---|
| 74 | compilation.hooks.runtimeRequirementInTree
|
---|
| 75 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
| 76 | .tap("ImportScriptsChunkLoadingPlugin", (chunk, set) => {
|
---|
| 77 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 78 | set.add(RuntimeGlobals.publicPath);
|
---|
| 79 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
| 80 | });
|
---|
| 81 | compilation.hooks.runtimeRequirementInTree
|
---|
| 82 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
| 83 | .tap("ImportScriptsChunkLoadingPlugin", (chunk, set) => {
|
---|
| 84 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 85 | set.add(RuntimeGlobals.publicPath);
|
---|
| 86 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
---|
| 87 | set.add(RuntimeGlobals.moduleCache);
|
---|
| 88 | set.add(RuntimeGlobals.hmrModuleData);
|
---|
| 89 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
| 90 | });
|
---|
| 91 | compilation.hooks.runtimeRequirementInTree
|
---|
| 92 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
| 93 | .tap("ImportScriptsChunkLoadingPlugin", (chunk, set) => {
|
---|
| 94 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 95 | set.add(RuntimeGlobals.publicPath);
|
---|
| 96 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
---|
| 97 | });
|
---|
| 98 | }
|
---|
| 99 | );
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | module.exports = ImportScriptsChunkLoadingPlugin;
|
---|