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.

Publish as modules (instead of compiling to cjs)

See original GitHub issue

Progress 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:closed
  • Created 3 years ago
  • Reactions:38
  • Comments:22 (21 by maintainers)

github_iconTop GitHub Comments

19reactions
nicolo-ribaudocommented, Feb 12, 2021

Update

We are thinking about releasing Babel 8 as an ESM-only package, for the following reasons:

  • starting from Babel 8, every Node.js version that we’ll support will have native support for ES modules;
  • it avoids the pain of managing dual-node packages, where basically everything would still be CJS + manually maintained ESM entrypoints;
  • maintainers of popular packages on npm are starting to ship ESM-only versions of their libraries. For example, Sindre Sorhus is going to re-release all of its packages as ESM-only in 2021 (https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77). Using CJS means that we are stuck on old dependency versions, without the ability to update them.
  • ESM make it impossible to synchronously require() and babel.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 use babel.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, and grunt 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/11081

8reactions
nicolo-ribaudocommented, Oct 7, 2020

I’m working on this, but it won’t happen sooner than Babel 8.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Publish ESM and CJS in a single package - Anthony Fu
A short tutorial of shipping both ESM and CJS dual formats in a single NPM package.
Read more >
Publishing Node modules with TypeScript and ES modules
A version that uses CommonJS modules (the require code you'll be used to if you work in Node) so older build tools and...
Read more >
Publish Library as an NPM & Github module with CJS & ESM ...
Run npm publish command from the project directory. This will publish your node. js project as npm module to github repository.
Read more >
8 Creating CommonJS-based npm packages via TypeScript
(I deliberately have not published it as a package to npm.) ... All our TypeScript code is compiled to CommonJS modules with the...
Read more >
Rolling (up) a multi module system (esm, cjs...) compatible ...
In this article we will delve into the build chain and build steps necessary to create the artifacts required to publish a library...
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