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

Always respect synthetic namespaces in namespace reexports #3894

Merged
merged 2 commits into from Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;