1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | */
|
---|
4 |
|
---|
5 | "use strict";
|
---|
6 |
|
---|
7 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
8 | const StartupChunkDependenciesRuntimeModule = require("./StartupChunkDependenciesRuntimeModule");
|
---|
9 | const StartupEntrypointRuntimeModule = require("./StartupEntrypointRuntimeModule");
|
---|
10 |
|
---|
11 | /** @typedef {import("../Compiler")} Compiler */
|
---|
12 |
|
---|
13 | class StartupChunkDependenciesPlugin {
|
---|
14 | constructor(options) {
|
---|
15 | this.chunkLoading = options.chunkLoading;
|
---|
16 | this.asyncChunkLoading =
|
---|
17 | typeof options.asyncChunkLoading === "boolean"
|
---|
18 | ? options.asyncChunkLoading
|
---|
19 | : true;
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Apply the plugin
|
---|
24 | * @param {Compiler} compiler the compiler instance
|
---|
25 | * @returns {void}
|
---|
26 | */
|
---|
27 | apply(compiler) {
|
---|
28 | compiler.hooks.thisCompilation.tap(
|
---|
29 | "StartupChunkDependenciesPlugin",
|
---|
30 | compilation => {
|
---|
31 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
32 | const isEnabledForChunk = chunk => {
|
---|
33 | const options = chunk.getEntryOptions();
|
---|
34 | const chunkLoading =
|
---|
35 | options && options.chunkLoading !== undefined
|
---|
36 | ? options.chunkLoading
|
---|
37 | : globalChunkLoading;
|
---|
38 | return chunkLoading === this.chunkLoading;
|
---|
39 | };
|
---|
40 | compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
---|
41 | "StartupChunkDependenciesPlugin",
|
---|
42 | (chunk, set, { chunkGraph }) => {
|
---|
43 | if (!isEnabledForChunk(chunk)) return;
|
---|
44 | if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
|
---|
45 | set.add(RuntimeGlobals.startup);
|
---|
46 | set.add(RuntimeGlobals.ensureChunk);
|
---|
47 | set.add(RuntimeGlobals.ensureChunkIncludeEntries);
|
---|
48 | compilation.addRuntimeModule(
|
---|
49 | chunk,
|
---|
50 | new StartupChunkDependenciesRuntimeModule(
|
---|
51 | this.asyncChunkLoading
|
---|
52 | )
|
---|
53 | );
|
---|
54 | }
|
---|
55 | }
|
---|
56 | );
|
---|
57 | compilation.hooks.runtimeRequirementInTree
|
---|
58 | .for(RuntimeGlobals.startupEntrypoint)
|
---|
59 | .tap("StartupChunkDependenciesPlugin", (chunk, set) => {
|
---|
60 | if (!isEnabledForChunk(chunk)) return;
|
---|
61 | set.add(RuntimeGlobals.require);
|
---|
62 | set.add(RuntimeGlobals.ensureChunk);
|
---|
63 | set.add(RuntimeGlobals.ensureChunkIncludeEntries);
|
---|
64 | compilation.addRuntimeModule(
|
---|
65 | chunk,
|
---|
66 | new StartupEntrypointRuntimeModule(this.asyncChunkLoading)
|
---|
67 | );
|
---|
68 | });
|
---|
69 | }
|
---|
70 | );
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | module.exports = StartupChunkDependenciesPlugin;
|
---|