Skip to content

Commit

Permalink
Handle specifier as default with enableLegacyBabel5ModuleInterop (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jul 11, 2023
1 parent 6854479 commit 631f64c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
69 changes: 39 additions & 30 deletions src/transformers/CJSImportTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,35 +292,9 @@ export default class CJSImportTransformer extends Transformer {
}
this.processExportDefault();
return true;
}
this.hadNamedExport = true;
if (
this.tokens.matches2(tt._export, tt._var) ||
this.tokens.matches2(tt._export, tt._let) ||
this.tokens.matches2(tt._export, tt._const)
) {
this.processExportVar();
return true;
} else if (
this.tokens.matches2(tt._export, tt._function) ||
// export async function
this.tokens.matches3(tt._export, tt.name, tt._function)
) {
this.processExportFunction();
return true;
} else if (
this.tokens.matches2(tt._export, tt._class) ||
this.tokens.matches3(tt._export, tt._abstract, tt._class) ||
this.tokens.matches2(tt._export, tt.at)
) {
this.processExportClass();
return true;
} else if (this.tokens.matches2(tt._export, tt.braceL)) {
this.processExportBindings();
return true;
} else if (this.tokens.matches2(tt._export, tt.star)) {
this.processExportStar();
return true;
} else if (
this.tokens.matches2(tt._export, tt.name) &&
this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, ContextualKeyword._type)
Expand Down Expand Up @@ -357,6 +331,32 @@ export default class CJSImportTransformer extends Transformer {
removeMaybeImportAttributes(this.tokens);
}
return true;
}
this.hadNamedExport = true;
if (
this.tokens.matches2(tt._export, tt._var) ||
this.tokens.matches2(tt._export, tt._let) ||
this.tokens.matches2(tt._export, tt._const)
) {
this.processExportVar();
return true;
} else if (
this.tokens.matches2(tt._export, tt._function) ||
// export async function
this.tokens.matches3(tt._export, tt.name, tt._function)
) {
this.processExportFunction();
return true;
} else if (
this.tokens.matches2(tt._export, tt._class) ||
this.tokens.matches3(tt._export, tt._abstract, tt._class) ||
this.tokens.matches2(tt._export, tt.at)
) {
this.processExportClass();
return true;
} else if (this.tokens.matches2(tt._export, tt.star)) {
this.processExportStar();
return true;
} else {
throw new Error("Unrecognized export syntax.");
}
Expand Down Expand Up @@ -798,14 +798,23 @@ export default class CJSImportTransformer extends Transformer {
}

const specifierInfo = getImportExportSpecifierInfo(this.tokens);

while (this.tokens.currentIndex() < specifierInfo.endIndex) {
this.tokens.removeToken();
}
if (!specifierInfo.isType && !this.shouldElideExportedIdentifier(specifierInfo.leftName)) {
const localName = specifierInfo.leftName;

if (!specifierInfo.isType) {
const exportedName = specifierInfo.rightName;
const newLocalName = this.importProcessor.getIdentifierReplacement(localName);
exportStatements.push(`exports.${exportedName} = ${newLocalName || localName};`);
if (exportedName === "default") {
this.hadDefaultExport = true;
} else {
this.hadNamedExport = true;
}
if (!this.shouldElideExportedIdentifier(specifierInfo.leftName)) {
const localName = specifierInfo.leftName;
const newLocalName = this.importProcessor.getIdentifierReplacement(localName);
exportStatements.push(`exports.${exportedName} = ${newLocalName || localName};`);
}
}

if (this.tokens.matches1(tt.braceR)) {
Expand Down
18 changes: 18 additions & 0 deletions test/imports-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,24 @@ module.exports = exports.default;
`,
{transforms: ["imports"], enableLegacyBabel5ModuleInterop: true},
);

assertResult(
`
export { x as default } from './foo'
export {}
export type A = string
export { type A as B }
`,
`"use strict";${ESMODULE_PREFIX}${CREATE_NAMED_EXPORT_FROM_PREFIX}
var _foo = require('./foo'); _createNamedExportFrom(_foo, 'default', 'x');
module.exports = exports.default;
`,
{transforms: ["imports", "typescript"], enableLegacyBabel5ModuleInterop: true},
);
});

it("does not add module exports suffix when there is a named export", () => {
Expand Down

0 comments on commit 631f64c

Please sign in to comment.