diff --git a/src/Module.ts b/src/Module.ts index 8510b24920d..0509a209ae0 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -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, @@ -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; @@ -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; } } diff --git a/test/function/samples/reexport-from-synthetic/_config.js b/test/function/samples/reexport-from-synthetic/_config.js new file mode 100644 index 00000000000..7a0aa62df7a --- /dev/null +++ b/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' }; + } + } + ] + } +}; diff --git a/test/function/samples/reexport-from-synthetic/main.js b/test/function/samples/reexport-from-synthetic/main.js new file mode 100644 index 00000000000..be276abded9 --- /dev/null +++ b/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); diff --git a/test/function/samples/reexport-from-synthetic/reexport.js b/test/function/samples/reexport-from-synthetic/reexport.js new file mode 100644 index 00000000000..abb3502ee80 --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/reexport.js @@ -0,0 +1,2 @@ +export * from './synthetic1.js'; +export * from './synthetic2.js'; diff --git a/test/function/samples/reexport-from-synthetic/synthetic1.js b/test/function/samples/reexport-from-synthetic/synthetic1.js new file mode 100644 index 00000000000..614026ef6a0 --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/synthetic1.js @@ -0,0 +1,2 @@ +export const __synth = { synth: 1 }; +export const explicit1 = 2; diff --git a/test/function/samples/reexport-from-synthetic/synthetic2.js b/test/function/samples/reexport-from-synthetic/synthetic2.js new file mode 100644 index 00000000000..31e5c1e86af --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/synthetic2.js @@ -0,0 +1,2 @@ +export const __synth = { synth: 3 }; +export const explicit2 = 4;