feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -59,7 +59,7 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
])});`,
"var obj = {};",
`obj[webpackQueues] = ${runtimeTemplate.expressionFunction(
`fn(queue)`,
"fn(queue)",
"fn"
)};`,
"return obj;"

View File

@@ -10,6 +10,7 @@ const Template = require("../Template");
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
const { getUndoPath } = require("../util/identifier");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compilation")} Compilation */
class AutoPublicPathRuntimeModule extends RuntimeModule {
@@ -25,7 +26,8 @@ class AutoPublicPathRuntimeModule extends RuntimeModule {
const { scriptType, importMetaName, path } = compilation.outputOptions;
const chunkName = compilation.getPath(
JavascriptModulesPlugin.getChunkFilenameTemplate(
this.chunk,
/** @type {Chunk} */
(this.chunk),
compilation.outputOptions
),
{
@@ -48,8 +50,11 @@ class AutoPublicPathRuntimeModule extends RuntimeModule {
`var document = ${RuntimeGlobals.global}.document;`,
"if (!scriptUrl && document) {",
Template.indent([
`if (document.currentScript)`,
Template.indent(`scriptUrl = document.currentScript.src;`),
// Technically we could use `document.currentScript instanceof window.HTMLScriptElement`,
// but an attacker could try to inject `<script>HTMLScriptElement = HTMLImageElement</script>`
// and use `<img name="currentScript" src="https://attacker.controlled.server/"></img>`
"if (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT')",
Template.indent("scriptUrl = document.currentScript.src;"),
"if (!scriptUrl) {",
Template.indent([
'var scripts = document.getElementsByTagName("script");',

View File

@@ -37,8 +37,8 @@ class CreateFakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
// Note: must be a function (not arrow), because this is used in body!
`${fn} = function(value, mode) {`,
Template.indent([
`if(mode & 1) value = this(value);`,
`if(mode & 8) return value;`,
"if(mode & 1) value = this(value);",
"if(mode & 8) return value;",
"if(typeof value === 'object' && value) {",
Template.indent([
"if((mode & 4) && value.__esModule) return value;",

View File

@@ -25,7 +25,7 @@ class DefinePropertyGettersRuntimeModule extends HelperRuntimeModule {
return Template.asString([
"// define getter functions for harmony exports",
`${fn} = ${runtimeTemplate.basicFunction("exports, definition", [
`for(var key in definition) {`,
"for(var key in definition) {",
Template.indent([
`if(${RuntimeGlobals.hasOwnProperty}(definition, key) && !${RuntimeGlobals.hasOwnProperty}(exports, key)) {`,
Template.indent([

View File

@@ -51,18 +51,17 @@ class EnsureChunkRuntimeModule extends RuntimeModule {
]
)};`
]);
} else {
// There ensureChunk is used somewhere in the tree, so we need an empty requireEnsure
// function. This can happen with multiple entrypoints.
return Template.asString([
"// The chunk loading function for additional chunks",
"// Since all referenced chunks are already included",
"// in this file, this function is empty here.",
`${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.returningFunction(
"Promise.resolve()"
)};`
]);
}
// There ensureChunk is used somewhere in the tree, so we need an empty requireEnsure
// function. This can happen with multiple entrypoints.
return Template.asString([
"// The chunk loading function for additional chunks",
"// Since all referenced chunks are already included",
"// in this file, this function is empty here.",
`${RuntimeGlobals.ensureChunk} = ${runtimeTemplate.returningFunction(
"Promise.resolve()"
)};`
]);
}
}

View File

@@ -13,16 +13,14 @@ const { first } = require("../util/SetHelpers");
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Compilation").AssetInfo} AssetInfo */
/** @typedef {import("../Compilation").PathData} PathData */
/** @typedef {function(PathData, AssetInfo=): string} FilenameFunction */
/** @typedef {import("../TemplatedPathPlugin").TemplatePath} TemplatePath */
class GetChunkFilenameRuntimeModule extends RuntimeModule {
/**
* @param {string} contentType the contentType to use the content hash for
* @param {string} name kind of filename
* @param {string} global function name to be assigned
* @param {function(Chunk): string | FilenameFunction} getFilenameForChunk functor to get the filename or function
* @param {function(Chunk): TemplatePath | false} getFilenameForChunk functor to get the filename or function
* @param {boolean} allChunks when false, only async chunks are included
*/
constructor(contentType, name, global, getFilenameForChunk, allChunks) {
@@ -44,7 +42,7 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
const chunk = /** @type {Chunk} */ (this.chunk);
const { runtimeTemplate } = compilation;
/** @type {Map<string | FilenameFunction, Set<Chunk>>} */
/** @type {Map<string | TemplatePath, Set<Chunk>>} */
const chunkFilenames = new Map();
let maxChunks = 0;
/** @type {string | undefined} */
@@ -74,11 +72,10 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
if (
chunkFilename.length ===
/** @type {string} */ (dynamicFilename).length
/** @type {string} */ (dynamicFilename).length &&
chunkFilename < /** @type {string} */ (dynamicFilename)
) {
if (chunkFilename < /** @type {string} */ (dynamicFilename)) {
return;
}
return;
}
}
maxChunks = set.size;
@@ -122,7 +119,7 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
/**
* @param {Chunk} c the chunk
* @param {string | FilenameFunction} chunkFilename the filename template for the chunk
* @param {string | TemplatePath} chunkFilename the filename template for the chunk
* @returns {void}
*/
const addStaticUrl = (c, chunkFilename) => {
@@ -137,7 +134,7 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
return '" + chunkId + "';
}
const s = JSON.stringify(str);
return s.slice(1, s.length - 1);
return s.slice(1, -1);
};
/**
* @param {string} value string
@@ -231,17 +228,14 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
* @param {function(Chunk): string | number} fn function from chunk to value
* @returns {string} code with static mapping of results of fn for including in quoted string
*/
const mapExpr = fn => {
return `" + ${createMap(fn)} + "`;
};
const mapExpr = fn => `" + ${createMap(fn)} + "`;
/**
* @param {function(Chunk): string | number} fn function from chunk to value
* @returns {function(number): string} function which generates code with static mapping of results of fn for including in quoted string for specific length
*/
const mapExprWithLength = fn => length => {
return `" + ${createMap(c => `${fn(c)}`.slice(0, length))} + "`;
};
const mapExprWithLength = fn => length =>
`" + ${createMap(c => `${fn(c)}`.slice(0, length))} + "`;
const url =
dynamicFilename &&
@@ -250,7 +244,7 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
hashWithLength: length =>
`" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
chunk: {
id: `" + chunkId + "`,
id: '" + chunkId + "',
hash: mapExpr(c => /** @type {string} */ (c.renderedHash)),
hashWithLength: mapExprWithLength(
c => /** @type {string} */ (c.renderedHash)

View File

@@ -113,7 +113,7 @@ class LoadScriptRuntimeModule extends HelperRuntimeModule {
return Template.asString([
"var inProgress = {};",
uniqueName
? `var dataWebpackPrefix = ${JSON.stringify(uniqueName + ":")};`
? `var dataWebpackPrefix = ${JSON.stringify(`${uniqueName}:`)};`
: "// data-webpack is not used as build has no uniqueName",
"// loadScript function to load a script via script tag",
`${fn} = ${runtimeTemplate.basicFunction(
@@ -145,23 +145,22 @@ class LoadScriptRuntimeModule extends HelperRuntimeModule {
]),
"}",
"inProgress[url] = [done];",
"var onScriptComplete = " +
runtimeTemplate.basicFunction(
"prev, event",
Template.asString([
"// avoid mem leaks in IE.",
"script.onerror = script.onload = null;",
"clearTimeout(timeout);",
"var doneFns = inProgress[url];",
"delete inProgress[url];",
"script.parentNode && script.parentNode.removeChild(script);",
`doneFns && doneFns.forEach(${runtimeTemplate.returningFunction(
"fn(event)",
"fn"
)});`,
"if(prev) return prev(event);"
])
),
`var onScriptComplete = ${runtimeTemplate.basicFunction(
"prev, event",
Template.asString([
"// avoid mem leaks in IE.",
"script.onerror = script.onload = null;",
"clearTimeout(timeout);",
"var doneFns = inProgress[url];",
"delete inProgress[url];",
"script.parentNode && script.parentNode.removeChild(script);",
`doneFns && doneFns.forEach(${runtimeTemplate.returningFunction(
"fn(event)",
"fn"
)});`,
"if(prev) return prev(event);"
])
)}`,
`var timeout = setTimeout(onScriptComplete.bind(null, undefined, { type: 'timeout', target: script }), ${loadTimeout});`,
"script.onerror = onScriptComplete.bind(null, script.onerror);",
"script.onload = onScriptComplete.bind(null, script.onload);",

View File

@@ -30,9 +30,7 @@ class StartupChunkDependenciesRuntimeModule extends RuntimeModule {
const chunk = /** @type {Chunk} */ (this.chunk);
const chunkIds = Array.from(
chunkGraph.getChunkEntryDependentChunksIterable(chunk)
).map(chunk => {
return chunk.id;
});
).map(chunk => chunk.id);
const compilation = /** @type {Compilation} */ (this.compilation);
const { runtimeTemplate } = compilation;
return Template.asString([