Publish as modules (instead of compiling to cjs)
See original GitHub issueProgress tracked at https://github.com/babel/babel/projects/16
Feature Request
Is your feature request related to a problem?
Would like to be able to consume babel packages directly as modules (as they are written), instead of just cjs. (In my case, may want to import Babel itself into something like snowpack/vite which tries to only support esm). This would be for anything trying to use babel for tooling like the repl/etc so not a common usecase.
Also good that we dogfood what other packages/libraries are doing, even though we aren’t really a tool for the browser it would be good to attempt to understand the issues with doing all this anyway.
Describe the solution you’d like
⚠️ EDIT: See https://github.com/babel/babel/issues/11701#issuecomment-777118858
Use package exports. When building we change our config to do modules: false
in preset-env. Currently we do "main": "lib/index.js",
for most packages.
Could output a whole folder like dist
or module
instead of lib
or output .mjs
in the same lib
folder.
{
"main": "./lib/main.js",
"module": "./lib/module.mjs",
"exports": {
".": {
"import": "./lib/module.mjs",
"default": "./lib/main.js"
},
"./other": {
"import": "./lib/other.mjs",
"default": "./lib/other.js"
}
}
}
Describe alternatives you’ve considered.
Don’t think there’s an alternative other than consuming the source via github which is difficult because this is a monorepo. We only publish cjs.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:38
- Comments:22 (21 by maintainers)
Update
We are thinking about releasing Babel 8 as an ESM-only package, for the following reasons:
require()
andbabel.transformSync
in CJS. However, it’s already not always possible to safely do it (a plugin/preset or a config could contain an async step, forcing consumers to usebabel.transformAsync
) (*).We currently got feedback from @devongovett that Parcel runs Babel in a Node.js
vm
context to track what files Babel requires internally. If we move to ESM, Parcel would probably be forced to use something like @vercel/nft to get Babel’s internal dependencies AOT rather than at runtime.(*) The
webpack
,rollup
,browserify
,gulp
, andgrunt
integrations already use Babel asynchronously. We still have to figure out how it will work for ESLint. Jest issue: https://github.com/facebook/jest/issues/11081I’m working on this, but it won’t happen sooner than Babel 8.