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

@@ -5,6 +5,7 @@
"use strict";
const ConditionalInitFragment = require("../ConditionalInitFragment");
const Dependency = require("../Dependency");
const { UsageState } = require("../ExportsInfo");
const HarmonyLinkingError = require("../HarmonyLinkingError");
@@ -16,7 +17,11 @@ const { first, combine } = require("../util/SetHelpers");
const makeSerializable = require("../util/makeSerializable");
const propertyAccess = require("../util/propertyAccess");
const { propertyName } = require("../util/propertyName");
const { getRuntimeKey, keyToRuntime } = require("../util/runtime");
const {
getRuntimeKey,
keyToRuntime,
filterRuntime
} = require("../util/runtime");
const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
const HarmonyImportDependency = require("./HarmonyImportDependency");
const processExportInfo = require("./processExportInfo");
@@ -31,6 +36,7 @@ const processExportInfo = require("./processExportInfo");
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../ExportsInfo")} ExportsInfo */
/** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
/** @typedef {import("../ExportsInfo").UsedName} UsedName */
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
@@ -45,6 +51,7 @@ const processExportInfo = require("./processExportInfo");
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
/** @typedef {import("./processExportInfo").ReferencedExports} ReferencedExports */
/** @typedef {"missing"|"unused"|"empty-star"|"reexport-dynamic-default"|"reexport-named-default"|"reexport-namespace-object"|"reexport-fake-namespace-object"|"reexport-undefined"|"normal-reexport"|"dynamic-reexport"} ExportModeType */
@@ -69,6 +76,9 @@ class NormalReexportItem {
}
}
/** @typedef {Set<string>} ExportModeIgnored */
/** @typedef {Set<string>} ExportModeHidden */
class ExportMode {
/**
* @param {ExportModeType} type type of the mode
@@ -88,11 +98,11 @@ class ExportMode {
this.partialNamespaceExportInfo = null;
// for "dynamic-reexport":
/** @type {Set<string> | null} */
/** @type {ExportModeIgnored | null} */
this.ignored = null;
// for "dynamic-reexport" | "empty-star":
/** @type {Set<string> | null} */
/** @type {ExportModeHidden | undefined | null} */
this.hidden = null;
// for "missing":
@@ -105,12 +115,19 @@ class ExportMode {
}
}
/**
* @param {ModuleGraph} moduleGraph module graph
* @param {TODO} dependencies dependencies
* @param {TODO=} additionalDependency additional dependency
* @returns {TODO} result
*/
const determineExportAssignments = (
moduleGraph,
dependencies,
additionalDependency
) => {
const names = new Set();
/** @type {number[]} */
const dependencyIndices = [];
if (additionalDependency) {
@@ -306,7 +323,8 @@ const getMode = (moduleGraph, dep, runtimeKey) => {
exportName,
[exportName],
exportsInfo.getReadOnlyExportInfo(exportName),
checked.has(exportName),
/** @type {Set<string>} */
(checked).has(exportName),
false
)
);
@@ -327,11 +345,17 @@ const getMode = (moduleGraph, dep, runtimeKey) => {
return mode;
};
/** @typedef {string[]} Ids */
/** @typedef {Set<string>} Exports */
/** @typedef {Set<string>} Checked */
/** @typedef {Set<string>} Hidden */
/** @typedef {Set<string>} IgnoredExports */
class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
/**
* @param {string} request the request string
* @param {number} sourceOrder the order in the original source file
* @param {string[]} ids the requested export name of the imported module
* @param {Ids} ids the requested export name of the imported module
* @param {string | null} name the export name of for this module
* @param {Set<string>} activeExports other named exports in the module
* @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency> | null} otherStarExports other star exports in the module before this import
@@ -388,7 +412,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {string[]} the imported id
* @returns {Ids} the imported id
*/
getIds(moduleGraph) {
return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
@@ -396,11 +420,12 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {string[]} ids the imported ids
* @param {Ids} ids the imported ids
* @returns {void}
*/
setIds(moduleGraph, ids) {
moduleGraph.getMeta(this)[idsSymbol] = ids;
/** @type {TODO} */
(moduleGraph.getMeta(this))[idsSymbol] = ids;
}
/**
@@ -421,7 +446,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
* @param {RuntimeSpec} runtime the runtime
* @param {ExportsInfo} exportsInfo exports info about the current module (optional)
* @param {Module} importedModule the imported module (optional)
* @returns {{exports?: Set<string>, checked?: Set<string>, ignoredExports: Set<string>, hidden?: Set<string>}} information
* @returns {{exports?: Exports, checked?: Checked, ignoredExports: IgnoredExports, hidden?: Hidden}} information
*/
getStarReexports(
moduleGraph,
@@ -439,7 +464,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
const ignoredExports = new Set(["default", ...this.activeExports]);
let hiddenExports = undefined;
let hiddenExports;
const otherStarExports =
this._discoverActiveExportsFromOtherStarExports(moduleGraph);
if (otherStarExports !== undefined) {
@@ -457,11 +482,11 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
};
}
/** @type {Set<string>} */
/** @type {Exports} */
const exports = new Set();
/** @type {Set<string>} */
/** @type {Checked} */
const checked = new Set();
/** @type {Set<string> | undefined} */
/** @type {Hidden | undefined} */
const hidden = hiddenExports !== undefined ? new Set() : undefined;
if (noExtraImports) {
@@ -489,7 +514,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
if (hiddenExports !== undefined && hiddenExports.has(name)) {
/** @type {Set<string>} */
/** @type {ExportModeHidden} */
(hidden).add(name);
continue;
}
@@ -543,7 +568,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
case "reexport-named-default": {
if (!mode.partialNamespaceExportInfo)
return Dependency.EXPORTS_OBJECT_REFERENCED;
/** @type {string[][]} */
/** @type {ReferencedExports} */
const referencedExports = [];
processExportInfo(
runtime,
@@ -558,7 +583,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
case "reexport-fake-namespace-object": {
if (!mode.partialNamespaceExportInfo)
return Dependency.EXPORTS_OBJECT_REFERENCED;
/** @type {string[][]} */
/** @type {ReferencedExports} */
const referencedExports = [];
processExportInfo(
runtime,
@@ -574,8 +599,13 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
return Dependency.EXPORTS_OBJECT_REFERENCED;
case "normal-reexport": {
/** @type {ReferencedExports} */
const referencedExports = [];
for (const { ids, exportInfo, hidden } of mode.items) {
for (const {
ids,
exportInfo,
hidden
} of /** @type {NormalReexportItem[]} */ (mode.items)) {
if (hidden) continue;
processExportInfo(runtime, referencedExports, ids, exportInfo, false);
}
@@ -592,13 +622,13 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
* @returns {{ names: string[], namesSlice: number, dependencyIndices: number[], dependencyIndex: number } | undefined} exported names and their origin dependency
*/
_discoverActiveExportsFromOtherStarExports(moduleGraph) {
if (!this.otherStarExports) return undefined;
if (!this.otherStarExports) return;
const i =
"length" in this.otherStarExports
? this.otherStarExports.length
: countIterable(this.otherStarExports);
if (i === 0) return undefined;
if (i === 0) return;
if (this.allStarExports) {
const { names, dependencyIndices } = moduleGraph.cached(
@@ -638,7 +668,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
switch (mode.type) {
case "missing":
return undefined;
return;
case "dynamic-reexport": {
const from =
/** @type {ModuleGraphConnection} */
@@ -648,8 +678,11 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
from,
canMangle: false,
excludeExports: mode.hidden
? combine(mode.ignored, mode.hidden)
: mode.ignored,
? combine(
/** @type {ExportModeIgnored} */ (mode.ignored),
mode.hidden
)
: /** @type {ExportModeIgnored} */ (mode.ignored),
hideExports: mode.hidden,
dependencies: [from.module]
};
@@ -666,33 +699,34 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
/** @type {ModuleGraphConnection} */
(moduleGraph.getConnection(this));
return {
exports: Array.from(mode.items, item => ({
name: item.name,
from,
export: item.ids,
hidden: item.hidden
})),
exports: Array.from(
/** @type {NormalReexportItem[]} */ (mode.items),
item => ({
name: item.name,
from,
export: item.ids,
hidden: item.hidden
})
),
priority: 1,
dependencies: [from.module]
};
}
case "reexport-dynamic-default": {
{
const from =
/** @type {ModuleGraphConnection} */
(moduleGraph.getConnection(this));
return {
exports: [
{
name: /** @type {string} */ (mode.name),
from,
export: ["default"]
}
],
priority: 1,
dependencies: [from.module]
};
}
const from =
/** @type {ModuleGraphConnection} */
(moduleGraph.getConnection(this));
return {
exports: [
{
name: /** @type {string} */ (mode.name),
from,
export: ["default"]
}
],
priority: 1,
dependencies: [from.module]
};
}
case "reexport-undefined":
return {
@@ -825,6 +859,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
const importedModule = moduleGraph.getModule(this);
if (importedModule) {
const exportsInfo = moduleGraph.getExportsInfo(importedModule);
/** @type {Map<string, string[]>} */
const conflicts = new Map();
for (const exportInfo of exportsInfo.orderedExports) {
if (exportInfo.provided !== true) continue;
@@ -841,12 +876,12 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
if (!conflictingDependency) continue;
const target = exportInfo.getTerminalBinding(moduleGraph);
if (!target) continue;
const conflictingModule = moduleGraph.getModule(
conflictingDependency
);
const conflictingModule =
/** @type {Module} */
(moduleGraph.getModule(conflictingDependency));
if (conflictingModule === importedModule) continue;
const conflictingExportInfo = moduleGraph.getExportInfo(
/** @type {Module} */ (conflictingModule),
conflictingModule,
exportInfo.name
);
const conflictingTarget =
@@ -944,7 +979,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
switch (mode.type) {
case "reexport-undefined":
concatenationScope.registerRawExport(
mode.name,
/** @type {NonNullable<ExportMode["name"]>} */ (mode.name),
"/* reexport non-default export from non-harmony */ undefined"
);
}
@@ -1020,7 +1055,9 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
this.getReexportFragment(
module,
"reexport default from dynamic",
moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
moduleGraph
.getExportsInfo(module)
.getUsedName(/** @type {string} */ (mode.name), runtime),
importVar,
null,
runtimeRequirements
@@ -1032,7 +1069,9 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
initFragments.push(
...this.getReexportFakeNamespaceObjectFragments(
module,
moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
moduleGraph
.getExportsInfo(module)
.getUsedName(/** @type {string} */ (mode.name), runtime),
importVar,
mode.fakeType,
runtimeRequirements
@@ -1045,7 +1084,9 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
this.getReexportFragment(
module,
"reexport non-default export from non-harmony",
moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
moduleGraph
.getExportsInfo(module)
.getUsedName(/** @type {string} */ (mode.name), runtime),
"undefined",
"",
runtimeRequirements
@@ -1058,7 +1099,9 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
this.getReexportFragment(
module,
"reexport default export from named module",
moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
moduleGraph
.getExportsInfo(module)
.getUsedName(/** @type {string} */ (mode.name), runtime),
importVar,
"",
runtimeRequirements
@@ -1071,7 +1114,9 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
this.getReexportFragment(
module,
"reexport module object",
moduleGraph.getExportsInfo(module).getUsedName(mode.name, runtime),
moduleGraph
.getExportsInfo(module)
.getUsedName(/** @type {string} */ (mode.name), runtime),
importVar,
"",
runtimeRequirements
@@ -1080,23 +1125,36 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
break;
case "normal-reexport":
for (const { name, ids, checked, hidden } of mode.items) {
for (const {
name,
ids,
checked,
hidden
} of /** @type {NormalReexportItem[]} */ (mode.items)) {
if (hidden) continue;
if (checked) {
const connection = moduleGraph.getConnection(dep);
const key = `harmony reexport (checked) ${importVar} ${name}`;
const runtimeCondition = dep.weak
? false
: connection
? filterRuntime(runtime, r => connection.isTargetActive(r))
: true;
initFragments.push(
new InitFragment(
"/* harmony reexport (checked) */ " +
this.getConditionalReexportStatement(
module,
name,
importVar,
ids,
runtimeRequirements
),
new ConditionalInitFragment(
`/* harmony reexport (checked) */ ${this.getConditionalReexportStatement(
module,
name,
importVar,
ids,
runtimeRequirements
)}`,
moduleGraph.isAsync(importedModule)
? InitFragment.STAGE_ASYNC_HARMONY_IMPORTS
: InitFragment.STAGE_HARMONY_IMPORTS,
dep.sourceOrder
dep.sourceOrder,
key,
runtimeCondition
)
);
} else {
@@ -1118,8 +1176,12 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
case "dynamic-reexport": {
const ignored = mode.hidden
? combine(mode.ignored, mode.hidden)
: mode.ignored;
? combine(
/** @type {ExportModeIgnored} */
(mode.ignored),
mode.hidden
)
: /** @type {ExportModeIgnored} */ (mode.ignored);
const modern =
runtimeTemplate.supportsConst() &&
runtimeTemplate.supportsArrowFunction();
@@ -1132,22 +1194,19 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
// Filter out exports which are defined by other exports
// and filter out default export because it cannot be reexported with *
if (ignored.size > 1) {
content +=
"if(" +
JSON.stringify(Array.from(ignored)) +
".indexOf(__WEBPACK_IMPORT_KEY__) < 0) ";
content += `if(${JSON.stringify(
Array.from(ignored)
)}.indexOf(__WEBPACK_IMPORT_KEY__) < 0) `;
} else if (ignored.size === 1) {
content += `if(__WEBPACK_IMPORT_KEY__ !== ${JSON.stringify(
first(ignored)
)}) `;
}
content += `__WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = `;
if (modern) {
content += `() => ${importVar}[__WEBPACK_IMPORT_KEY__]`;
} else {
content += `function(key) { return ${importVar}[key]; }.bind(0, __WEBPACK_IMPORT_KEY__)`;
}
content += "__WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = ";
content += modern
? `() => ${importVar}[__WEBPACK_IMPORT_KEY__]`
: `function(key) { return ${importVar}[key]; }.bind(0, __WEBPACK_IMPORT_KEY__)`;
runtimeRequirements.add(RuntimeGlobals.exports);
runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
@@ -1170,6 +1229,15 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
}
}
/**
* @param {Module} module the current module
* @param {string} comment comment
* @param {UsedName} key key
* @param {string} name name
* @param {string | string[] | null | false} valueKey value key
* @param {RuntimeRequirements} runtimeRequirements runtime requirements
* @returns {HarmonyExportInitFragment} harmony export init fragment
*/
getReexportFragment(
module,
comment,