| 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 StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 13 | /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Defines the common js chunk loading plugin options type used by this module.
|
|---|
| 17 | * @typedef {object} CommonJsChunkLoadingPluginOptions
|
|---|
| 18 | * @property {boolean=} asyncChunkLoading enable async chunk loading
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | const PLUGIN_NAME = "CommonJsChunkLoadingPlugin";
|
|---|
| 22 |
|
|---|
| 23 | class CommonJsChunkLoadingPlugin {
|
|---|
| 24 | /**
|
|---|
| 25 | * Creates an instance of CommonJsChunkLoadingPlugin.
|
|---|
| 26 | * @param {CommonJsChunkLoadingPluginOptions=} options options
|
|---|
| 27 | */
|
|---|
| 28 | constructor(options = {}) {
|
|---|
| 29 | /** @type {CommonJsChunkLoadingPluginOptions} */
|
|---|
| 30 | this.options = options;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 35 | * @param {Compiler} compiler the compiler instance
|
|---|
| 36 | * @returns {void}
|
|---|
| 37 | */
|
|---|
| 38 | apply(compiler) {
|
|---|
| 39 | const ChunkLoadingRuntimeModule = this.options.asyncChunkLoading
|
|---|
| 40 | ? require("./ReadFileChunkLoadingRuntimeModule")
|
|---|
| 41 | : require("./RequireChunkLoadingRuntimeModule");
|
|---|
| 42 | const chunkLoadingValue = this.options.asyncChunkLoading
|
|---|
| 43 | ? "async-node"
|
|---|
| 44 | : "require";
|
|---|
| 45 | new StartupChunkDependenciesPlugin({
|
|---|
| 46 | chunkLoading: chunkLoadingValue,
|
|---|
| 47 | asyncChunkLoading: this.options.asyncChunkLoading
|
|---|
| 48 | }).apply(compiler);
|
|---|
| 49 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 50 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
|---|
| 51 | /**
|
|---|
| 52 | * Checks whether this common js chunk loading plugin is enabled for chunk.
|
|---|
| 53 | * @param {Chunk} chunk chunk
|
|---|
| 54 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
|---|
| 55 | */
|
|---|
| 56 | const isEnabledForChunk = (chunk) => {
|
|---|
| 57 | const options = chunk.getEntryOptions();
|
|---|
| 58 | const chunkLoading =
|
|---|
| 59 | options && options.chunkLoading !== undefined
|
|---|
| 60 | ? options.chunkLoading
|
|---|
| 61 | : globalChunkLoading;
|
|---|
| 62 | return chunkLoading === chunkLoadingValue;
|
|---|
| 63 | };
|
|---|
| 64 | /** @type {WeakSet<Chunk>} */
|
|---|
| 65 | const onceForChunkSet = new WeakSet();
|
|---|
| 66 | /**
|
|---|
| 67 | * Handles the hook callback for this code path.
|
|---|
| 68 | * @param {Chunk} chunk chunk
|
|---|
| 69 | * @param {RuntimeRequirements} set runtime requirements
|
|---|
| 70 | */
|
|---|
| 71 | const handler = (chunk, set) => {
|
|---|
| 72 | if (onceForChunkSet.has(chunk)) return;
|
|---|
| 73 | onceForChunkSet.add(chunk);
|
|---|
| 74 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 75 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 76 | set.add(RuntimeGlobals.hasOwnProperty);
|
|---|
| 77 | compilation.addRuntimeModule(chunk, new ChunkLoadingRuntimeModule(set));
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 81 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 82 | .tap(PLUGIN_NAME, handler);
|
|---|
| 83 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 84 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 85 | .tap(PLUGIN_NAME, handler);
|
|---|
| 86 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 87 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 88 | .tap(PLUGIN_NAME, handler);
|
|---|
| 89 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 90 | .for(RuntimeGlobals.baseURI)
|
|---|
| 91 | .tap(PLUGIN_NAME, handler);
|
|---|
| 92 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 93 | .for(RuntimeGlobals.externalInstallChunk)
|
|---|
| 94 | .tap(PLUGIN_NAME, handler);
|
|---|
| 95 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 96 | .for(RuntimeGlobals.onChunksLoaded)
|
|---|
| 97 | .tap(PLUGIN_NAME, handler);
|
|---|
| 98 |
|
|---|
| 99 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 100 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 101 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 102 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 103 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
|---|
| 104 | });
|
|---|
| 105 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 106 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 107 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 108 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 109 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
|---|
| 110 | set.add(RuntimeGlobals.moduleCache);
|
|---|
| 111 | set.add(RuntimeGlobals.hmrModuleData);
|
|---|
| 112 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 113 | });
|
|---|
| 114 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 115 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 116 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 117 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 118 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
|---|
| 119 | });
|
|---|
| 120 | });
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | module.exports = CommonJsChunkLoadingPlugin;
|
|---|