2014-06-03 05:40:50 +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-20 21:28:20 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-03-22 19:05:58 +08:00
|
|
|
const { ConcatSource } = require("webpack-sources");
|
2018-11-28 20:07:40 +08:00
|
|
|
const HotUpdateChunk = require("../HotUpdateChunk");
|
2014-06-03 05:40:50 +08:00
|
|
|
|
2018-07-24 23:35:36 +08:00
|
|
|
/** @typedef {import("../ChunkTemplate")} ChunkTemplate */
|
2018-11-28 20:07:40 +08:00
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
2018-07-24 23:35:36 +08:00
|
|
|
|
2017-02-20 21:28:20 +08:00
|
|
|
class WebWorkerChunkTemplatePlugin {
|
2018-11-28 20:07:40 +08:00
|
|
|
/**
|
|
|
|
* @param {Compilation} compilation the compilation
|
|
|
|
*/
|
|
|
|
constructor(compilation) {
|
|
|
|
this.compilation = compilation;
|
|
|
|
}
|
|
|
|
|
2018-07-24 23:35:36 +08:00
|
|
|
/**
|
|
|
|
* @param {ChunkTemplate} chunkTemplate the chunk template
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-20 21:28:20 +08:00
|
|
|
apply(chunkTemplate) {
|
2018-02-25 09:00:20 +08:00
|
|
|
chunkTemplate.hooks.render.tap(
|
|
|
|
"WebWorkerChunkTemplatePlugin",
|
2018-11-28 20:07:40 +08:00
|
|
|
(modules, moduleTemplate, { chunk, chunkGraph }) => {
|
|
|
|
const hotUpdateChunk = chunk instanceof HotUpdateChunk ? chunk : null;
|
2018-02-25 09:00:20 +08:00
|
|
|
const globalObject = chunkTemplate.outputOptions.globalObject;
|
|
|
|
const source = new ConcatSource();
|
2018-11-28 20:07:40 +08:00
|
|
|
const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder(chunk);
|
|
|
|
const runtimePart =
|
|
|
|
runtimeModules.length > 0 &&
|
|
|
|
`,${JSON.stringify(
|
|
|
|
runtimeModules.map(m => chunkGraph.getModuleId(m))
|
|
|
|
)}`;
|
|
|
|
if (hotUpdateChunk) {
|
|
|
|
const jsonpFunction = chunkTemplate.outputOptions.hotUpdateFunction;
|
|
|
|
source.add(`${globalObject}[${JSON.stringify(jsonpFunction)}](`);
|
|
|
|
source.add(modules);
|
|
|
|
if (runtimePart) {
|
|
|
|
source.add(runtimePart);
|
|
|
|
}
|
|
|
|
source.add(")");
|
|
|
|
} else {
|
|
|
|
const chunkCallbackName =
|
|
|
|
chunkTemplate.outputOptions.chunkCallbackName;
|
|
|
|
source.add(`${globalObject}[${JSON.stringify(chunkCallbackName)}](`);
|
|
|
|
source.add(`${JSON.stringify(chunk.ids)},`);
|
|
|
|
source.add(modules);
|
|
|
|
if (runtimePart) {
|
|
|
|
source.add(runtimePart);
|
|
|
|
}
|
|
|
|
source.add(")");
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
);
|
2017-12-14 04:35:39 +08:00
|
|
|
chunkTemplate.hooks.hash.tap("WebWorkerChunkTemplatePlugin", hash => {
|
2017-02-20 21:28:20 +08:00
|
|
|
hash.update("webworker");
|
2018-11-28 20:07:40 +08:00
|
|
|
hash.update("4");
|
2017-11-08 18:32:05 +08:00
|
|
|
hash.update(`${chunkTemplate.outputOptions.chunkCallbackName}`);
|
2018-11-24 02:22:29 +08:00
|
|
|
hash.update(`${chunkTemplate.outputOptions.hotUpdateFunction}`);
|
2017-12-28 01:46:37 +08:00
|
|
|
hash.update(`${chunkTemplate.outputOptions.globalObject}`);
|
2017-02-20 21:28:20 +08:00
|
|
|
});
|
2018-11-28 20:07:40 +08:00
|
|
|
chunkTemplate.hooks.hashForChunk.tap(
|
|
|
|
"WebWorkerChunkTemplatePlugin",
|
|
|
|
(hash, chunk) => {
|
|
|
|
const chunkGraph = this.compilation.chunkGraph;
|
|
|
|
const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder(chunk);
|
|
|
|
hash.update(
|
|
|
|
JSON.stringify(runtimeModules.map(m => chunkGraph.getModuleId(m)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-02-20 21:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = WebWorkerChunkTemplatePlugin;
|