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");
|
2017-01-08 00:06:08 +08:00
|
|
|
const NodeChunkTemplatePlugin = require("./NodeChunkTemplatePlugin");
|
2018-11-23 16:37:33 +08:00
|
|
|
const ReadFileChunkLoadingRuntimeModule = require("./ReadFileChunkLoadingRuntimeModule");
|
|
|
|
const RequireChunkLoadingRuntimeModule = require("./RequireChunkLoadingRuntimeModule");
|
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
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
? ReadFileChunkLoadingRuntimeModule
|
|
|
|
: RequireChunkLoadingRuntimeModule;
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.thisCompilation.tap("NodeTemplatePlugin", compilation => {
|
2018-11-28 20:07:40 +08:00
|
|
|
new NodeChunkTemplatePlugin(compilation).apply(compilation.chunkTemplate);
|
|
|
|
|
|
|
|
const onceForChunkSet = new WeakSet();
|
|
|
|
const handler = (chunk, set) => {
|
|
|
|
if (onceForChunkSet.has(chunk)) return;
|
|
|
|
onceForChunkSet.add(chunk);
|
|
|
|
set.add(RuntimeGlobals.moduleFactories);
|
2019-01-05 01:40:17 +08:00
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
2019-05-22 19:07:10 +08:00
|
|
|
new ChunkLoadingRuntimeModule(chunk, compilation.chunkGraph, set)
|
2019-01-05 01:40:17 +08:00
|
|
|
);
|
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);
|
|
|
|
set.add(RuntimeGlobals.moduleFactories);
|
|
|
|
});
|
|
|
|
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;
|