Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use map for namespace reexports by name #4411

Merged
merged 9 commits into from Feb 22, 2022
34 changes: 16 additions & 18 deletions src/Module.ts
Expand Up @@ -232,13 +232,13 @@ export default class Module {
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 exportShimVariable = new ExportShimVariable(this);
private readonly exports = new Map<string, ExportDescription>();
private declare magicString: MagicString;
private namespaceReexportsByName: Record<
private readonly namespaceReexportsByName = new Map<
string,
[variable: Variable | null, indirectExternal?: boolean]
> = Object.create(null);
>();
private readonly reexportDescriptions = new Map<string, ReexportDescription>();
private relevantDependencies: Set<Module | ExternalModule> | null = null;
private readonly syntheticExports = new Map<string, SyntheticNamedExportVariable>();
Expand Down Expand Up @@ -380,7 +380,8 @@ export default class Module {

getDependenciesToBeIncluded(): Set<Module | ExternalModule> {
if (this.relevantDependencies) return this.relevantDependencies;
const relevantDependencies = new Set<Module | ExternalModule>();

this.relevantDependencies = new Set<Module | ExternalModule>();
const necessaryDependencies = new Set<Module | ExternalModule>();
const alwaysCheckedDependencies = new Set<Module>();
const dependencyVariables = new Set(this.imports);
Expand Down Expand Up @@ -414,19 +415,19 @@ export default class Module {
}
if (!this.options.treeshake || this.info.moduleSideEffects === 'no-treeshake') {
for (const dependency of this.dependencies) {
relevantDependencies.add(dependency);
this.relevantDependencies.add(dependency);
}
} else {
this.addRelevantSideEffectDependencies(
relevantDependencies,
this.relevantDependencies,
necessaryDependencies,
alwaysCheckedDependencies
);
}
for (const dependency of necessaryDependencies) {
relevantDependencies.add(dependency);
this.relevantDependencies.add(dependency);
}
return (this.relevantDependencies = relevantDependencies);
return this.relevantDependencies;
}

getExportNamesByVariable(): Map<Variable, string[]> {
Expand Down Expand Up @@ -463,8 +464,6 @@ export default class Module {
if (this.transitiveReexports) {
return this.transitiveReexports;
}
// to avoid infinite recursion when using circular `export * from X`
this.transitiveReexports = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would very much expect this one to be needed, maybe a test is missing. After all, there is a recursion in line 475 which needs to be broken if there is a cycle. Will need to look into this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a failing test for this one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome! thank you! I'll revert.


const reexports = new Set(this.reexportDescriptions.keys());

Expand Down Expand Up @@ -579,14 +578,13 @@ export default class Module {

if (name !== 'default') {
const foundNamespaceReexport =
name in this.namespaceReexportsByName
? this.namespaceReexportsByName[name]
: this.getVariableFromNamespaceReexports(
name,
importerForSideEffects,
searchedNamesAndModules
);
this.namespaceReexportsByName[name] = foundNamespaceReexport;
this.namespaceReexportsByName.get(name) ??
this.getVariableFromNamespaceReexports(
name,
importerForSideEffects,
searchedNamesAndModules
);
this.namespaceReexportsByName.set(name, foundNamespaceReexport);
if (foundNamespaceReexport[0]) {
return foundNamespaceReexport;
}
Expand Down