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 HotModuleReplacementPlugin = require("../HotModuleReplacementPlugin");
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const Template = require("../Template");
|
2017-01-08 00:06:08 +08:00
|
|
|
const NodeChunkTemplatePlugin = require("./NodeChunkTemplatePlugin");
|
|
|
|
const NodeHotUpdateChunkTemplatePlugin = require("./NodeHotUpdateChunkTemplatePlugin");
|
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) {
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.thisCompilation.tap("NodeTemplatePlugin", compilation => {
|
2017-12-20 16:53:33 +08:00
|
|
|
new NodeChunkTemplatePlugin().apply(compilation.chunkTemplate);
|
2018-02-25 09:00:20 +08:00
|
|
|
new NodeHotUpdateChunkTemplatePlugin().apply(
|
|
|
|
compilation.hotUpdateChunkTemplate
|
|
|
|
);
|
2018-11-23 16:37:33 +08:00
|
|
|
|
|
|
|
const mainTemplate = compilation.mainTemplate;
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
|
|
|
.tap("NodeTemplatePlugin", (chunk, set) => {
|
|
|
|
set.add(RuntimeGlobals.moduleFactories);
|
|
|
|
set.add(RuntimeGlobals.getChunkScriptFilename);
|
|
|
|
if (this.asyncChunkLoading) {
|
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
|
|
|
new ReadFileChunkLoadingRuntimeModule(chunk)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
|
|
|
new RequireChunkLoadingRuntimeModule(chunk)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const { hotBootstrap } = HotModuleReplacementPlugin.getMainTemplateHooks(
|
|
|
|
mainTemplate
|
|
|
|
);
|
|
|
|
|
|
|
|
hotBootstrap.tap("NodeTemplatePlugin", (source, chunk, hash) => {
|
|
|
|
const hotUpdateChunkFilename =
|
|
|
|
mainTemplate.outputOptions.hotUpdateChunkFilename;
|
|
|
|
const hotUpdateMainFilename =
|
|
|
|
mainTemplate.outputOptions.hotUpdateMainFilename;
|
|
|
|
const chunkMaps = chunk.getChunkMaps();
|
|
|
|
const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(
|
|
|
|
JSON.stringify(hotUpdateChunkFilename),
|
|
|
|
{
|
2018-11-26 16:49:28 +08:00
|
|
|
hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
|
2018-11-23 16:37:33 +08:00
|
|
|
hashWithLength: length =>
|
2018-11-26 16:49:28 +08:00
|
|
|
`" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
|
2018-11-23 16:37:33 +08:00
|
|
|
chunk: {
|
|
|
|
id: '" + chunkId + "',
|
|
|
|
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
|
|
|
|
hashWithLength: length => {
|
|
|
|
const shortChunkHashMap = {};
|
|
|
|
for (const chunkId of Object.keys(chunkMaps.hash)) {
|
|
|
|
if (typeof chunkMaps.hash[chunkId] === "string") {
|
|
|
|
shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(
|
|
|
|
0,
|
|
|
|
length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
|
|
|
|
},
|
|
|
|
name: `" + (${JSON.stringify(
|
|
|
|
chunkMaps.name
|
|
|
|
)}[chunkId]||chunkId) + "`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const currentHotUpdateMainFilename = mainTemplate.getAssetPath(
|
|
|
|
JSON.stringify(hotUpdateMainFilename),
|
|
|
|
{
|
2018-11-26 16:49:28 +08:00
|
|
|
hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
|
2018-11-23 16:37:33 +08:00
|
|
|
hashWithLength: length =>
|
2018-11-26 16:49:28 +08:00
|
|
|
`" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`
|
2018-11-23 16:37:33 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
return Template.getFunctionContent(
|
|
|
|
this.asyncChunkLoading
|
|
|
|
? require("./NodeMainTemplateAsync.runtime")
|
|
|
|
: require("./NodeMainTemplate.runtime")
|
|
|
|
)
|
|
|
|
.replace(/\$onError\$/g, RuntimeGlobals.uncaughtErrorHandler)
|
|
|
|
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
|
|
|
|
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename);
|
|
|
|
});
|
|
|
|
mainTemplate.hooks.hash.tap("NodeTemplatePlugin", hash => {
|
|
|
|
hash.update("node");
|
|
|
|
hash.update("4");
|
|
|
|
});
|
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;
|