-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix(babel-generator): add named export of generate function #17100
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
fix(babel-generator): add named export of generate function #17100
Conversation
This should fix babel#15269. Import of CJS built @babel/generator module from esm node.js scripts. Now one can use named import.
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58647 |
JLHwung
left a comment
There was a problem hiding this 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.
|
I don't think this PR actually fixes #15269, this PR adds support for |
|
@JLHwung But it actually works, and really fix mentioned issue. I mean, that at least we can import and use $ node --version
v22.13.1
$ npm install @babel/generatorCreate 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]
} |
|
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 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 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. |
|
Agreed! Thanks for the detailed explanation. Technically, it's not a fix, it's a workaround. 🤝 |
nicolo-ribaudo
left a comment
There was a problem hiding this 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?
|
@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? |
|
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 :) |
Named export was introduced in babel/babel#17100 and allows to import generator from node.js esm scripts from cjs built generator.
* 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]>
|
The @types/babel__generator package is still outdated. How do you guys use it in a typescript project? |
|
@seancheung Please report it to https://github.com/DefinitelyTyped/DefinitelyTyped, |
|
@seancheung I created PR to DefinitelyTyped (wow, it's a huge git repo — 24Gb on disk) |
|
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. |
|
In the next major. We were considering doing it now already, but the new type definitions cause significant breakage compared to the DT ones. |
|
Interesting, I guess I didn't think they'd be all that different. No worries, of course, just asking! |
Thanks bro! |

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