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

Is there any way to bundle CJS to ESM (or keep the require call) ? #1467

Open
hyrious opened this issue Jul 23, 2021 · 2 comments
Open

Is there any way to bundle CJS to ESM (or keep the require call) ? #1467

hyrious opened this issue Jul 23, 2021 · 2 comments

Comments

@hyrious
Copy link

hyrious commented Jul 23, 2021

When it comes to bundling a cjs module into an esm one with some cjs externals, esbuild will convert require to __require and print useful information at runtime. However, it also prevents other ESM based bundlers (rollup, vite (which is using rollup), webpack) including esbuild itself to bundle the esm one again.

For example, in react-dom, there is require("react"), let's bundle it and externalize react:

export { render } from "react-dom"

> esbuild index.js --bundle --format=esm --external:react

...
var React = __require("react"); // will break other bundlers!
// except **webpack**, because it is based on cjs and will make `require` usable
...

> rollup index.js -e react -p node-resolve -p commonjs

import require$$0 from "react"; // looks ok
...

> tsc --module esnext --target esnext --moduleResolution node

// no, tsc doesn't do bundle
// but if you give it cjs code, it is likely to not change it
// input
var React = require("react");
// output
var React = require("react"); // not break other bundlers
@hyrious hyrious changed the title Is there any plan to transform CJS to ESM? Is there any way to transform CJS to ESM (or keep the require call) ? Jul 23, 2021
@hyrious hyrious changed the title Is there any way to transform CJS to ESM (or keep the require call) ? Is there any way to bundle CJS to ESM (or keep the require call) ? Jul 24, 2021
@hyrious
Copy link
Author

hyrious commented Jan 13, 2022

@zheeeng
Copy link

zheeeng commented Feb 28, 2022

Any official saying on this?

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

2 participants