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
32 changes: 16 additions & 16 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 @@ -579,14 +580,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
15 changes: 15 additions & 0 deletions test/function/samples/recursive-reexports/_config.js
@@ -0,0 +1,15 @@
const assert = require('assert');
module.exports = {
description: 'handles recursive namespace reexports',
exports(exports) {
assert.deepStrictEqual(exports, { main: 'main', other: 'other' });
},
warnings: [
{
code: 'CIRCULAR_DEPENDENCY',
cycle: ['main.js', 'other.js', 'main.js'],
importer: 'main.js',
message: 'Circular dependency: main.js -> other.js -> main.js'
}
]
};
2 changes: 2 additions & 0 deletions test/function/samples/recursive-reexports/main.js
@@ -0,0 +1,2 @@
export * from './other.js';
export const main = 'main';
2 changes: 2 additions & 0 deletions test/function/samples/recursive-reexports/other.js
@@ -0,0 +1,2 @@
export * from './main.js';
export const other = 'other';