2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
2017-01-08 00:06:08 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-11-23 16:37:33 +08:00
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
2020-08-25 16:30:56 +08:00
|
|
|
const CommonJsChunkFormatPlugin = require("../javascript/CommonJsChunkFormatPlugin");
|
2020-06-29 17:46:16 +08:00
|
|
|
const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
|
2017-01-08 00:06:08 +08:00
|
|
|
|
2018-11-09 05:59:19 +08:00
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
2017-01-08 00:06:08 +08:00
|
|
|
class NodeTemplatePlugin {
|
|
|
|
constructor(options) {
|
|
|
|
options = options || {};
|
|
|
|
this.asyncChunkLoading = options.asyncChunkLoading;
|
|
|
|
}
|
|
|
|
|
2018-11-09 05:59:19 +08:00
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
2018-11-09 05:59:19 +08:00
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-08 00:06:08 +08:00
|
|
|
apply(compiler) {
|
2019-01-05 01:40:17 +08:00
|
|
|
const ChunkLoadingRuntimeModule = this.asyncChunkLoading
|
2020-06-29 17:46:16 +08:00
|
|
|
? require("./ReadFileChunkLoadingRuntimeModule")
|
|
|
|
: require("./RequireChunkLoadingRuntimeModule");
|
2019-01-05 01:40:17 +08:00
|
|
|
|
2020-06-29 17:46:16 +08:00
|
|
|
new StartupChunkDependenciesPlugin({
|
|
|
|
asyncChunkLoading: this.asyncChunkLoading,
|
|
|
|
exposedGlobal: "module.exports"
|
|
|
|
}).apply(compiler);
|
2020-08-25 16:30:56 +08:00
|
|
|
new CommonJsChunkFormatPlugin().apply(compiler);
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.thisCompilation.tap("NodeTemplatePlugin", compilation => {
|
2018-11-28 20:07:40 +08:00
|
|
|
const onceForChunkSet = new WeakSet();
|
|
|
|
const handler = (chunk, set) => {
|
|
|
|
if (onceForChunkSet.has(chunk)) return;
|
|
|
|
onceForChunkSet.add(chunk);
|
2019-10-09 04:29:46 +08:00
|
|
|
set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
2019-12-03 23:11:46 +08:00
|
|
|
set.add(RuntimeGlobals.hasOwnProperty);
|
2019-08-27 02:21:07 +08:00
|
|
|
compilation.addRuntimeModule(chunk, new ChunkLoadingRuntimeModule(set));
|
2018-11-28 20:07:40 +08:00
|
|
|
};
|
|
|
|
|
2020-06-29 17:46:16 +08:00
|
|
|
compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
|
|
|
"NodeTemplatePlugin",
|
|
|
|
(chunk, set) => {
|
|
|
|
if (
|
|
|
|
Array.from(chunk.getAllReferencedChunks()).some(
|
|
|
|
c =>
|
|
|
|
c !== chunk &&
|
|
|
|
compilation.chunkGraph.getNumberOfEntryModules(c) > 0
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
set.add(RuntimeGlobals.startupEntrypoint);
|
|
|
|
set.add(RuntimeGlobals.externalInstallChunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2018-11-28 20:07:40 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
|
|
|
.tap("NodeTemplatePlugin", handler);
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|
|
|
.tap("NodeTemplatePlugin", handler);
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadManifest)
|
|
|
|
.tap("NodeTemplatePlugin", handler);
|
2018-11-23 16:37:33 +08:00
|
|
|
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
|
|
|
.tap("NodeTemplatePlugin", (chunk, set) => {
|
|
|
|
set.add(RuntimeGlobals.getChunkScriptFilename);
|
|
|
|
});
|
2018-11-28 20:07:40 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|
|
|
.tap("NodeTemplatePlugin", (chunk, set) => {
|
|
|
|
set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
|
|
|
set.add(RuntimeGlobals.moduleCache);
|
|
|
|
set.add(RuntimeGlobals.hmrModuleData);
|
2019-10-09 04:29:46 +08:00
|
|
|
set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
2018-11-28 20:07:40 +08:00
|
|
|
});
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadManifest)
|
|
|
|
.tap("NodeTemplatePlugin", (chunk, set) => {
|
|
|
|
set.add(RuntimeGlobals.getUpdateManifestFilename);
|
|
|
|
});
|
2017-01-08 00:06:08 +08:00
|
|
|
});
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2017-01-08 00:06:08 +08:00
|
|
|
|
2013-03-01 16:44:58 +08:00
|
|
|
module.exports = NodeTemplatePlugin;
|