Skip to content

Commit

Permalink
refactor: module exports to map (#4405)
Browse files Browse the repository at this point in the history
* 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 <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Feb 14, 2022
1 parent f715e3c commit 1d76887
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/Module.ts
Expand Up @@ -187,7 +187,6 @@ function getAndExtendSideEffectModules(variable: Variable, module: Module): Set<

export default class Module {
readonly alternativeReexportModules = new Map<Variable, Module>();
ast: Program | null = null;
readonly chunkFileNames = new Set<string>();
chunkNames: {
isUserDefined: boolean;
Expand All @@ -201,8 +200,6 @@ export default class Module {
readonly dynamicImports: DynamicImport[] = [];
excludeFromSourcemap: boolean;
execIndex = Infinity;
readonly exportAllSources = new Set<string>();
readonly exports: { [name: string]: ExportDescription } = Object.create(null);
readonly implicitlyLoadedAfter = new Set<Module>();
readonly implicitlyLoadedBefore = new Set<Module>();
readonly importDescriptions: { [name: string]: ImportDescription } = Object.create(null);
Expand All @@ -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<Variable, Set<Module>>();
Expand All @@ -229,17 +225,22 @@ export default class Module {
usesTopLevelAwait = false;

private allExportNames: Set<string> | 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<string>();
private exportNamesByVariable: Map<Variable, string[]> | null = null;
private readonly exportShimVariable: ExportShimVariable = new ExportShimVariable(this);
private readonly exports = new Map<string, ExportDescription>();
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<Module | ExternalModule> | null = null;
private readonly syntheticExports = new Map<string, SyntheticNamedExportVariable>();
private syntheticNamespace: Variable | null | undefined = null;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -360,29 +361,27 @@ export default class Module {
if (this.allExportNames) {
return this.allExportNames;
}
const allExportNames = (this.allExportNames = new Set<string>());
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<Module | ExternalModule> {
Expand Down Expand Up @@ -463,7 +462,7 @@ export default class Module {
}

getExports(): string[] {
return Object.keys(this.exports);
return Array.from(this.exports.keys());
}

getReexports(): string[] {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -938,22 +937,22 @@ 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 }

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 });
}
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 1d76887

Please sign in to comment.