Skip to content

Commit

Permalink
refactor: use map for namespace reexports by name (#4411)
Browse files Browse the repository at this point in the history
* refactor: use map for namespace reexports by name

* remove explicit type assignment

* refactor: remove variable, use context property directly

* refactor: remove unnecessary array assignment

* Add test for recursive reexports

* Revert "refactor: remove unnecessary array assignment"

This reverts commit f7434c3.

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
3 people committed Feb 22, 2022
1 parent 2cca505 commit 3106ec9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
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';

0 comments on commit 3106ec9

Please sign in to comment.