Support "extensions" option in config files and presets#12216
Support "extensions" option in config files and presets#12216nicolo-ribaudo wants to merge 5 commits intobabel:mainfrom
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.
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.
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/nodeand 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.tsloading without explicitly specifying it in@babel/register's options):These issues are fixed by not special-casing extensions handling in
@babel/cli:--no-copy-ignoredonly ignores.jsfiles 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