Skip to content

Commit 1d76887

Browse files
refactor: module exports to map (#4405)
* refactor: module exports to map * traverse keys directly * remove var, use instance field directly * simplify, pass values into set constructor * Mark more private properties private Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent f715e3c commit 1d76887

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

src/Module.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ function getAndExtendSideEffectModules(variable: Variable, module: Module): Set<
187187

188188
export default class Module {
189189
readonly alternativeReexportModules = new Map<Variable, Module>();
190-
ast: Program | null = null;
191190
readonly chunkFileNames = new Set<string>();
192191
chunkNames: {
193192
isUserDefined: boolean;
@@ -201,8 +200,6 @@ export default class Module {
201200
readonly dynamicImports: DynamicImport[] = [];
202201
excludeFromSourcemap: boolean;
203202
execIndex = Infinity;
204-
readonly exportAllSources = new Set<string>();
205-
readonly exports: { [name: string]: ExportDescription } = Object.create(null);
206203
readonly implicitlyLoadedAfter = new Set<Module>();
207204
readonly implicitlyLoadedBefore = new Set<Module>();
208205
readonly importDescriptions: { [name: string]: ImportDescription } = Object.create(null);
@@ -219,7 +216,6 @@ export default class Module {
219216
declare originalCode: string;
220217
declare originalSourcemap: ExistingDecodedSourceMap | null;
221218
preserveSignature: PreserveEntrySignaturesOption;
222-
readonly reexportDescriptions: { [name: string]: ReexportDescription } = Object.create(null);
223219
declare resolvedIds: ResolvedIdMap;
224220
declare scope: ModuleScope;
225221
readonly sideEffectDependenciesByVariable = new Map<Variable, Set<Module>>();
@@ -229,17 +225,22 @@ export default class Module {
229225
usesTopLevelAwait = false;
230226

231227
private allExportNames: Set<string> | null = null;
228+
private ast: Program | null = null;
232229
private declare astContext: AstContext;
233230
private readonly context: string;
234231
private declare customTransformCache: boolean;
235232
private readonly exportAllModules: (Module | ExternalModule)[] = [];
233+
private readonly exportAllSources = new Set<string>();
236234
private exportNamesByVariable: Map<Variable, string[]> | null = null;
237235
private readonly exportShimVariable: ExportShimVariable = new ExportShimVariable(this);
236+
private readonly exports = new Map<string, ExportDescription>();
238237
private declare magicString: MagicString;
239238
private namespaceReexportsByName: Record<
240239
string,
241240
[variable: Variable | null, indirectExternal?: boolean]
242241
> = Object.create(null);
242+
private readonly reexportDescriptions: { [name: string]: ReexportDescription } =
243+
Object.create(null);
243244
private relevantDependencies: Set<Module | ExternalModule> | null = null;
244245
private readonly syntheticExports = new Map<string, SyntheticNamedExportVariable>();
245246
private syntheticNamespace: Variable | null | undefined = null;
@@ -295,7 +296,7 @@ export default class Module {
295296
if (!module.ast) {
296297
return null;
297298
}
298-
return 'default' in module.exports || 'default' in reexportDescriptions;
299+
return module.exports.has('default') || 'default' in reexportDescriptions;
299300
},
300301
get hasModuleSideEffects() {
301302
warnDeprecation(
@@ -360,29 +361,27 @@ export default class Module {
360361
if (this.allExportNames) {
361362
return this.allExportNames;
362363
}
363-
const allExportNames = (this.allExportNames = new Set<string>());
364-
for (const name of this.getExports()) {
365-
allExportNames.add(name);
366-
}
367-
for (const name of Object.keys(this.reexportDescriptions)) {
368-
allExportNames.add(name);
369-
}
364+
this.allExportNames = new Set([
365+
...this.exports.keys(),
366+
...Object.keys(this.reexportDescriptions)
367+
]);
368+
370369
for (const module of this.exportAllModules) {
371370
if (module instanceof ExternalModule) {
372-
allExportNames.add(`*${module.id}`);
371+
this.allExportNames.add(`*${module.id}`);
373372
continue;
374373
}
375374

376375
for (const name of module.getAllExportNames()) {
377-
if (name !== 'default') allExportNames.add(name);
376+
if (name !== 'default') this.allExportNames.add(name);
378377
}
379378
}
380379
// We do not count the synthetic namespace as a regular export to hide it
381380
// from entry signatures and namespace objects
382381
if (typeof this.info.syntheticNamedExports === 'string') {
383-
allExportNames.delete(this.info.syntheticNamedExports);
382+
this.allExportNames.delete(this.info.syntheticNamedExports);
384383
}
385-
return allExportNames;
384+
return this.allExportNames;
386385
}
387386

388387
getDependenciesToBeIncluded(): Set<Module | ExternalModule> {
@@ -463,7 +462,7 @@ export default class Module {
463462
}
464463

465464
getExports(): string[] {
466-
return Object.keys(this.exports);
465+
return Array.from(this.exports.keys());
467466
}
468467

469468
getReexports(): string[] {
@@ -493,7 +492,7 @@ export default class Module {
493492
// only direct exports are counted here, not reexports at all
494493
const renderedExports: string[] = [];
495494
const removedExports: string[] = [];
496-
for (const exportName in this.exports) {
495+
for (const exportName of this.exports.keys()) {
497496
const [variable] = this.getVariableForExportName(exportName);
498497
(variable && variable.included ? renderedExports : removedExports).push(exportName);
499498
}
@@ -565,7 +564,7 @@ export default class Module {
565564
return [variable];
566565
}
567566

568-
const exportDeclaration = this.exports[name];
567+
const exportDeclaration = this.exports.get(name);
569568
if (exportDeclaration) {
570569
if (exportDeclaration === MISSING_EXPORT_SHIM_DESCRIPTION) {
571570
return [this.exportShimVariable];
@@ -642,7 +641,7 @@ export default class Module {
642641
this.graph.needsTreeshakingPass = true;
643642
}
644643

645-
for (const exportName of this.getExports()) {
644+
for (const exportName of this.exports.keys()) {
646645
if (includeNamespaceMembers || exportName !== this.info.syntheticNamedExports) {
647646
const variable = this.getVariableForExportName(exportName)[0]!;
648647
variable.deoptimizePath(UNKNOWN_PATH);
@@ -894,10 +893,10 @@ export default class Module {
894893
if (node instanceof ExportDefaultDeclaration) {
895894
// export default foo;
896895

897-
this.exports.default = {
896+
this.exports.set('default', {
898897
identifier: node.variable.getAssignedVariableName(),
899898
localName: 'default'
900-
};
899+
});
901900
} else if (node instanceof ExportAllDeclaration) {
902901
const source = node.source.value;
903902
this.sources.add(source);
@@ -938,22 +937,22 @@ export default class Module {
938937

939938
for (const declarator of declaration.declarations) {
940939
for (const localName of extractAssignedNames(declarator.id)) {
941-
this.exports[localName] = { identifier: null, localName };
940+
this.exports.set(localName, { identifier: null, localName });
942941
}
943942
}
944943
} else {
945944
// export function foo () {}
946945

947946
const localName = (declaration.id as Identifier).name;
948-
this.exports[localName] = { identifier: null, localName };
947+
this.exports.set(localName, { identifier: null, localName });
949948
}
950949
} else {
951950
// export { foo, bar, baz }
952951

953952
for (const specifier of node.specifiers) {
954953
const localName = specifier.local.name;
955954
const exportedName = specifier.exported.name;
956-
this.exports[exportedName] = { identifier: null, localName };
955+
this.exports.set(exportedName, { identifier: null, localName });
957956
}
958957
}
959958
}
@@ -1184,7 +1183,7 @@ export default class Module {
11841183
exportName: name,
11851184
message: `Missing export "${name}" has been shimmed in module ${relativeId(this.id)}.`
11861185
});
1187-
this.exports[name] = MISSING_EXPORT_SHIM_DESCRIPTION;
1186+
this.exports.set(name, MISSING_EXPORT_SHIM_DESCRIPTION);
11881187
}
11891188
}
11901189

0 commit comments

Comments
 (0)