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

Transforming export as with string literal from ESM to CJS with platform: 'node' results in invalid JS #4100

Open
simon-abbott opened this issue Feb 27, 2025 · 0 comments

Comments

@simon-abbott
Copy link

simon-abbott commented Feb 27, 2025

When transforming EMS code to CJS with platform: 'node', using the export as syntax with the as side being a string literal that is not also a valid identifier name results in invalid output.

For example, this is perfectly valid ESM:

const something = "stub";
export default something;
export { something as "foo.bar" };

But the output of transforming this ends with

0 && (module.exports = {
  "foo.bar"
});

Which is not valid syntax.

According to the ECMAScript spec, the right side of an export as statement is allowed to be an arbitrary string literal (an ExportSpecifier can be of the form ModuleExportName or ModuleExportName as ModuleExportName, where ModuleExportName can be either an IdentifierName or a StringLiteral).

This pattern was observed in the wild in v1.0.0 of the async-function package, specifically in the require.mjs file.

Playground reproduction link: https://esbuild.github.io/try/#dAAwLjI1LjAAewogIGZvcm1hdDogJ2NqcycsCiAgcGxhdGZvcm06ICdub2RlJwp9AGNvbnN0IHNvbWV0aGluZyA9ICJzdHViIjsKZXhwb3J0IGRlZmF1bHQgc29tZXRoaW5nOwpleHBvcnQgeyBzb21ldGhpbmcgYXMgImZvby5iYXIiIH07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant