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 #12151
Support "extensions" option in config files and presets #12151
Conversation
@@ -147,6 +147,8 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig( | |||
const opts: Object = optionDefaults; | |||
mergeOptions(opts, options); | |||
|
|||
if (!isCompilableFile(context.filename, opts.extensions)) return null; |
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.
After #11907 is merged, I need to check how to make Babel report these files as "unsupported"
.
The we can use this info in @babel/cli
/@babel/register
instead of a custom ignore logic.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/29708/ |
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 1acae43:
|
7eb2a67
to
609969a
Compare
describe("integration tests", function () { | ||
it("can hook into extensions defined by the config", async () => { | ||
const { stdout, stderr } = await fixture("load-ts"); | ||
|
||
expect(stdout).toMatchInlineSnapshot(` | ||
"LOADED: \\"<ROOT>/load-ts/index.js\\" | ||
LOADED: \\"<ROOT>/load-ts/foo.ts\\" | ||
DONE: foo.ts | ||
DONE: index.js | ||
" | ||
`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
}); |
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 test shows well the new feature 🎉
@babel/register
can now load .ts
files (or in general, any extension) by specifying it in babel.config.json
rather than in the programmatic options
7afa235
to
c19499e
Compare
// 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
// 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).
6a797c6
to
288f4ef
Compare
960b087
to
eab3c06
Compare
49276e2
to
000eaa5
Compare
- 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 ["*"].
000eaa5
to
2387ea7
Compare
// Technically we could use the "semver" package here, but (for exmaple) | ||
// parseFloat("4.23.6") returns 4.23 so it's "good enough" | ||
export const BABEL_SUPPORTS_EXTENSIONS_OPTION = | ||
parseFloat(babel.version) >= 7.11; |
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.
⚠️
- Before merging this PR, I need to add a warning in
new-version
inMakefile
to replace 7.11 with the actual new version.
|
||
// Technically we could use the "semver" package here, but (for exmaple) | ||
// parseFloat("4.23.6") returns 4.23 so it's "good enough" | ||
const BABEL_SUPPORTS_EXTENSIONS_OPTION = parseFloat(babel.version) >= 7.11; |
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.
⚠️
- Before merging this PR, I need to add a warning in
new-version
inMakefile
to replace 7.11 with the actual new version.
@@ -15,6 +15,10 @@ export default declare( | |||
) => { | |||
api.assertVersion(7); | |||
|
|||
// Technically we could use the "semver" package here, but (for exmaple) | |||
// parseFloat("4.23.6") returns 4.23 so it's "good enough" | |||
const BABEL_SUPPORTS_EXTENSIONS_OPTION = parseFloat(api.version) >= 7.11; |
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.
⚠️
- Before merging this PR, I need to add a warning in
new-version
inMakefile
to replace 7.11 with the actual new version.
@@ -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 🎉
This was closed by accident because I deleted the target branch. I'll reopen. |
Moved to #12216
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 can also fix the following issues, when we'll define
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