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 ExportWebpackRequireRuntimeModule = require("./ExportWebpackRequireRuntimeModule");
|
---|
10 | const ModuleChunkLoadingRuntimeModule = require("./ModuleChunkLoadingRuntimeModule");
|
---|
11 |
|
---|
12 | /** @typedef {import("../Chunk")} Chunk */
|
---|
13 | /** @typedef {import("../Compiler")} Compiler */
|
---|
14 |
|
---|
15 | class ModuleChunkLoadingPlugin {
|
---|
16 | /**
|
---|
17 | * Apply the plugin
|
---|
18 | * @param {Compiler} compiler the compiler instance
|
---|
19 | * @returns {void}
|
---|
20 | */
|
---|
21 | apply(compiler) {
|
---|
22 | compiler.hooks.thisCompilation.tap(
|
---|
23 | "ModuleChunkLoadingPlugin",
|
---|
24 | compilation => {
|
---|
25 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
26 | /**
|
---|
27 | * @param {Chunk} chunk chunk to check
|
---|
28 | * @returns {boolean} true, when the plugin is enabled for the chunk
|
---|
29 | */
|
---|
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";
|
---|
37 | };
|
---|
38 | const onceForChunkSet = new WeakSet();
|
---|
39 | /**
|
---|
40 | * @param {Chunk} chunk chunk to check
|
---|
41 | * @param {Set<string>} set runtime requirements
|
---|
42 | */
|
---|
43 | const handler = (chunk, set) => {
|
---|
44 | if (onceForChunkSet.has(chunk)) return;
|
---|
45 | onceForChunkSet.add(chunk);
|
---|
46 | if (!isEnabledForChunk(chunk)) return;
|
---|
47 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
48 | set.add(RuntimeGlobals.hasOwnProperty);
|
---|
49 | compilation.addRuntimeModule(
|
---|
50 | chunk,
|
---|
51 | new ModuleChunkLoadingRuntimeModule(set)
|
---|
52 | );
|
---|
53 | };
|
---|
54 | compilation.hooks.runtimeRequirementInTree
|
---|
55 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
56 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
57 | compilation.hooks.runtimeRequirementInTree
|
---|
58 | .for(RuntimeGlobals.baseURI)
|
---|
59 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
60 | compilation.hooks.runtimeRequirementInTree
|
---|
61 | .for(RuntimeGlobals.externalInstallChunk)
|
---|
62 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
63 | compilation.hooks.runtimeRequirementInTree
|
---|
64 | .for(RuntimeGlobals.onChunksLoaded)
|
---|
65 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
66 | compilation.hooks.runtimeRequirementInTree
|
---|
67 | .for(RuntimeGlobals.externalInstallChunk)
|
---|
68 | .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
|
---|
69 | if (!isEnabledForChunk(chunk)) return;
|
---|
70 | compilation.addRuntimeModule(
|
---|
71 | chunk,
|
---|
72 | new ExportWebpackRequireRuntimeModule()
|
---|
73 | );
|
---|
74 | });
|
---|
75 |
|
---|
76 | compilation.hooks.runtimeRequirementInTree
|
---|
77 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
78 | .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
|
---|
79 | if (!isEnabledForChunk(chunk)) return;
|
---|
80 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
81 | });
|
---|
82 | }
|
---|
83 | );
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | module.exports = ModuleChunkLoadingPlugin;
|
---|