targeting CJS and ESM modules
See original GitHub issueQuestion
Hello @wessberg, first of thank you for the wonderful utility, this helped unblock my project to target ESM/CJS with yargs-parser 💯
Here’s what I’m tying to figure out. I’ve created a module that uses tsc
to target ESM, and uses rollup-plugin-ts
to target CJS.
In my index I have:
export default yargsParser
This works well for my compilation to ESM, which maintains the same export, but I end up with the following syntax when using rollup-plugin-ts
:
export { yargsParser as default };
I would rather than the export be:
export = yargsParser;
As I believe this is the best way to keep yargs-parser
’s existing API surface.
Is there a configuration setting that would facilitate this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to Create a Hybrid NPM Module for ESM and CommonJS.
Creating an NPM module from single code base that easily targets both CommonJS and ES modules can be an exercise in frustration. Sometimes ......
Read more >Node Modules at War: Why CommonJS and ES ... - Code Red
ESM and CJS are completely different animals. Superficially, ESM looks very similar to CJS, but their implementations couldn't be more different ...
Read more >CJS vs ESM - Andrea Giammarchi - Medium
The conclusion, here, is that ESM is a more secure module system than CJS and last, but not least, ESM is about syntax...
Read more >johnloy/esm-commonjs-interop-manual - GitHub
CJS and ESM support for default exports and named exports in the same module is fundamentally incompatible. When authoring a package in ESM...
Read more >Rolling (up) a multi module system (esm, cjs...) compatible ...
With the example application written in TypeScript, our first step is to go to the target JavaScript versions. Usually this can either be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@bcoe you can try using the package I’ve recently made for this use case: https://github.com/axtgr/ts-transform-default-export
Adding it in your
rollup.config.js
like this should do the trick:It’s apparently even possible to use this combination to make Rollup produce both CJS and ESM bundles and a single declaration file that is compatible with both of them, possibly solving the problem mentioned by @wessberg. The declaration will have
export default foo; export = foo
, which isn’t super valid, but it seems to provide IntelliSense just fine for both CJS and ESM.@axtgr thank you 😄 I prefer this to my hacky replace logic.