| 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 | /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
|
|---|
| 15 |
|
|---|
| 16 | const PLUGIN_NAME = "ModuleChunkLoadingPlugin";
|
|---|
| 17 |
|
|---|
| 18 | class ModuleChunkLoadingPlugin {
|
|---|
| 19 | /**
|
|---|
| 20 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 21 | * @param {Compiler} compiler the compiler instance
|
|---|
| 22 | * @returns {void}
|
|---|
| 23 | */
|
|---|
| 24 | apply(compiler) {
|
|---|
| 25 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 26 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
|---|
| 27 | /**
|
|---|
| 28 | * Checks whether this module chunk loading plugin is enabled for chunk.
|
|---|
| 29 | * @param {Chunk} chunk chunk to check
|
|---|
| 30 | * @returns {boolean} true, when the plugin is enabled for the chunk
|
|---|
| 31 | */
|
|---|
| 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 === "import";
|
|---|
| 39 | };
|
|---|
| 40 | /** @type {WeakSet<Chunk>} */
|
|---|
| 41 | const onceForChunkSet = new WeakSet();
|
|---|
| 42 | /**
|
|---|
| 43 | * Handles the hook callback for this code path.
|
|---|
| 44 | * @param {Chunk} chunk chunk to check
|
|---|
| 45 | * @param {RuntimeRequirements} set runtime requirements
|
|---|
| 46 | */
|
|---|
| 47 | const handler = (chunk, set) => {
|
|---|
| 48 | if (onceForChunkSet.has(chunk)) return;
|
|---|
| 49 | onceForChunkSet.add(chunk);
|
|---|
| 50 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 51 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 52 | set.add(RuntimeGlobals.hasOwnProperty);
|
|---|
| 53 | compilation.addRuntimeModule(
|
|---|
| 54 | chunk,
|
|---|
| 55 | new ModuleChunkLoadingRuntimeModule(set)
|
|---|
| 56 | );
|
|---|
| 57 | };
|
|---|
| 58 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 59 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 60 | .tap(PLUGIN_NAME, handler);
|
|---|
| 61 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 62 | .for(RuntimeGlobals.baseURI)
|
|---|
| 63 | .tap(PLUGIN_NAME, handler);
|
|---|
| 64 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 65 | .for(RuntimeGlobals.externalInstallChunk)
|
|---|
| 66 | .tap(PLUGIN_NAME, handler);
|
|---|
| 67 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 68 | .for(RuntimeGlobals.onChunksLoaded)
|
|---|
| 69 | .tap(PLUGIN_NAME, handler);
|
|---|
| 70 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 71 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 72 | .tap(PLUGIN_NAME, handler);
|
|---|
| 73 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 74 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 75 | .tap(PLUGIN_NAME, handler);
|
|---|
| 76 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 77 | .for(RuntimeGlobals.externalInstallChunk)
|
|---|
| 78 | .tap(PLUGIN_NAME, (chunk) => {
|
|---|
| 79 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 80 | compilation.addRuntimeModule(
|
|---|
| 81 | chunk,
|
|---|
| 82 | new ExportWebpackRequireRuntimeModule()
|
|---|
| 83 | );
|
|---|
| 84 | });
|
|---|
| 85 |
|
|---|
| 86 | // We need public path only when we prefetch/preload chunk or public path is not `auto`
|
|---|
| 87 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 88 | .for(RuntimeGlobals.prefetchChunkHandlers)
|
|---|
| 89 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 90 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 91 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 92 | });
|
|---|
| 93 |
|
|---|
| 94 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 95 | .for(RuntimeGlobals.preloadChunkHandlers)
|
|---|
| 96 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 97 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 98 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 99 | });
|
|---|
| 100 |
|
|---|
| 101 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 102 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 103 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 104 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 105 |
|
|---|
| 106 | if (compilation.outputOptions.publicPath !== "auto") {
|
|---|
| 107 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
|---|
| 111 | });
|
|---|
| 112 |
|
|---|
| 113 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 114 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 115 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 116 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 117 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 118 | set.add(RuntimeGlobals.loadScript);
|
|---|
| 119 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
|---|
| 120 | set.add(RuntimeGlobals.moduleCache);
|
|---|
| 121 | set.add(RuntimeGlobals.hmrModuleData);
|
|---|
| 122 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 123 | });
|
|---|
| 124 |
|
|---|
| 125 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 126 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 127 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 128 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 129 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 130 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
|---|
| 131 | });
|
|---|
| 132 |
|
|---|
| 133 | compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
|---|
| 134 | PLUGIN_NAME,
|
|---|
| 135 | (chunk, set, { chunkGraph }) => {
|
|---|
| 136 | if (chunkGraph.hasChunkEntryDependentChunks(chunk)) {
|
|---|
| 137 | set.add(RuntimeGlobals.externalInstallChunk);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | );
|
|---|
| 141 | });
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | module.exports = ModuleChunkLoadingPlugin;
|
|---|