From 1d768870c6f5e881fba635530f07d90c0ea71218 Mon Sep 17 00:00:00 2001 From: dnalborczyk Date: Mon, 14 Feb 2022 00:29:57 -0500 Subject: [PATCH] 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 --- src/Module.ts | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/Module.ts b/src/Module.ts index 056217768e5..544b40153cc 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -187,7 +187,6 @@ function getAndExtendSideEffectModules(variable: Variable, module: Module): Set< export default class Module { readonly alternativeReexportModules = new Map(); - ast: Program | null = null; readonly chunkFileNames = new Set(); chunkNames: { isUserDefined: boolean; @@ -201,8 +200,6 @@ export default class Module { readonly dynamicImports: DynamicImport[] = []; excludeFromSourcemap: boolean; execIndex = Infinity; - readonly exportAllSources = new Set(); - readonly exports: { [name: string]: ExportDescription } = Object.create(null); readonly implicitlyLoadedAfter = new Set(); readonly implicitlyLoadedBefore = new Set(); readonly importDescriptions: { [name: string]: ImportDescription } = Object.create(null); @@ -219,7 +216,6 @@ export default class Module { declare originalCode: string; declare originalSourcemap: ExistingDecodedSourceMap | null; preserveSignature: PreserveEntrySignaturesOption; - readonly reexportDescriptions: { [name: string]: ReexportDescription } = Object.create(null); declare resolvedIds: ResolvedIdMap; declare scope: ModuleScope; readonly sideEffectDependenciesByVariable = new Map>(); @@ -229,17 +225,22 @@ export default class Module { usesTopLevelAwait = false; private allExportNames: Set | null = null; + private ast: Program | null = null; private declare astContext: AstContext; private readonly context: string; private declare customTransformCache: boolean; private readonly exportAllModules: (Module | ExternalModule)[] = []; + private readonly exportAllSources = new Set(); private exportNamesByVariable: Map | null = null; private readonly exportShimVariable: ExportShimVariable = new ExportShimVariable(this); + private readonly exports = new Map(); private declare magicString: MagicString; private namespaceReexportsByName: Record< string, [variable: Variable | null, indirectExternal?: boolean] > = Object.create(null); + private readonly reexportDescriptions: { [name: string]: ReexportDescription } = + Object.create(null); private relevantDependencies: Set | null = null; private readonly syntheticExports = new Map(); private syntheticNamespace: Variable | null | undefined = null; @@ -295,7 +296,7 @@ export default class Module { if (!module.ast) { return null; } - return 'default' in module.exports || 'default' in reexportDescriptions; + return module.exports.has('default') || 'default' in reexportDescriptions; }, get hasModuleSideEffects() { warnDeprecation( @@ -360,29 +361,27 @@ export default class Module { if (this.allExportNames) { return this.allExportNames; } - const allExportNames = (this.allExportNames = new Set()); - for (const name of this.getExports()) { - allExportNames.add(name); - } - for (const name of Object.keys(this.reexportDescriptions)) { - allExportNames.add(name); - } + this.allExportNames = new Set([ + ...this.exports.keys(), + ...Object.keys(this.reexportDescriptions) + ]); + for (const module of this.exportAllModules) { if (module instanceof ExternalModule) { - allExportNames.add(`*${module.id}`); + this.allExportNames.add(`*${module.id}`); continue; } for (const name of module.getAllExportNames()) { - if (name !== 'default') allExportNames.add(name); + if (name !== 'default') this.allExportNames.add(name); } } // We do not count the synthetic namespace as a regular export to hide it // from entry signatures and namespace objects if (typeof this.info.syntheticNamedExports === 'string') { - allExportNames.delete(this.info.syntheticNamedExports); + this.allExportNames.delete(this.info.syntheticNamedExports); } - return allExportNames; + return this.allExportNames; } getDependenciesToBeIncluded(): Set { @@ -463,7 +462,7 @@ export default class Module { } getExports(): string[] { - return Object.keys(this.exports); + return Array.from(this.exports.keys()); } getReexports(): string[] { @@ -493,7 +492,7 @@ export default class Module { // only direct exports are counted here, not reexports at all const renderedExports: string[] = []; const removedExports: string[] = []; - for (const exportName in this.exports) { + for (const exportName of this.exports.keys()) { const [variable] = this.getVariableForExportName(exportName); (variable && variable.included ? renderedExports : removedExports).push(exportName); } @@ -565,7 +564,7 @@ export default class Module { return [variable]; } - const exportDeclaration = this.exports[name]; + const exportDeclaration = this.exports.get(name); if (exportDeclaration) { if (exportDeclaration === MISSING_EXPORT_SHIM_DESCRIPTION) { return [this.exportShimVariable]; @@ -642,7 +641,7 @@ export default class Module { this.graph.needsTreeshakingPass = true; } - for (const exportName of this.getExports()) { + for (const exportName of this.exports.keys()) { if (includeNamespaceMembers || exportName !== this.info.syntheticNamedExports) { const variable = this.getVariableForExportName(exportName)[0]!; variable.deoptimizePath(UNKNOWN_PATH); @@ -894,10 +893,10 @@ export default class Module { if (node instanceof ExportDefaultDeclaration) { // export default foo; - this.exports.default = { + this.exports.set('default', { identifier: node.variable.getAssignedVariableName(), localName: 'default' - }; + }); } else if (node instanceof ExportAllDeclaration) { const source = node.source.value; this.sources.add(source); @@ -938,14 +937,14 @@ export default class Module { for (const declarator of declaration.declarations) { for (const localName of extractAssignedNames(declarator.id)) { - this.exports[localName] = { identifier: null, localName }; + this.exports.set(localName, { identifier: null, localName }); } } } else { // export function foo () {} const localName = (declaration.id as Identifier).name; - this.exports[localName] = { identifier: null, localName }; + this.exports.set(localName, { identifier: null, localName }); } } else { // export { foo, bar, baz } @@ -953,7 +952,7 @@ export default class Module { for (const specifier of node.specifiers) { const localName = specifier.local.name; const exportedName = specifier.exported.name; - this.exports[exportedName] = { identifier: null, localName }; + this.exports.set(exportedName, { identifier: null, localName }); } } } @@ -1184,7 +1183,7 @@ export default class Module { exportName: name, message: `Missing export "${name}" has been shimmed in module ${relativeId(this.id)}.` }); - this.exports[name] = MISSING_EXPORT_SHIM_DESCRIPTION; + this.exports.set(name, MISSING_EXPORT_SHIM_DESCRIPTION); } }