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
Support "extensions" option in config files and presets #12216
base: main
Are you sure you want to change the base?
Conversation
// Node.js's algorithm tries to automatically resolve the extension of the | ||
// required file. This means that if you have, for example, require("./foo") | ||
// it can load foo.js (even if ".js" is not specified). | ||
// This doesn't only work with predefined extensions: whenever a new extension | ||
// hook is registered, Node.js can resolve it. | ||
// For this reason, we need to register new extensions as soon as we know about | ||
// them. This means: | ||
// 1) When we compile a file and see a new extension in its `extensions` option. | ||
// 2) When a file with a new explicit extension is loaded | ||
// | ||
// In practice, this means that to load foo.ts you have to use | ||
// node -r @babel/register ./foo.ts | ||
// instead of just | ||
// node -r @babel/register ./foo |
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 will need to be clearly written in the docs.
Another example that cannot work:
// index.js
require("@babel/register");
require("./foo"); // <-- You'll need require("./foo.ts") here, since it's the first time Babel sees a .ts file
// foo.ts
console.log("OK");
Note that the ESM loader won't have this problem, it's CJS-specific (and only for the entry points, require("./foo")
correctly resolves foo.ts
if it's not the entry point).
@@ -0,0 +1 @@ | |||
console.log("DONE: foo.ts"); |
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 .ts
file is loaded because the TS preset is defined in the config 🎉
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/30583/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 4805c69:
|
This comment has been minimized.
This comment has been minimized.
- It can be set in programmatic option, babel.config.json, .babelrc.json and in presets - Files not matching any extension are ignored - If no filename is given, the file is not ignored - "*" is supported as a catch-all extension For backward compatibility reasons, this option defaults to ["*"].
ae4b79f
to
4805c69
Compare
@nicolo-ribaudo If it's useful, here's the WIP patch I had when I was experimenting with this: main...loganfsmyth:__archive-allow-user-extensions Primary differences I see is that:
So on the |
Original: #12151
This PR makes it possible to specify, for example,
extensions: [".ts", ".tsx"]
in the typescript preset, or in a config file. By doing so, users can keep their config in a single place instead of manually passing the extensions to@babel/register
/@babel/cli
/@babel/node
and other integrations.Depends on #11689, which makes it possible to know the list of extensions before instantiating the plugins.
This PR fixes following issues, by defining
extensions: [".ts", ".tsx"]
in@babel/preset-typescript
(so Babel can hook into.ts
loading without explicitly specifying it in@babel/register
's options):These issues are fixed by not special-casing extensions handling in
@babel/cli
:--no-copy-ignored
only ignores.js
files and does not enforce config "ignore:" settings #11394These are done in 4 different commits; I suggest reviewing them one by one!
TODO:
@babel/register
@babel/cli