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("../Compiler")} Compiler */
|
---|
13 |
|
---|
14 | class ModuleChunkLoadingPlugin {
|
---|
15 | /**
|
---|
16 | * Apply the plugin
|
---|
17 | * @param {Compiler} compiler the compiler instance
|
---|
18 | * @returns {void}
|
---|
19 | */
|
---|
20 | apply(compiler) {
|
---|
21 | compiler.hooks.thisCompilation.tap(
|
---|
22 | "ModuleChunkLoadingPlugin",
|
---|
23 | compilation => {
|
---|
24 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
25 | const isEnabledForChunk = chunk => {
|
---|
26 | const options = chunk.getEntryOptions();
|
---|
27 | const chunkLoading =
|
---|
28 | (options && options.chunkLoading) || globalChunkLoading;
|
---|
29 | return chunkLoading === "import";
|
---|
30 | };
|
---|
31 | const onceForChunkSet = new WeakSet();
|
---|
32 | const handler = (chunk, set) => {
|
---|
33 | if (onceForChunkSet.has(chunk)) return;
|
---|
34 | onceForChunkSet.add(chunk);
|
---|
35 | if (!isEnabledForChunk(chunk)) return;
|
---|
36 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
37 | set.add(RuntimeGlobals.hasOwnProperty);
|
---|
38 | compilation.addRuntimeModule(
|
---|
39 | chunk,
|
---|
40 | new ModuleChunkLoadingRuntimeModule(set)
|
---|
41 | );
|
---|
42 | };
|
---|
43 | compilation.hooks.runtimeRequirementInTree
|
---|
44 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
45 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
46 | compilation.hooks.runtimeRequirementInTree
|
---|
47 | .for(RuntimeGlobals.baseURI)
|
---|
48 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
49 | compilation.hooks.runtimeRequirementInTree
|
---|
50 | .for(RuntimeGlobals.externalInstallChunk)
|
---|
51 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
52 | compilation.hooks.runtimeRequirementInTree
|
---|
53 | .for(RuntimeGlobals.onChunksLoaded)
|
---|
54 | .tap("ModuleChunkLoadingPlugin", handler);
|
---|
55 | compilation.hooks.runtimeRequirementInTree
|
---|
56 | .for(RuntimeGlobals.externalInstallChunk)
|
---|
57 | .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
|
---|
58 | if (!isEnabledForChunk(chunk)) return;
|
---|
59 | compilation.addRuntimeModule(
|
---|
60 | chunk,
|
---|
61 | new ExportWebpackRequireRuntimeModule()
|
---|
62 | );
|
---|
63 | });
|
---|
64 |
|
---|
65 | compilation.hooks.runtimeRequirementInTree
|
---|
66 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
67 | .tap("ModuleChunkLoadingPlugin", (chunk, set) => {
|
---|
68 | if (!isEnabledForChunk(chunk)) return;
|
---|
69 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
70 | });
|
---|
71 | }
|
---|
72 | );
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | module.exports = ModuleChunkLoadingPlugin;
|
---|