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";
|
|
|
|
|
2019-10-04 18:24:52 +08:00
|
|
|
const { ConcatSource } = require("webpack-sources");
|
|
|
|
const HotUpdateChunk = require("../HotUpdateChunk");
|
2018-11-23 16:37:33 +08:00
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
2019-10-04 18:24:52 +08:00
|
|
|
const Template = require("../Template");
|
2019-10-11 21:46:57 +08:00
|
|
|
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
|
2020-06-29 17:46:16 +08:00
|
|
|
const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
|
2018-11-28 20:07:40 +08:00
|
|
|
const ImportScriptsChunkLoadingRuntimeModule = require("./ImportScriptsChunkLoadingRuntimeModule");
|
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
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
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) {
|
2020-06-29 17:46:16 +08:00
|
|
|
new StartupChunkDependenciesPlugin({
|
|
|
|
asyncChunkLoading: true
|
|
|
|
}).apply(compiler);
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.thisCompilation.tap(
|
|
|
|
"WebWorkerTemplatePlugin",
|
|
|
|
compilation => {
|
2019-10-04 18:24:52 +08:00
|
|
|
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
|
|
|
|
hooks.renderChunk.tap(
|
|
|
|
"WebWorkerTemplatePlugin",
|
|
|
|
(modules, renderContext) => {
|
|
|
|
const { chunk, chunkGraph, runtimeTemplate } = renderContext;
|
|
|
|
const hotUpdateChunk =
|
|
|
|
chunk instanceof HotUpdateChunk ? chunk : null;
|
|
|
|
const globalObject = runtimeTemplate.outputOptions.globalObject;
|
|
|
|
const source = new ConcatSource();
|
|
|
|
const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder(
|
|
|
|
chunk
|
|
|
|
);
|
|
|
|
const runtimePart =
|
|
|
|
runtimeModules.length > 0 &&
|
|
|
|
Template.renderChunkRuntimeModules(runtimeModules, renderContext);
|
|
|
|
if (hotUpdateChunk) {
|
2020-08-25 15:16:49 +08:00
|
|
|
const hotUpdateGlobal =
|
|
|
|
runtimeTemplate.outputOptions.hotUpdateGlobal;
|
|
|
|
source.add(
|
|
|
|
`${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`
|
|
|
|
);
|
2019-10-04 18:24:52 +08:00
|
|
|
source.add(modules);
|
|
|
|
if (runtimePart) {
|
|
|
|
source.add(",\n");
|
|
|
|
source.add(runtimePart);
|
|
|
|
}
|
|
|
|
source.add(")");
|
|
|
|
} else {
|
2020-08-25 15:16:49 +08:00
|
|
|
const chunkLoadingGlobal =
|
|
|
|
runtimeTemplate.outputOptions.chunkLoadingGlobal;
|
2019-10-04 18:24:52 +08:00
|
|
|
source.add(
|
2020-08-25 15:16:49 +08:00
|
|
|
`${globalObject}[${JSON.stringify(chunkLoadingGlobal)}](`
|
2019-10-04 18:24:52 +08:00
|
|
|
);
|
|
|
|
source.add(`${JSON.stringify(chunk.ids)},`);
|
|
|
|
source.add(modules);
|
|
|
|
if (runtimePart) {
|
|
|
|
source.add(",\n");
|
|
|
|
source.add(runtimePart);
|
|
|
|
}
|
|
|
|
source.add(")");
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
hooks.chunkHash.tap(
|
|
|
|
"WebWorkerTemplatePlugin",
|
|
|
|
(chunk, hash, { runtimeTemplate }) => {
|
2019-10-09 21:55:21 +08:00
|
|
|
if (chunk.hasRuntime()) return;
|
2019-10-04 18:24:52 +08:00
|
|
|
hash.update("webworker");
|
|
|
|
hash.update("1");
|
2020-08-25 15:16:49 +08:00
|
|
|
hash.update(`${runtimeTemplate.outputOptions.chunkLoadingGlobal}`);
|
|
|
|
hash.update(`${runtimeTemplate.outputOptions.hotUpdateGlobal}`);
|
2019-10-04 18:24:52 +08:00
|
|
|
hash.update(`${runtimeTemplate.outputOptions.globalObject}`);
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2018-11-23 16:37:33 +08:00
|
|
|
|
2018-11-28 20:07:40 +08:00
|
|
|
const onceForChunkSet = new WeakSet();
|
|
|
|
const handler = (chunk, set) => {
|
|
|
|
if (onceForChunkSet.has(chunk)) return;
|
|
|
|
onceForChunkSet.add(chunk);
|
2019-10-09 04:29:46 +08:00
|
|
|
set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
2019-12-03 21:27:39 +08:00
|
|
|
set.add(RuntimeGlobals.hasOwnProperty);
|
2018-11-28 20:07:40 +08:00
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
2019-08-27 02:21:07 +08:00
|
|
|
new ImportScriptsChunkLoadingRuntimeModule(set)
|
2018-11-28 20:07:40 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
2019-10-04 18:24:52 +08:00
|
|
|
.tap("WebWorkerTemplatePlugin", handler);
|
2018-11-28 20:07:40 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
2019-10-04 18:24:52 +08:00
|
|
|
.tap("WebWorkerTemplatePlugin", handler);
|
2018-11-28 20:07:40 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadManifest)
|
2019-10-04 18:24:52 +08:00
|
|
|
.tap("WebWorkerTemplatePlugin", handler);
|
2018-11-23 16:37:33 +08:00
|
|
|
|
2018-11-28 20:07:40 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.ensureChunkHandlers)
|
2019-10-04 18:24:52 +08:00
|
|
|
.tap("WebWorkerTemplatePlugin", (chunk, set) => {
|
2018-11-28 20:07:40 +08:00
|
|
|
set.add(RuntimeGlobals.publicPath);
|
|
|
|
set.add(RuntimeGlobals.getChunkScriptFilename);
|
|
|
|
});
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
2019-10-04 18:24:52 +08:00
|
|
|
.tap("WebWorkerTemplatePlugin", (chunk, set) => {
|
2018-11-28 20:07:40 +08:00
|
|
|
set.add(RuntimeGlobals.publicPath);
|
|
|
|
set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
|
|
|
set.add(RuntimeGlobals.moduleCache);
|
|
|
|
set.add(RuntimeGlobals.hmrModuleData);
|
2019-10-09 04:29:46 +08:00
|
|
|
set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
2018-11-28 20:07:40 +08:00
|
|
|
});
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.hmrDownloadManifest)
|
2019-10-04 18:24:52 +08:00
|
|
|
.tap("WebWorkerTemplatePlugin", (chunk, set) => {
|
2018-11-28 20:07:40 +08:00
|
|
|
set.add(RuntimeGlobals.publicPath);
|
|
|
|
set.add(RuntimeGlobals.getUpdateManifestFilename);
|
|
|
|
});
|
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;
|