UNPKG

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