| 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 | * Options that describe which chunk loading backend should receive startup
|
|---|
| 17 | * dependency handling and whether the runtime should wait for those chunks
|
|---|
| 18 | * asynchronously.
|
|---|
| 19 | * @typedef {object} Options
|
|---|
| 20 | * @property {ChunkLoadingType} chunkLoading
|
|---|
| 21 | * @property {boolean=} asyncChunkLoading
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | const PLUGIN_NAME = "StartupChunkDependenciesPlugin";
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Adds runtime modules that delay entry startup until entry-dependent chunks
|
|---|
| 28 | * required by the selected chunk loading strategy have been loaded.
|
|---|
| 29 | */
|
|---|
| 30 | class StartupChunkDependenciesPlugin {
|
|---|
| 31 | /**
|
|---|
| 32 | * Configures which chunk loading implementation this plugin should enhance
|
|---|
| 33 | * and whether startup waits should use promises or synchronous ensures.
|
|---|
| 34 | * @param {Options} options options
|
|---|
| 35 | */
|
|---|
| 36 | constructor(options) {
|
|---|
| 37 | /** @type {ChunkLoadingType} */
|
|---|
| 38 | this.chunkLoading = options.chunkLoading;
|
|---|
| 39 | /** @type {boolean} */
|
|---|
| 40 | this.asyncChunkLoading =
|
|---|
| 41 | typeof options.asyncChunkLoading === "boolean"
|
|---|
| 42 | ? options.asyncChunkLoading
|
|---|
| 43 | : true;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Registers compilation hooks that attach the startup dependency runtime
|
|---|
| 48 | * modules to entry chunks using the configured chunk loading mechanism.
|
|---|
| 49 | * @param {Compiler} compiler the compiler instance
|
|---|
| 50 | * @returns {void}
|
|---|
| 51 | */
|
|---|
| 52 | apply(compiler) {
|
|---|
| 53 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 54 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
|---|
| 55 | /**
|
|---|
| 56 | * Determines whether a chunk uses the chunk loading backend that this
|
|---|
| 57 | * plugin is responsible for augmenting.
|
|---|
| 58 | * @param {Chunk} chunk chunk to check
|
|---|
| 59 | * @returns {boolean} true, when the plugin is enabled for the chunk
|
|---|
| 60 | */
|
|---|
| 61 | const isEnabledForChunk = (chunk) => {
|
|---|
| 62 | const options = chunk.getEntryOptions();
|
|---|
| 63 | const chunkLoading =
|
|---|
| 64 | options && options.chunkLoading !== undefined
|
|---|
| 65 | ? options.chunkLoading
|
|---|
| 66 | : globalChunkLoading;
|
|---|
| 67 | return chunkLoading === this.chunkLoading;
|
|---|
| 68 | };
|
|---|
| 69 | compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
|---|
| 70 | PLUGIN_NAME,
|
|---|
| 71 | (chunk, set, { chunkGraph }) => {
|
|---|
| 72 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 73 | if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
|
|---|
| 74 | set.add(RuntimeGlobals.startup);
|
|---|
| 75 | set.add(RuntimeGlobals.ensureChunk);
|
|---|
| 76 | set.add(RuntimeGlobals.ensureChunkIncludeEntries);
|
|---|
| 77 | compilation.addRuntimeModule(
|
|---|
| 78 | chunk,
|
|---|
| 79 | new StartupChunkDependenciesRuntimeModule(this.asyncChunkLoading)
|
|---|
| 80 | );
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | );
|
|---|
| 84 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 85 | .for(RuntimeGlobals.startupEntrypoint)
|
|---|
| 86 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 87 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 88 | set.add(RuntimeGlobals.require);
|
|---|
| 89 | set.add(RuntimeGlobals.ensureChunk);
|
|---|
| 90 | set.add(RuntimeGlobals.ensureChunkIncludeEntries);
|
|---|
| 91 | compilation.addRuntimeModule(
|
|---|
| 92 | chunk,
|
|---|
| 93 | new StartupEntrypointRuntimeModule(this.asyncChunkLoading)
|
|---|
| 94 | );
|
|---|
| 95 | });
|
|---|
| 96 | });
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | module.exports = StartupChunkDependenciesPlugin;
|
|---|