2014-06-03 03:23:53 +08:00
|
|
|
/*
|
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
|
*/
|
2017-02-21 07:05:34 +08:00
|
|
|
"use strict";
|
2014-06-03 03:23:53 +08:00
|
|
|
|
2017-02-21 17:37:25 +08:00
|
|
|
const Template = require("../Template");
|
2014-06-03 03:23:53 +08:00
|
|
|
|
2017-02-21 07:05:34 +08:00
|
|
|
class WebWorkerMainTemplatePlugin {
|
|
|
|
|
apply(mainTemplate) {
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.plugin("local-vars", (source, chunk) => {
|
2017-09-22 20:07:28 +08:00
|
|
|
if(chunk.getNumberOfChunks() > 0) {
|
2017-11-08 18:32:05 +08:00
|
|
|
return mainTemplate.asString([
|
2017-02-21 07:05:34 +08:00
|
|
|
source,
|
|
|
|
|
"",
|
|
|
|
|
"// object to store loaded chunks",
|
|
|
|
|
"// \"1\" means \"already loaded\"",
|
|
|
|
|
"var installedChunks = {",
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.indent(
|
2017-06-20 13:40:27 +08:00
|
|
|
chunk.ids.map((id) => `${id}: 1`).join(",\n")
|
2017-02-21 07:05:34 +08:00
|
|
|
),
|
2017-05-24 15:44:35 +08:00
|
|
|
"};"
|
2017-02-21 07:05:34 +08:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
return source;
|
|
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.plugin("require-ensure", (_, chunk, hash) => {
|
|
|
|
|
const chunkFilename = mainTemplate.outputOptions.chunkFilename;
|
|
|
|
|
return mainTemplate.asString([
|
2017-11-21 19:56:55 +08:00
|
|
|
"promises.push(Promise.resolve().then(function() {",
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.indent([
|
2017-05-24 15:44:35 +08:00
|
|
|
"// \"1\" is the signal for \"already loaded\"",
|
|
|
|
|
"if(!installedChunks[chunkId]) {",
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.indent([
|
2017-05-24 15:44:35 +08:00
|
|
|
"importScripts(" +
|
2017-11-29 01:43:01 +08:00
|
|
|
mainTemplate.getAssetPath(JSON.stringify(chunkFilename), {
|
2017-11-08 18:32:05 +08:00
|
|
|
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
|
|
|
|
|
hashWithLength: (length) => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
|
2017-05-24 15:44:35 +08:00
|
|
|
chunk: {
|
|
|
|
|
id: "\" + chunkId + \""
|
|
|
|
|
}
|
|
|
|
|
}) + ");"
|
|
|
|
|
]),
|
|
|
|
|
"}",
|
2014-06-03 03:23:53 +08:00
|
|
|
]),
|
2017-11-21 19:56:55 +08:00
|
|
|
"}));"
|
2014-06-03 03:23:53 +08:00
|
|
|
]);
|
2015-10-18 04:40:31 +08:00
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.plugin("bootstrap", (source, chunk, hash) => {
|
2017-09-22 20:24:12 +08:00
|
|
|
if(chunk.getNumberOfChunks() > 0) {
|
2017-11-08 18:32:05 +08:00
|
|
|
const chunkCallbackName = mainTemplate.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (mainTemplate.outputOptions.library || ""));
|
|
|
|
|
return mainTemplate.asString([
|
2017-02-21 07:05:34 +08:00
|
|
|
source,
|
2017-11-21 19:56:55 +08:00
|
|
|
`self[${JSON.stringify(chunkCallbackName)}] = function webpackChunkCallback(chunkIds, moreModules) {`,
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.indent([
|
2017-02-21 07:05:34 +08:00
|
|
|
"for(var moduleId in moreModules) {",
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.indent(mainTemplate.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
|
2017-02-21 07:05:34 +08:00
|
|
|
"}",
|
|
|
|
|
"while(chunkIds.length)",
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.indent("installedChunks[chunkIds.pop()] = 1;")
|
2017-02-21 07:05:34 +08:00
|
|
|
]),
|
|
|
|
|
"};"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
return source;
|
2015-10-18 04:40:31 +08:00
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.plugin("hot-bootstrap", (source, chunk, hash) => {
|
|
|
|
|
const hotUpdateChunkFilename = mainTemplate.outputOptions.hotUpdateChunkFilename;
|
|
|
|
|
const hotUpdateMainFilename = mainTemplate.outputOptions.hotUpdateMainFilename;
|
|
|
|
|
const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (mainTemplate.outputOptions.library || ""));
|
2017-11-29 01:43:01 +08:00
|
|
|
const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(JSON.stringify(hotUpdateChunkFilename), {
|
2017-11-08 18:32:05 +08:00
|
|
|
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
|
|
|
|
|
hashWithLength: (length) => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
|
2017-02-21 07:05:34 +08:00
|
|
|
chunk: {
|
|
|
|
|
id: "\" + chunkId + \""
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-11-29 01:43:01 +08:00
|
|
|
const currentHotUpdateMainFilename = mainTemplate.getAssetPath(JSON.stringify(hotUpdateMainFilename), {
|
2017-11-08 18:32:05 +08:00
|
|
|
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
|
|
|
|
|
hashWithLength: (length) => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
|
2017-02-21 07:05:34 +08:00
|
|
|
});
|
2015-10-18 04:40:31 +08:00
|
|
|
|
2017-02-21 07:05:34 +08:00
|
|
|
return source + "\n" +
|
2017-10-30 15:43:56 +08:00
|
|
|
`var parentHotUpdateCallback = self[${JSON.stringify(hotUpdateFunction)}];\n` +
|
|
|
|
|
`self[${JSON.stringify(hotUpdateFunction)}] = ` +
|
2017-06-20 13:40:27 +08:00
|
|
|
Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js"))
|
2017-02-21 07:05:34 +08:00
|
|
|
.replace(/\/\/\$semicolon/g, ";")
|
2017-11-08 18:32:05 +08:00
|
|
|
.replace(/\$require\$/g, mainTemplate.requireFn)
|
2017-02-21 07:05:34 +08:00
|
|
|
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
|
|
|
|
|
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
|
|
|
|
|
.replace(/\$hash\$/g, JSON.stringify(hash));
|
|
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
mainTemplate.plugin("hash", hash => {
|
2017-02-21 07:05:34 +08:00
|
|
|
hash.update("webworker");
|
|
|
|
|
hash.update("3");
|
2017-11-08 18:32:05 +08:00
|
|
|
hash.update(`${mainTemplate.outputOptions.publicPath}`);
|
|
|
|
|
hash.update(`${mainTemplate.outputOptions.filename}`);
|
|
|
|
|
hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
|
|
|
|
|
hash.update(`${mainTemplate.outputOptions.chunkCallbackName}`);
|
|
|
|
|
hash.update(`${mainTemplate.outputOptions.library}`);
|
2017-02-21 07:05:34 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
module.exports = WebWorkerMainTemplatePlugin;
|