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 |
|
---|
11 | /** @typedef {import("../Chunk")} Chunk */
|
---|
12 | /** @typedef {import("../Compiler")} Compiler */
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * @typedef {object} CommonJsChunkLoadingPluginOptions
|
---|
16 | * @property {boolean} [asyncChunkLoading] enable async chunk loading
|
---|
17 | */
|
---|
18 |
|
---|
19 | class CommonJsChunkLoadingPlugin {
|
---|
20 | /**
|
---|
21 | * @param {CommonJsChunkLoadingPluginOptions} [options] options
|
---|
22 | */
|
---|
23 | constructor(options = {}) {
|
---|
24 | this._asyncChunkLoading = options.asyncChunkLoading;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Apply the plugin
|
---|
29 | * @param {Compiler} compiler the compiler instance
|
---|
30 | * @returns {void}
|
---|
31 | */
|
---|
32 | apply(compiler) {
|
---|
33 | const ChunkLoadingRuntimeModule = this._asyncChunkLoading
|
---|
34 | ? require("./ReadFileChunkLoadingRuntimeModule")
|
---|
35 | : require("./RequireChunkLoadingRuntimeModule");
|
---|
36 | const chunkLoadingValue = this._asyncChunkLoading
|
---|
37 | ? "async-node"
|
---|
38 | : "require";
|
---|
39 | new StartupChunkDependenciesPlugin({
|
---|
40 | chunkLoading: chunkLoadingValue,
|
---|
41 | asyncChunkLoading: this._asyncChunkLoading
|
---|
42 | }).apply(compiler);
|
---|
43 | compiler.hooks.thisCompilation.tap(
|
---|
44 | "CommonJsChunkLoadingPlugin",
|
---|
45 | compilation => {
|
---|
46 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
47 | /**
|
---|
48 | * @param {Chunk} chunk chunk
|
---|
49 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
---|
50 | */
|
---|
51 | const isEnabledForChunk = chunk => {
|
---|
52 | const options = chunk.getEntryOptions();
|
---|
53 | const chunkLoading =
|
---|
54 | options && options.chunkLoading !== undefined
|
---|
55 | ? options.chunkLoading
|
---|
56 | : globalChunkLoading;
|
---|
57 | return chunkLoading === chunkLoadingValue;
|
---|
58 | };
|
---|
59 | const onceForChunkSet = new WeakSet();
|
---|
60 | /**
|
---|
61 | * @param {Chunk} chunk chunk
|
---|
62 | * @param {Set<string>} set runtime requirements
|
---|
63 | */
|
---|
64 | const handler = (chunk, set) => {
|
---|
65 | if (onceForChunkSet.has(chunk)) return;
|
---|
66 | onceForChunkSet.add(chunk);
|
---|
67 | if (!isEnabledForChunk(chunk)) return;
|
---|
68 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
69 | set.add(RuntimeGlobals.hasOwnProperty);
|
---|
70 | compilation.addRuntimeModule(
|
---|
71 | chunk,
|
---|
72 | new ChunkLoadingRuntimeModule(set)
|
---|
73 | );
|
---|
74 | };
|
---|
75 |
|
---|
76 | compilation.hooks.runtimeRequirementInTree
|
---|
77 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
78 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
79 | compilation.hooks.runtimeRequirementInTree
|
---|
80 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
81 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
82 | compilation.hooks.runtimeRequirementInTree
|
---|
83 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
84 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
85 | compilation.hooks.runtimeRequirementInTree
|
---|
86 | .for(RuntimeGlobals.baseURI)
|
---|
87 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
88 | compilation.hooks.runtimeRequirementInTree
|
---|
89 | .for(RuntimeGlobals.externalInstallChunk)
|
---|
90 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
91 | compilation.hooks.runtimeRequirementInTree
|
---|
92 | .for(RuntimeGlobals.onChunksLoaded)
|
---|
93 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
94 |
|
---|
95 | compilation.hooks.runtimeRequirementInTree
|
---|
96 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
97 | .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
|
---|
98 | if (!isEnabledForChunk(chunk)) return;
|
---|
99 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
100 | });
|
---|
101 | compilation.hooks.runtimeRequirementInTree
|
---|
102 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
103 | .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
|
---|
104 | if (!isEnabledForChunk(chunk)) return;
|
---|
105 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
---|
106 | set.add(RuntimeGlobals.moduleCache);
|
---|
107 | set.add(RuntimeGlobals.hmrModuleData);
|
---|
108 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
109 | });
|
---|
110 | compilation.hooks.runtimeRequirementInTree
|
---|
111 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
112 | .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
|
---|
113 | if (!isEnabledForChunk(chunk)) return;
|
---|
114 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
---|
115 | });
|
---|
116 | }
|
---|
117 | );
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | module.exports = CommonJsChunkLoadingPlugin;
|
---|