question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

targeting CJS and ESM modules

See original GitHub issue

Question

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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
axtgrcommented, Aug 16, 2020

@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:

import ts from "@wessberg/rollup-plugin-ts";
import transformDefaultExport from "ts-transform-default-export";

export default {
  ...
  plugins: [
    ts({
      transformers: ({ program }) => ({
        afterDeclarations: transformDefaultExport(program),
      }),
    }),
  ],
};

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.

0reactions
bcoecommented, Aug 18, 2020

@axtgr thank you 😄 I prefer this to my hacky replace logic.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found