Repository navigation
fix: Preserve module.exports assignments when converting to ESM - #210
Merged
Merged
Conversation
Assigning anything other than an identifier or a function expression to
module.exports removed the statement without replacement, so exports
like `module.exports = { c0, c1 }` disappeared. Object literals that only
reference identifiers now become named exports, and every other
expression becomes a default export.
Named exports had a related bug: `module.exports.foo = bar` produced
`export { bar }`, dropping the name foo, and non-identifier values
produced invalid code like `export { 42 }`. Properties are now exported
under their own name, through a local const alias when the name is a
reserved word or already used in the file. Deeper paths like
`module.exports.a.b` are left untouched instead of being flattened.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #125
module.exports = <expr>only produced an ESM export when the value was an identifier or a function expression. For anything else the statement was removed without a replacement, so exports disappeared silently.module.exports = { c0, one: c1 }becomesexport { c0, c1 as one };requirecalls) becomesexport default <expr>, so no code is dropped.While fixing this I found a related bug in the
module.exports.foo = …path, which always emittedexport { <right side> }:module.exports.foo = barexportedbarinstead offoo. It now emitsexport { bar as foo }.export { 42 }. They now emitexport const answer = 42;class,default, …) or already used in the file, the value goes through a local alias:const _answer = …; export { _answer as answer };module.exports.a.b = 1used to be flattened intoexport { 1 }. They are now left untouched.The "already used" check is conservative. It treats any identifier with the same text in the file as taken, so some exports get an alias they don't strictly need. The output is valid either way.
Supersedes #166, which only handled identifier-only object literals.