webpack/lib/node/NodeTemplatePlugin.js

117 lines
3.8 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const HotModuleReplacementPlugin = require("../HotModuleReplacementPlugin");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const NodeChunkTemplatePlugin = require("./NodeChunkTemplatePlugin");
const NodeHotUpdateChunkTemplatePlugin = require("./NodeHotUpdateChunkTemplatePlugin");
const ReadFileChunkLoadingRuntimeModule = require("./ReadFileChunkLoadingRuntimeModule");
const RequireChunkLoadingRuntimeModule = require("./RequireChunkLoadingRuntimeModule");
2018-11-09 05:59:19 +08:00
/** @typedef {import("../Compiler")} Compiler */
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}
*/
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.thisCompilation.tap("NodeTemplatePlugin", compilation => {
new NodeChunkTemplatePlugin().apply(compilation.chunkTemplate);
2018-02-25 09:00:20 +08:00
new NodeHotUpdateChunkTemplatePlugin().apply(
compilation.hotUpdateChunkTemplate
);
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),
{
hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
hashWithLength: length =>
`" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
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),
{
hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
hashWithLength: length =>
`" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`
}
);
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");
});
});
}
2013-01-31 01:49:25 +08:00
}
2013-03-01 16:44:58 +08:00
module.exports = NodeTemplatePlugin;