[79a0317] | 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("../../declarations/WebpackOptions").ChunkLoadingType} ChunkLoadingType */
|
---|
| 12 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 13 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * @typedef {object} Options
|
---|
| 17 | * @property {ChunkLoadingType} chunkLoading
|
---|
| 18 | * @property {boolean=} asyncChunkLoading
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | class StartupChunkDependenciesPlugin {
|
---|
| 22 | /**
|
---|
| 23 | * @param {Options} options options
|
---|
| 24 | */
|
---|
| 25 | constructor(options) {
|
---|
| 26 | this.chunkLoading = options.chunkLoading;
|
---|
| 27 | this.asyncChunkLoading =
|
---|
| 28 | typeof options.asyncChunkLoading === "boolean"
|
---|
| 29 | ? options.asyncChunkLoading
|
---|
| 30 | : true;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Apply the plugin
|
---|
| 35 | * @param {Compiler} compiler the compiler instance
|
---|
| 36 | * @returns {void}
|
---|
| 37 | */
|
---|
| 38 | apply(compiler) {
|
---|
| 39 | compiler.hooks.thisCompilation.tap(
|
---|
| 40 | "StartupChunkDependenciesPlugin",
|
---|
| 41 | compilation => {
|
---|
| 42 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
| 43 | /**
|
---|
| 44 | * @param {Chunk} chunk chunk to check
|
---|
| 45 | * @returns {boolean} true, when the plugin is enabled for the chunk
|
---|
| 46 | */
|
---|
| 47 | const isEnabledForChunk = chunk => {
|
---|
| 48 | const options = chunk.getEntryOptions();
|
---|
| 49 | const chunkLoading =
|
---|
| 50 | options && options.chunkLoading !== undefined
|
---|
| 51 | ? options.chunkLoading
|
---|
| 52 | : globalChunkLoading;
|
---|
| 53 | return chunkLoading === this.chunkLoading;
|
---|
| 54 | };
|
---|
| 55 | compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
---|
| 56 | "StartupChunkDependenciesPlugin",
|
---|
| 57 | (chunk, set, { chunkGraph }) => {
|
---|
| 58 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 59 | if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
|
---|
| 60 | set.add(RuntimeGlobals.startup);
|
---|
| 61 | set.add(RuntimeGlobals.ensureChunk);
|
---|
| 62 | set.add(RuntimeGlobals.ensureChunkIncludeEntries);
|
---|
| 63 | compilation.addRuntimeModule(
|
---|
| 64 | chunk,
|
---|
| 65 | new StartupChunkDependenciesRuntimeModule(
|
---|
| 66 | this.asyncChunkLoading
|
---|
| 67 | )
|
---|
| 68 | );
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | );
|
---|
| 72 | compilation.hooks.runtimeRequirementInTree
|
---|
| 73 | .for(RuntimeGlobals.startupEntrypoint)
|
---|
| 74 | .tap("StartupChunkDependenciesPlugin", (chunk, set) => {
|
---|
| 75 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 76 | set.add(RuntimeGlobals.require);
|
---|
| 77 | set.add(RuntimeGlobals.ensureChunk);
|
---|
| 78 | set.add(RuntimeGlobals.ensureChunkIncludeEntries);
|
---|
| 79 | compilation.addRuntimeModule(
|
---|
| 80 | chunk,
|
---|
| 81 | new StartupEntrypointRuntimeModule(this.asyncChunkLoading)
|
---|
| 82 | );
|
---|
| 83 | });
|
---|
| 84 | }
|
---|
| 85 | );
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | module.exports = StartupChunkDependenciesPlugin;
|
---|