Skip to content

fix: allow cjs version of packages to export a default value - #943

Merged
straker merged 3 commits into
developfrom
cjs-interop
Nov 30, 2023
Merged

fix: allow cjs version of packages to export a default value#943
straker merged 3 commits into
developfrom
cjs-interop

Conversation

@straker

@straker straker commented Nov 21, 2023

Copy link
Copy Markdown
Contributor

This adds an esbuild plugin to "fix" the esbuild problem of having export default a changed into module.exports.default = a instead of modules.exports = a. Using the plugin removes the build warnings we get from esbuild when we tried to add module.exports into the code to do this manually.

Closes #948

@straker
straker requested a review from a team as a code owner November 21, 2023 21:07
@straker straker changed the title chore: allow csj to export a default value Nov 21, 2023
Comment on lines +4 to +9
* "Fixes" the esbuild problem of exporting the CJS default export as `module.exports.default`
* instead of `module.exports`. The plugin appends a block of code to the file that takes the
* `.default` module export and re-exports it as `module.exports`. It then takes all named
* exports and re-exports them as part of the `module.exports` under the same name. This also
* gives the benefit of exporting the `.default` module which allows us to support all 3 export
* styles: the default export, `.default` export, and named exports.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue with ESBuild open on this? If not, could you submit one? This feels like it would be a generally useful feature.

WilcoFiers
WilcoFiers previously approved these changes Nov 27, 2023

@WilcoFiers WilcoFiers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpick, up to you.

Comment thread utils/esbuild-plugin-cjs-interop.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants