mirror of https://github.com/webpack/webpack.git
use array push chunk format for web workers too
This commit is contained in:
parent
b475d5fd0c
commit
e5ba0356f5
|
|
@ -25,6 +25,7 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
|
|||
const {
|
||||
chunk,
|
||||
compilation: {
|
||||
runtimeTemplate,
|
||||
outputOptions: { globalObject, chunkLoadingGlobal, hotUpdateGlobal }
|
||||
}
|
||||
} = this;
|
||||
|
|
@ -58,36 +59,35 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
|
|||
withLoading
|
||||
? Template.asString([
|
||||
"// importScripts chunk loading",
|
||||
`${fn}.i = function(chunkId, promises) {`,
|
||||
Template.indent([
|
||||
`${globalObject}[${JSON.stringify(
|
||||
chunkLoadingGlobal
|
||||
)}] = { push: ${runtimeTemplate.basicFunction("data", [
|
||||
"var chunkIds = data[0];",
|
||||
"var moreModules = data[1];",
|
||||
"var runtime = data[2];",
|
||||
"for(var moduleId in moreModules) {",
|
||||
Template.indent([
|
||||
`if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
|
||||
Template.indent(
|
||||
`${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`
|
||||
),
|
||||
"}"
|
||||
]),
|
||||
"}",
|
||||
"if(runtime) runtime(__webpack_require__);",
|
||||
"while(chunkIds.length)",
|
||||
Template.indent("installedChunks[chunkIds.pop()] = 1;")
|
||||
])} };`,
|
||||
`${fn}.i = ${runtimeTemplate.basicFunction("chunkId, promises", [
|
||||
'// "1" is the signal for "already loaded"',
|
||||
"if(!installedChunks[chunkId]) {",
|
||||
Template.indent([
|
||||
`${globalObject}[${JSON.stringify(
|
||||
chunkLoadingGlobal
|
||||
)}] = function webpackChunkCallback(chunkIds, moreModules, runtime) {`,
|
||||
Template.indent([
|
||||
"for(var moduleId in moreModules) {",
|
||||
Template.indent([
|
||||
`if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
|
||||
Template.indent(
|
||||
`${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`
|
||||
),
|
||||
"}"
|
||||
]),
|
||||
"}",
|
||||
"if(runtime) runtime(__webpack_require__);",
|
||||
"while(chunkIds.length)",
|
||||
Template.indent("installedChunks[chunkIds.pop()] = 1;")
|
||||
]),
|
||||
"};",
|
||||
`importScripts(${JSON.stringify(rootOutputDir)} + ${
|
||||
RuntimeGlobals.getChunkScriptFilename
|
||||
}(chunkId));`
|
||||
]),
|
||||
"}"
|
||||
]),
|
||||
"};"
|
||||
])};`
|
||||
])
|
||||
: "// no chunk loading",
|
||||
"",
|
||||
|
|
@ -98,8 +98,7 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
|
|||
"var success = false;",
|
||||
`${globalObject}[${JSON.stringify(
|
||||
hotUpdateGlobal
|
||||
)}] = function(moreModules, runtime) {`,
|
||||
Template.indent([
|
||||
)}] = ${runtimeTemplate.basicFunction("_, moreModules, runtime", [
|
||||
"for(var moduleId in moreModules) {",
|
||||
Template.indent([
|
||||
`if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
|
||||
|
|
@ -112,8 +111,7 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
|
|||
"}",
|
||||
"if(runtime) currentUpdateRuntime.push(runtime);",
|
||||
"success = true;"
|
||||
]),
|
||||
"};",
|
||||
])};`,
|
||||
"// start update chunk loading",
|
||||
`importScripts(${JSON.stringify(rootOutputDir)} + ${
|
||||
RuntimeGlobals.getChunkUpdateScriptFilename
|
||||
|
|
@ -149,18 +147,18 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
|
|||
"",
|
||||
withHmrManifest
|
||||
? Template.asString([
|
||||
`${RuntimeGlobals.hmrDownloadManifest} = function() {`,
|
||||
Template.indent([
|
||||
`${
|
||||
RuntimeGlobals.hmrDownloadManifest
|
||||
} = ${runtimeTemplate.basicFunction("", [
|
||||
'if (typeof fetch === "undefined") throw new Error("No browser support: need fetch API");',
|
||||
`return fetch(${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getUpdateManifestFilename}()).then(function(response) {`,
|
||||
Template.indent([
|
||||
`return fetch(${RuntimeGlobals.publicPath} + ${
|
||||
RuntimeGlobals.getUpdateManifestFilename
|
||||
}()).then(${runtimeTemplate.basicFunction("response", [
|
||||
"if(response.status === 404) return; // no update available",
|
||||
'if(!response.ok) throw new Error("Failed to fetch update manifest " + response.statusText);',
|
||||
"return response.json();"
|
||||
]),
|
||||
"});"
|
||||
]),
|
||||
"};"
|
||||
])});`
|
||||
])};`
|
||||
])
|
||||
: "// no HMR manifest"
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -5,11 +5,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { ConcatSource } = require("webpack-sources");
|
||||
const HotUpdateChunk = require("../HotUpdateChunk");
|
||||
const RuntimeGlobals = require("../RuntimeGlobals");
|
||||
const Template = require("../Template");
|
||||
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
|
||||
const ArrayPushCallbackChunkFormatPlugin = require("../javascript/ArrayPushCallbackChunkFormatPlugin");
|
||||
const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
|
||||
const ImportScriptsChunkLoadingRuntimeModule = require("./ImportScriptsChunkLoadingRuntimeModule");
|
||||
|
||||
|
|
@ -25,65 +22,10 @@ class WebWorkerTemplatePlugin {
|
|||
new StartupChunkDependenciesPlugin({
|
||||
asyncChunkLoading: true
|
||||
}).apply(compiler);
|
||||
new ArrayPushCallbackChunkFormatPlugin().apply(compiler);
|
||||
compiler.hooks.thisCompilation.tap(
|
||||
"WebWorkerTemplatePlugin",
|
||||
compilation => {
|
||||
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) {
|
||||
const hotUpdateGlobal =
|
||||
runtimeTemplate.outputOptions.hotUpdateGlobal;
|
||||
source.add(
|
||||
`${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`
|
||||
);
|
||||
source.add(modules);
|
||||
if (runtimePart) {
|
||||
source.add(",\n");
|
||||
source.add(runtimePart);
|
||||
}
|
||||
source.add(")");
|
||||
} else {
|
||||
const chunkLoadingGlobal =
|
||||
runtimeTemplate.outputOptions.chunkLoadingGlobal;
|
||||
source.add(
|
||||
`${globalObject}[${JSON.stringify(chunkLoadingGlobal)}](`
|
||||
);
|
||||
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 }) => {
|
||||
if (chunk.hasRuntime()) return;
|
||||
hash.update("webworker");
|
||||
hash.update("1");
|
||||
hash.update(`${runtimeTemplate.outputOptions.chunkLoadingGlobal}`);
|
||||
hash.update(`${runtimeTemplate.outputOptions.hotUpdateGlobal}`);
|
||||
hash.update(`${runtimeTemplate.outputOptions.globalObject}`);
|
||||
}
|
||||
);
|
||||
|
||||
const onceForChunkSet = new WeakSet();
|
||||
const handler = (chunk, set) => {
|
||||
if (onceForChunkSet.has(chunk)) return;
|
||||
|
|
|
|||
Loading…
Reference in New Issue