Skip to content

Commit

Permalink
Always respect synthetic namespaces in namespace reexports (#3894)
Browse files Browse the repository at this point in the history
* Always respect synthetic namespaces in namespace reexports

* Favour explicit named exports over synthetic ones for multiple namespace exports
  • Loading branch information
lukastaegert committed Jan 19, 2021
1 parent 5d33573 commit 0ed8722
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/Module.ts
Expand Up @@ -527,6 +527,7 @@ export default class Module {
}

if (name !== 'default') {
let foundSyntheticDeclaration: SyntheticNamedExportVariable | null = null;
for (const module of this.exportAllModules) {
const declaration = getVariableForExportNameRecursive(
module,
Expand All @@ -535,28 +536,38 @@ export default class Module {
searchedNamesAndModules
);

if (declaration) return declaration;
if (declaration) {
if (!(declaration instanceof SyntheticNamedExportVariable)) {
return declaration;
}
if (!foundSyntheticDeclaration) {
foundSyntheticDeclaration = declaration;
}
}
}
if (foundSyntheticDeclaration) {
return foundSyntheticDeclaration;
}
}

// we don't want to create shims when we are just
// probing export * modules for exports
if (!isExportAllSearch) {
if (this.info.syntheticNamedExports) {
let syntheticExport = this.syntheticExports.get(name);
if (!syntheticExport) {
const syntheticNamespace = this.getSyntheticNamespace();
syntheticExport = new SyntheticNamedExportVariable(
this.astContext,
name,
syntheticNamespace
);
this.syntheticExports.set(name, syntheticExport);
return syntheticExport;
}
if (this.info.syntheticNamedExports) {
let syntheticExport = this.syntheticExports.get(name);
if (!syntheticExport) {
const syntheticNamespace = this.getSyntheticNamespace();
syntheticExport = new SyntheticNamedExportVariable(
this.astContext,
name,
syntheticNamespace
);
this.syntheticExports.set(name, syntheticExport);
return syntheticExport;
}
return syntheticExport;
}

// we don't want to create shims when we are just
// probing export * modules for exports
if (!isExportAllSearch) {
if (this.options.shimMissingExports) {
this.shimMissingExport(name);
return this.exportShimVariable;
Expand Down Expand Up @@ -623,7 +634,7 @@ export default class Module {
this.addModulesToImportDescriptions(this.importDescriptions);
this.addModulesToImportDescriptions(this.reexportDescriptions);
for (const name in this.exports) {
if (name !== 'default') {
if (name !== 'default' && name !== this.info.syntheticNamedExports) {
this.exportsAll[name] = this.id;
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/function/samples/reexport-from-synthetic/_config.js
@@ -0,0 +1,12 @@
module.exports = {
description: 'handles reexporting a synthetic namespace from a non-synthetic module',
options: {
plugins: [
{
transform() {
return { syntheticNamedExports: '__synth' };
}
}
]
}
};
4 changes: 4 additions & 0 deletions test/function/samples/reexport-from-synthetic/main.js
@@ -0,0 +1,4 @@
import { synth, explicit1, explicit2 } from './reexport.js';
assert.strictEqual(synth, 1);
assert.strictEqual(explicit1, 2);
assert.strictEqual(explicit2, 4);
2 changes: 2 additions & 0 deletions test/function/samples/reexport-from-synthetic/reexport.js
@@ -0,0 +1,2 @@
export * from './synthetic1.js';
export * from './synthetic2.js';
2 changes: 2 additions & 0 deletions test/function/samples/reexport-from-synthetic/synthetic1.js
@@ -0,0 +1,2 @@
export const __synth = { synth: 1 };
export const explicit1 = 2;
2 changes: 2 additions & 0 deletions test/function/samples/reexport-from-synthetic/synthetic2.js
@@ -0,0 +1,2 @@
export const __synth = { synth: 3 };
export const explicit2 = 4;

0 comments on commit 0ed8722

Please sign in to comment.