2020-08-25 19:20:29 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
2020-08-25 19:42:22 +08:00
|
|
|
const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
|
2020-08-25 19:20:29 +08:00
|
|
|
const ImportScriptsChunkLoadingRuntimeModule = require("./ImportScriptsChunkLoadingRuntimeModule");
|
|
|
|
|
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
2020-08-26 03:45:56 +08:00
|
|
|
class ImportScriptsChunkLoadingPlugin {
|
2020-08-25 19:20:29 +08:00
|
|
|
/**
|
|
|
|
* Apply the plugin
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
apply(compiler) {
|
2020-08-25 19:42:22 +08:00
|
|
|
new StartupChunkDependenciesPlugin({
|
|
|
|
asyncChunkLoading: true
|
|
|
|
}).apply(compiler);
|
2020-08-25 19:20:29 +08:00
|
|
|
compiler.hooks.thisCompilation.tap(
|
2020-08-26 03:45:56 +08:00
|
|
|
"ImportScriptsChunkLoadingPlugin",
|
2020-08-25 19:20:29 +08:00
|
|
|
compilation => {
|
2020-08-25 19:37:40 +08:00
|
|
|
const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
|
|
|
const isEnabledForChunk = chunk => {
|
2020-08-26 03:45:56 +08:00
|
|
|
const entry = compilation.entries.get(chunk.name);
|
|
|
|
const chunkLoading =
|
|
|
|
(entry && entry.options.chunkLoading) || globalChunkLoading;
|
|
|
|
return chunkLoading === "import-scripts";
|
2020-08-25 19:37:40 +08:00
|
|
|
};
|
2020-08-25 19:20:29 +08:00
|
|
|
const onceForChunkSet = new WeakSet();
|
|
|
|
const handler = (chunk, set) => {
|
|
|
|
if (onceForChunkSet.has(chunk)) return;
|
|
|
|
onceForChunkSet.add(chunk);
|
2020-08-25 19:37:40 +08:00
|
|
|
if (!isEnabledForChunk(chunk)) return;
|
2020-08-25 19:20:29 +08:00
|
|
|
set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|
|
|
set.add(RuntimeGlobals.hasOwnProperty);
|
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
|
|
|
new ImportScriptsChunkLoadingRuntimeModule(set)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
2020-08-26 03:45:56 +08:00
|
|
|
.tap("ImportScriptsChunkLoadingPlugin", handler);
|
2020-08-25 19:20:29 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
2020-08-26 03:45:56 +08:00
|
|
|
.tap("ImportScriptsChunkLoadingPlugin", handler);
|
2020-08-25 19:20:29 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadManifest)
|
2020-08-26 03:45:56 +08:00
|
|
|
.tap("ImportScriptsChunkLoadingPlugin", handler);
|
2020-08-29 04:39:07 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.baseURI)
|
|
|
|
.tap("ImportScriptsChunkLoadingPlugin", handler);
|
2020-08-25 19:20:29 +08:00
|
|
|
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
2020-08-26 03:45:56 +08:00
|
|
|
.tap("ImportScriptsChunkLoadingPlugin", (chunk, set) => {
|
2020-08-25 19:37:40 +08:00
|
|
|
if (!isEnabledForChunk(chunk)) return;
|
2020-08-25 19:20:29 +08:00
|
|
|
set.add(RuntimeGlobals.publicPath);
|
|
|
|
set.add(RuntimeGlobals.getChunkScriptFilename);
|
|
|
|
});
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
2020-08-26 03:45:56 +08:00
|
|
|
.tap("ImportScriptsChunkLoadingPlugin", (chunk, set) => {
|
2020-08-25 19:37:40 +08:00
|
|
|
if (!isEnabledForChunk(chunk)) return;
|
2020-08-25 19:20:29 +08:00
|
|
|
set.add(RuntimeGlobals.publicPath);
|
|
|
|
set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
|
|
|
set.add(RuntimeGlobals.moduleCache);
|
|
|
|
set.add(RuntimeGlobals.hmrModuleData);
|
|
|
|
set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|
|
|
});
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadManifest)
|
2020-08-26 03:45:56 +08:00
|
|
|
.tap("ImportScriptsChunkLoadingPlugin", (chunk, set) => {
|
2020-08-25 19:37:40 +08:00
|
|
|
if (!isEnabledForChunk(chunk)) return;
|
2020-08-25 19:20:29 +08:00
|
|
|
set.add(RuntimeGlobals.publicPath);
|
|
|
|
set.add(RuntimeGlobals.getUpdateManifestFilename);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 03:45:56 +08:00
|
|
|
module.exports = ImportScriptsChunkLoadingPlugin;
|