2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-02-21 07:05:34 +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-02-21 17:38:24 +08:00
|
|
|
const WebWorkerChunkTemplatePlugin = require("./WebWorkerChunkTemplatePlugin");
|
|
|
|
const WebWorkerHotUpdateChunkTemplatePlugin = require("./WebWorkerHotUpdateChunkTemplatePlugin");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-11-09 05:59:19 +08:00
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
2017-02-21 07:05:34 +08:00
|
|
|
class WebWorkerTemplatePlugin {
|
2018-11-09 05:59:19 +08:00
|
|
|
/**
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-21 07:05:34 +08:00
|
|
|
apply(compiler) {
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.thisCompilation.tap(
|
|
|
|
"WebWorkerTemplatePlugin",
|
|
|
|
compilation => {
|
|
|
|
new WebWorkerChunkTemplatePlugin().apply(compilation.chunkTemplate);
|
|
|
|
new WebWorkerHotUpdateChunkTemplatePlugin().apply(
|
|
|
|
compilation.hotUpdateChunkTemplate
|
|
|
|
);
|
2018-11-23 16:37:33 +08:00
|
|
|
|
|
|
|
compilation.hooks.runtimeRequirementInTree.for(
|
|
|
|
RuntimeGlobals.ensureChunkHandlers
|
|
|
|
);
|
|
|
|
|
|
|
|
const mainTemplate = compilation.mainTemplate;
|
|
|
|
const {
|
|
|
|
hotBootstrap
|
|
|
|
} = HotModuleReplacementPlugin.getMainTemplateHooks(mainTemplate);
|
|
|
|
hotBootstrap.tap(
|
|
|
|
"WebWorkerMainTemplatePlugin",
|
|
|
|
(source, chunk, hash) => {
|
|
|
|
const hotUpdateChunkFilename =
|
|
|
|
mainTemplate.outputOptions.hotUpdateChunkFilename;
|
|
|
|
const hotUpdateMainFilename =
|
|
|
|
mainTemplate.outputOptions.hotUpdateMainFilename;
|
|
|
|
const hotUpdateFunction =
|
|
|
|
mainTemplate.outputOptions.hotUpdateFunction;
|
|
|
|
const globalObject = mainTemplate.outputOptions.globalObject;
|
|
|
|
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 + "'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
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 (
|
|
|
|
source +
|
|
|
|
"\n" +
|
|
|
|
`var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(
|
|
|
|
hotUpdateFunction
|
|
|
|
)}];\n` +
|
|
|
|
`${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ` +
|
|
|
|
Template.getFunctionContent(
|
|
|
|
require("./WebWorkerMainTemplate.runtime")
|
|
|
|
)
|
|
|
|
.replace(/\/\/\$semicolon/g, ";")
|
|
|
|
.replace(/\$publicPath\$/g, RuntimeGlobals.publicPath)
|
|
|
|
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
|
|
|
|
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
|
|
|
|
.replace(/\$hash\$/g, JSON.stringify(hash))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
mainTemplate.hooks.hash.tap("WebWorkerMainTemplatePlugin", hash => {
|
|
|
|
hash.update("webworker");
|
|
|
|
hash.update("4");
|
|
|
|
});
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
);
|
2017-02-21 07:05:34 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
module.exports = WebWorkerTemplatePlugin;
|