2018-11-23 16:37:33 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const RuntimeModule = require("../RuntimeModule");
|
|
|
|
const Template = require("../Template");
|
2020-08-04 04:55:51 +08:00
|
|
|
const {
|
2021-03-12 01:32:32 +08:00
|
|
|
getChunkFilenameTemplate,
|
|
|
|
chunkHasJs
|
2020-08-04 04:55:51 +08:00
|
|
|
} = require("../javascript/JavascriptModulesPlugin");
|
2021-03-15 02:54:34 +08:00
|
|
|
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
|
2021-03-12 01:32:32 +08:00
|
|
|
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
|
2020-08-04 04:55:51 +08:00
|
|
|
const { getUndoPath } = require("../util/identifier");
|
2018-11-23 16:37:33 +08:00
|
|
|
|
2022-02-21 21:23:47 +08:00
|
|
|
/** @typedef {import("../Chunk")} Chunk */
|
2023-06-12 22:21:21 +08:00
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
2022-02-21 21:23:47 +08:00
|
|
|
|
2018-11-23 16:37:33 +08:00
|
|
|
class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
|
2023-05-22 10:12:44 +08:00
|
|
|
/**
|
|
|
|
* @param {Set<string>} runtimeRequirements runtime requirements
|
|
|
|
* @param {boolean} withCreateScriptUrl with createScriptUrl support
|
|
|
|
*/
|
2021-05-10 15:33:04 +08:00
|
|
|
constructor(runtimeRequirements, withCreateScriptUrl) {
|
2020-12-11 21:32:42 +08:00
|
|
|
super("importScripts chunk loading", RuntimeModule.STAGE_ATTACH);
|
2018-11-28 20:07:40 +08:00
|
|
|
this.runtimeRequirements = runtimeRequirements;
|
2021-05-10 15:33:04 +08:00
|
|
|
this._withCreateScriptUrl = withCreateScriptUrl;
|
2018-11-23 16:37:33 +08:00
|
|
|
}
|
|
|
|
|
2022-02-21 21:23:47 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {Chunk} chunk chunk
|
|
|
|
* @returns {string} generated code
|
|
|
|
*/
|
|
|
|
_generateBaseUri(chunk) {
|
|
|
|
const options = chunk.getEntryOptions();
|
|
|
|
if (options && options.baseUri) {
|
2022-02-22 16:54:35 +08:00
|
|
|
return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`;
|
2022-02-21 21:23:47 +08:00
|
|
|
}
|
2023-06-12 22:21:21 +08:00
|
|
|
const compilation = /** @type {Compilation} */ (this.compilation);
|
|
|
|
const outputName = compilation.getPath(
|
|
|
|
getChunkFilenameTemplate(chunk, compilation.outputOptions),
|
2022-02-21 21:23:47 +08:00
|
|
|
{
|
|
|
|
chunk,
|
|
|
|
contentHashType: "javascript"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const rootOutputDir = getUndoPath(
|
|
|
|
outputName,
|
2023-06-12 22:21:21 +08:00
|
|
|
/** @type {string} */ (compilation.outputOptions.path),
|
2022-02-21 21:23:47 +08:00
|
|
|
false
|
|
|
|
);
|
|
|
|
return `${RuntimeGlobals.baseURI} = self.location + ${JSON.stringify(
|
|
|
|
rootOutputDir ? "/../" + rootOutputDir : ""
|
|
|
|
)};`;
|
|
|
|
}
|
|
|
|
|
2018-11-23 16:37:33 +08:00
|
|
|
/**
|
2023-06-17 01:13:03 +08:00
|
|
|
* @returns {string | null} runtime code
|
2018-11-23 16:37:33 +08:00
|
|
|
*/
|
|
|
|
generate() {
|
2023-06-12 22:21:21 +08:00
|
|
|
const compilation = /** @type {Compilation} */ (this.compilation);
|
2018-11-23 16:37:33 +08:00
|
|
|
const fn = RuntimeGlobals.ensureChunkHandlers;
|
2020-08-29 04:39:07 +08:00
|
|
|
const withBaseURI = this.runtimeRequirements.has(RuntimeGlobals.baseURI);
|
2018-11-28 20:07:40 +08:00
|
|
|
const withLoading = this.runtimeRequirements.has(
|
|
|
|
RuntimeGlobals.ensureChunkHandlers
|
|
|
|
);
|
|
|
|
const withHmr = this.runtimeRequirements.has(
|
|
|
|
RuntimeGlobals.hmrDownloadUpdateHandlers
|
|
|
|
);
|
|
|
|
const withHmrManifest = this.runtimeRequirements.has(
|
|
|
|
RuntimeGlobals.hmrDownloadManifest
|
|
|
|
);
|
2023-06-12 22:21:21 +08:00
|
|
|
const globalObject = compilation.runtimeTemplate.globalObject;
|
2020-08-28 07:44:56 +08:00
|
|
|
const chunkLoadingGlobalExpr = `${globalObject}[${JSON.stringify(
|
2023-06-12 22:21:21 +08:00
|
|
|
compilation.outputOptions.chunkLoadingGlobal
|
2020-08-28 07:44:56 +08:00
|
|
|
)}]`;
|
2023-06-12 22:21:21 +08:00
|
|
|
const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
|
|
|
|
const chunk = /** @type {Chunk} */ (this.chunk);
|
2021-03-12 01:32:32 +08:00
|
|
|
const hasJsMatcher = compileBooleanMatcher(
|
|
|
|
chunkGraph.getChunkConditionMap(chunk, chunkHasJs)
|
|
|
|
);
|
2021-12-03 23:23:09 +08:00
|
|
|
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
|
2021-03-12 01:32:32 +08:00
|
|
|
|
2021-08-16 15:43:50 +08:00
|
|
|
const stateExpression = withHmr
|
|
|
|
? `${RuntimeGlobals.hmrRuntimeStatePrefix}_importScripts`
|
|
|
|
: undefined;
|
2023-06-12 22:21:21 +08:00
|
|
|
const runtimeTemplate = compilation.runtimeTemplate;
|
|
|
|
const { _withCreateScriptUrl: withCreateScriptUrl } = this;
|
2021-08-16 15:43:50 +08:00
|
|
|
|
2018-11-23 16:37:33 +08:00
|
|
|
return Template.asString([
|
2022-02-21 21:23:47 +08:00
|
|
|
withBaseURI ? this._generateBaseUri(chunk) : "// no baseURI",
|
2020-08-29 04:39:07 +08:00
|
|
|
"",
|
2018-11-23 16:37:33 +08:00
|
|
|
"// object to store loaded chunks",
|
|
|
|
'// "1" means "already loaded"',
|
2021-08-16 15:43:50 +08:00
|
|
|
`var installedChunks = ${
|
|
|
|
stateExpression ? `${stateExpression} = ${stateExpression} || ` : ""
|
|
|
|
}{`,
|
2018-11-23 16:37:33 +08:00
|
|
|
Template.indent(
|
2021-03-15 02:54:34 +08:00
|
|
|
Array.from(initialChunkIds, id => `${JSON.stringify(id)}: 1`).join(
|
|
|
|
",\n"
|
|
|
|
)
|
2018-11-23 16:37:33 +08:00
|
|
|
),
|
|
|
|
"};",
|
|
|
|
"",
|
2018-11-28 20:07:40 +08:00
|
|
|
withLoading
|
|
|
|
? Template.asString([
|
|
|
|
"// importScripts chunk loading",
|
2021-03-12 01:32:32 +08:00
|
|
|
`var installChunk = ${runtimeTemplate.basicFunction("data", [
|
|
|
|
runtimeTemplate.destructureArray(
|
|
|
|
["chunkIds", "moreModules", "runtime"],
|
|
|
|
"data"
|
|
|
|
),
|
|
|
|
"for(var moduleId in moreModules) {",
|
2018-11-28 20:07:40 +08:00
|
|
|
Template.indent([
|
2021-03-12 01:32:32 +08:00
|
|
|
`if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
|
|
|
|
Template.indent(
|
|
|
|
`${RuntimeGlobals.moduleFactories}[moduleId] = moreModules[moduleId];`
|
|
|
|
),
|
|
|
|
"}"
|
2018-11-28 20:07:40 +08:00
|
|
|
]),
|
2021-03-12 01:32:32 +08:00
|
|
|
"}",
|
2023-05-19 23:55:47 +08:00
|
|
|
`if(runtime) runtime(${RuntimeGlobals.require});`,
|
2021-03-12 01:32:32 +08:00
|
|
|
"while(chunkIds.length)",
|
|
|
|
Template.indent("installedChunks[chunkIds.pop()] = 1;"),
|
|
|
|
"parentChunkLoadingFunction(data);"
|
|
|
|
])};`
|
2024-01-14 09:41:34 +08:00
|
|
|
])
|
2021-03-12 01:32:32 +08:00
|
|
|
: "// no chunk install function needed",
|
|
|
|
withLoading
|
|
|
|
? Template.asString([
|
|
|
|
`${fn}.i = ${runtimeTemplate.basicFunction(
|
|
|
|
"chunkId, promises",
|
|
|
|
hasJsMatcher !== false
|
|
|
|
? [
|
|
|
|
'// "1" is the signal for "already loaded"',
|
|
|
|
"if(!installedChunks[chunkId]) {",
|
|
|
|
Template.indent([
|
|
|
|
hasJsMatcher === true
|
|
|
|
? "if(true) { // all chunks have JS"
|
|
|
|
: `if(${hasJsMatcher("chunkId")}) {`,
|
|
|
|
Template.indent(
|
2021-05-10 15:33:04 +08:00
|
|
|
`importScripts(${
|
|
|
|
withCreateScriptUrl
|
|
|
|
? `${RuntimeGlobals.createScriptUrl}(${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId))`
|
|
|
|
: `${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkScriptFilename}(chunkId)`
|
|
|
|
});`
|
2021-03-12 01:32:32 +08:00
|
|
|
),
|
|
|
|
"}"
|
|
|
|
]),
|
|
|
|
"}"
|
2024-01-14 09:41:34 +08:00
|
|
|
]
|
2021-03-12 01:32:32 +08:00
|
|
|
: "installedChunks[chunkId] = 1;"
|
|
|
|
)};`,
|
2020-08-28 07:44:56 +08:00
|
|
|
"",
|
|
|
|
`var chunkLoadingGlobal = ${chunkLoadingGlobalExpr} = ${chunkLoadingGlobalExpr} || [];`,
|
|
|
|
"var parentChunkLoadingFunction = chunkLoadingGlobal.push.bind(chunkLoadingGlobal);",
|
2021-03-12 01:32:32 +08:00
|
|
|
"chunkLoadingGlobal.push = installChunk;"
|
2024-01-14 09:41:34 +08:00
|
|
|
])
|
2018-11-28 20:07:40 +08:00
|
|
|
: "// no chunk loading",
|
2018-11-23 16:37:33 +08:00
|
|
|
"",
|
2018-11-28 20:07:40 +08:00
|
|
|
withHmr
|
|
|
|
? Template.asString([
|
|
|
|
"function loadUpdateChunk(chunkId, updatedModulesList) {",
|
|
|
|
Template.indent([
|
|
|
|
"var success = false;",
|
2019-08-27 02:21:07 +08:00
|
|
|
`${globalObject}[${JSON.stringify(
|
2023-06-12 22:21:21 +08:00
|
|
|
compilation.outputOptions.hotUpdateGlobal
|
2020-08-25 16:07:16 +08:00
|
|
|
)}] = ${runtimeTemplate.basicFunction("_, moreModules, runtime", [
|
2018-11-29 21:42:16 +08:00
|
|
|
"for(var moduleId in moreModules) {",
|
2018-11-28 20:07:40 +08:00
|
|
|
Template.indent([
|
2019-12-02 22:59:37 +08:00
|
|
|
`if(${RuntimeGlobals.hasOwnProperty}(moreModules, moduleId)) {`,
|
2018-11-28 20:07:40 +08:00
|
|
|
Template.indent([
|
|
|
|
"currentUpdate[moduleId] = moreModules[moduleId];",
|
|
|
|
"if(updatedModulesList) updatedModulesList.push(moduleId);"
|
|
|
|
]),
|
|
|
|
"}"
|
|
|
|
]),
|
|
|
|
"}",
|
2018-11-29 21:13:02 +08:00
|
|
|
"if(runtime) currentUpdateRuntime.push(runtime);",
|
2018-11-28 20:07:40 +08:00
|
|
|
"success = true;"
|
2020-08-25 16:07:16 +08:00
|
|
|
])};`,
|
2018-11-28 20:07:40 +08:00
|
|
|
"// start update chunk loading",
|
2021-05-10 15:33:04 +08:00
|
|
|
`importScripts(${
|
|
|
|
withCreateScriptUrl
|
|
|
|
? `${RuntimeGlobals.createScriptUrl}(${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId))`
|
|
|
|
: `${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkUpdateScriptFilename}(chunkId)`
|
|
|
|
});`,
|
2018-11-28 20:07:40 +08:00
|
|
|
'if(!success) throw new Error("Loading update chunk failed for unknown reason");'
|
|
|
|
]),
|
|
|
|
"}",
|
|
|
|
"",
|
2020-04-15 16:51:03 +08:00
|
|
|
Template.getFunctionContent(
|
|
|
|
require("../hmr/JavascriptHotModuleReplacement.runtime.js")
|
|
|
|
)
|
2023-07-03 17:57:21 +08:00
|
|
|
.replace(/\$key\$/g, "importScripts")
|
2020-04-15 16:51:03 +08:00
|
|
|
.replace(/\$installedChunks\$/g, "installedChunks")
|
|
|
|
.replace(/\$loadUpdateChunk\$/g, "loadUpdateChunk")
|
|
|
|
.replace(/\$moduleCache\$/g, RuntimeGlobals.moduleCache)
|
|
|
|
.replace(/\$moduleFactories\$/g, RuntimeGlobals.moduleFactories)
|
|
|
|
.replace(
|
|
|
|
/\$ensureChunkHandlers\$/g,
|
|
|
|
RuntimeGlobals.ensureChunkHandlers
|
|
|
|
)
|
|
|
|
.replace(/\$hasOwnProperty\$/g, RuntimeGlobals.hasOwnProperty)
|
|
|
|
.replace(/\$hmrModuleData\$/g, RuntimeGlobals.hmrModuleData)
|
|
|
|
.replace(
|
|
|
|
/\$hmrDownloadUpdateHandlers\$/g,
|
|
|
|
RuntimeGlobals.hmrDownloadUpdateHandlers
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/\$hmrInvalidateModuleHandlers\$/g,
|
|
|
|
RuntimeGlobals.hmrInvalidateModuleHandlers
|
|
|
|
)
|
2024-01-14 09:41:34 +08:00
|
|
|
])
|
2018-11-28 20:07:40 +08:00
|
|
|
: "// no HMR",
|
|
|
|
"",
|
|
|
|
withHmrManifest
|
|
|
|
? Template.asString([
|
2020-08-25 16:07:16 +08:00
|
|
|
`${
|
|
|
|
RuntimeGlobals.hmrDownloadManifest
|
|
|
|
} = ${runtimeTemplate.basicFunction("", [
|
2018-11-28 20:07:40 +08:00
|
|
|
'if (typeof fetch === "undefined") throw new Error("No browser support: need fetch API");',
|
2020-08-25 16:07:16 +08:00
|
|
|
`return fetch(${RuntimeGlobals.publicPath} + ${
|
|
|
|
RuntimeGlobals.getUpdateManifestFilename
|
|
|
|
}()).then(${runtimeTemplate.basicFunction("response", [
|
2018-11-28 20:07:40 +08:00
|
|
|
"if(response.status === 404) return; // no update available",
|
|
|
|
'if(!response.ok) throw new Error("Failed to fetch update manifest " + response.statusText);',
|
|
|
|
"return response.json();"
|
2020-08-25 16:07:16 +08:00
|
|
|
])});`
|
|
|
|
])};`
|
2024-01-14 09:41:34 +08:00
|
|
|
])
|
2018-11-28 20:07:40 +08:00
|
|
|
: "// no HMR manifest"
|
2018-11-23 16:37:33 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ImportScriptsChunkLoadingRuntimeModule;
|