[6a3a178] | 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("../Compiler")} Compiler */
|
---|
| 12 |
|
---|
| 13 | class CommonJsChunkLoadingPlugin {
|
---|
| 14 | constructor(options) {
|
---|
| 15 | options = options || {};
|
---|
| 16 | this._asyncChunkLoading = options.asyncChunkLoading;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Apply the plugin
|
---|
| 21 | * @param {Compiler} compiler the compiler instance
|
---|
| 22 | * @returns {void}
|
---|
| 23 | */
|
---|
| 24 | apply(compiler) {
|
---|
| 25 | const ChunkLoadingRuntimeModule = this._asyncChunkLoading
|
---|
| 26 | ? require("./ReadFileChunkLoadingRuntimeModule")
|
---|
| 27 | : require("./RequireChunkLoadingRuntimeModule");
|
---|
| 28 | const chunkLoadingValue = this._asyncChunkLoading
|
---|
| 29 | ? "async-node"
|
---|
| 30 | : "require";
|
---|
| 31 | new StartupChunkDependenciesPlugin({
|
---|
| 32 | chunkLoading: chunkLoadingValue,
|
---|
| 33 | asyncChunkLoading: this._asyncChunkLoading
|
---|
| 34 | }).apply(compiler);
|
---|
| 35 | compiler.hooks.thisCompilation.tap(
|
---|
| 36 | "CommonJsChunkLoadingPlugin",
|
---|
| 37 | compilation => {
|
---|
| 38 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
| 39 | const isEnabledForChunk = chunk => {
|
---|
| 40 | const options = chunk.getEntryOptions();
|
---|
| 41 | const chunkLoading =
|
---|
| 42 | options && options.chunkLoading !== undefined
|
---|
| 43 | ? options.chunkLoading
|
---|
| 44 | : globalChunkLoading;
|
---|
| 45 | return chunkLoading === chunkLoadingValue;
|
---|
| 46 | };
|
---|
| 47 | const onceForChunkSet = new WeakSet();
|
---|
| 48 | const handler = (chunk, set) => {
|
---|
| 49 | if (onceForChunkSet.has(chunk)) return;
|
---|
| 50 | onceForChunkSet.add(chunk);
|
---|
| 51 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 52 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
| 53 | set.add(RuntimeGlobals.hasOwnProperty);
|
---|
| 54 | compilation.addRuntimeModule(
|
---|
| 55 | chunk,
|
---|
| 56 | new ChunkLoadingRuntimeModule(set)
|
---|
| 57 | );
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | compilation.hooks.runtimeRequirementInTree
|
---|
| 61 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
| 62 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
| 63 | compilation.hooks.runtimeRequirementInTree
|
---|
| 64 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
| 65 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
| 66 | compilation.hooks.runtimeRequirementInTree
|
---|
| 67 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
| 68 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
| 69 | compilation.hooks.runtimeRequirementInTree
|
---|
| 70 | .for(RuntimeGlobals.baseURI)
|
---|
| 71 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
| 72 | compilation.hooks.runtimeRequirementInTree
|
---|
| 73 | .for(RuntimeGlobals.externalInstallChunk)
|
---|
| 74 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
| 75 | compilation.hooks.runtimeRequirementInTree
|
---|
| 76 | .for(RuntimeGlobals.onChunksLoaded)
|
---|
| 77 | .tap("CommonJsChunkLoadingPlugin", handler);
|
---|
| 78 |
|
---|
| 79 | compilation.hooks.runtimeRequirementInTree
|
---|
| 80 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
| 81 | .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
|
---|
| 82 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 83 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
| 84 | });
|
---|
| 85 | compilation.hooks.runtimeRequirementInTree
|
---|
| 86 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
| 87 | .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
|
---|
| 88 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 89 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
---|
| 90 | set.add(RuntimeGlobals.moduleCache);
|
---|
| 91 | set.add(RuntimeGlobals.hmrModuleData);
|
---|
| 92 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
| 93 | });
|
---|
| 94 | compilation.hooks.runtimeRequirementInTree
|
---|
| 95 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
| 96 | .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
|
---|
| 97 | if (!isEnabledForChunk(chunk)) return;
|
---|
| 98 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
---|
| 99 | });
|
---|
| 100 | }
|
---|
| 101 | );
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | module.exports = CommonJsChunkLoadingPlugin;
|
---|