Skip to content

Conversation

@vovkasm
Copy link
Contributor

@vovkasm vovkasm commented Jan 29, 2025

Q                       A
Fixed Issues? See #15269
Patch: Bug Fix? 👍
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Not applicable
Documentation PR Link
Any Dependency Changes?
License MIT

This should fix #15269. Import of CJS built @babel/generator module from esm node.js scripts. Now one can use named import.

This should fix babel#15269. Import of CJS built @babel/generator module from esm node.js scripts.
Now one can use named import.
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58647

@JLHwung JLHwung added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: generator labels Feb 5, 2025
Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good to me. For reviewers: currently the Babel 7 CJS build only contains exports.default = generate;: https://unpkg.com/@babel/[email protected]/lib/index.js so require("@babel/generator") only provides the namespace access.

@JLHwung
Copy link
Contributor

JLHwung commented Feb 5, 2025

I don't think this PR actually fixes #15269, this PR adds support for import { generate } from "@babel/generator" given ESM-to-CJS transforms applied. The pure ESM import, either the default one or the named one in this PR, would still be blocked until Babel 8.

@vovkasm
Copy link
Contributor Author

vovkasm commented Feb 5, 2025

@JLHwung But it actually works, and really fix mentioned issue. I mean, that at least we can import and use @babel/generator from .mjs files.
Quick experiment:

$ node --version
v22.13.1
$ npm install @babel/generator

Create test.mjs

import {generate} from "@babel/generator";
import parser from "@babel/parser";

const code = `function square(n) {
  return n * n;
}`;

const ast = parser.parse(code);

const output = generate(ast, {}, code);
console.log(output);

Next, I patched file `node_modules/@babel/generator/lib/index.js

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = generate;

exports.generate = generate;  // <--- Inserted this line
var _sourceMap = require("./source-map.js");
var _printer = require("./printer.js");

And after

$ node test.mjs
{
  code: 'function square(n) {\n  return n * n;\n}',
  decodedMap: undefined,
  __mergedMap: [Getter],
  map: [Getter/Setter],
  rawMappings: [Getter/Setter]
}

@JLHwung
Copy link
Contributor

JLHwung commented Feb 6, 2025

Thank you for your experiment. The issue #15269 is that

import generate from "@babel/generator"
generate(bla)

does not work. It will never work because generate is a namespace as Babel 7 is CJS. One would have to access from the default binding:

import generate from "@babel/generator"
generate.default(bla)

When you use Babel 8 (ESM), the first usage will work, but Babel 8 is still in alpha.

In this PR, we added a new generate export so that the named import works

import { generate } from "@babel/generator"
generate(bla)

and it should work for both Babel 7 CJS and Babel 8 ESM. But this PR does not make the first usage work, this is why I stated that this PR does not fix #15269 but rather offered a more convenient API access so that users don't have to deal with CJS/ESM import interop.

@vovkasm
Copy link
Contributor Author

vovkasm commented Feb 6, 2025

Agreed! Thanks for the detailed explanation. Technically, it's not a fix, it's a workaround. 🤝

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable. Could you also submit a PR to babel/website updating the docs?

@vovkasm
Copy link
Contributor Author

vovkasm commented Feb 6, 2025

@nicolo-ribaudo Of course, I will be happy to update the documentation! Can I do PR in babel/website in advance with this PR or better wait until this PR is merged?

@nicolo-ribaudo
Copy link
Member

This will take a while (1 month?) to be merged because we are going to wait for the next minor release.

Please go ahead, I'll take care of merging everything at the right time :)

@nicolo-ribaudo nicolo-ribaudo added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Feb 6, 2025
@nicolo-ribaudo nicolo-ribaudo added this to the v7.26.0 milestone Feb 6, 2025
vovkasm added a commit to vovkasm/babel-website that referenced this pull request Feb 7, 2025
Named export was introduced in babel/babel#17100 and allows to import
generator from node.js esm scripts from cjs built generator.
@nicolo-ribaudo nicolo-ribaudo modified the milestones: v7.26.0, v7.27.0 Feb 7, 2025
@nicolo-ribaudo nicolo-ribaudo merged commit 4cb43c9 into babel:main Mar 21, 2025
55 checks passed
nicolo-ribaudo added a commit to babel/website that referenced this pull request Mar 21, 2025
* Improve generator docs and use named export

Named export was introduced in babel/babel#17100 and allows to import
generator from node.js esm scripts from cjs built generator.

* Apply suggestions from code review

---------

Co-authored-by: Nicolò Ribaudo <[email protected]>
@seancheung
Copy link

The @types/babel__generator package is still outdated. How do you guys use it in a typescript project?
image

@JLHwung
Copy link
Contributor

JLHwung commented Mar 28, 2025

@seancheung Please report it to https://github.com/DefinitelyTyped/DefinitelyTyped, @types/babel__generator is not maintained by us.

@vovkasm
Copy link
Contributor Author

vovkasm commented Apr 3, 2025

@seancheung I created PR to DefinitelyTyped (wow, it's a huge git repo — 24Gb on disk)

@jakebailey
Copy link

If this package is in TypeScript now, can you all just publish types with your package? Seems like extra work and pain to have to update this in two places.

@nicolo-ribaudo
Copy link
Member

In the next major. We were considering doing it now already, but the new type definitions cause significant breakage compared to the DT ones.

@jakebailey
Copy link

Interesting, I guess I didn't think they'd be all that different. No worries, of course, just asking!

@seancheung
Copy link

@seancheung I created PR to DefinitelyTyped (wow, it's a huge git repo — 24Gb on disk)

Thanks bro!
BTW you could just do a shallow clone and it should be much smaller 👍

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 5, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Importing 'default' export from e.g. @babel/generator does not work in ESM with TS

6 participants