Skip to content
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

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Oct 7, 2020

Moved to #12216

Q                       A
Fixed Issues? Closes #11409, fixes #8652, fixes #11394, fixes #10232, fixes #8962, fixes #11833, closes #8971
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT
  • 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 ["*"].

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:


These are done in 4 different commits; I suggest reviewing them one by one!

TODO:

  • Add the option
  • Use it in @babel/register
  • Use it in @babel/cli
  • Use it in the TS preset

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: core labels Oct 7, 2020
@@ -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;
Copy link
Member Author

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.

#11907 (comment)

@babel-bot
Copy link
Collaborator

babel-bot commented Oct 7, 2020

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 7, 2020

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:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo nicolo-ribaudo marked this pull request as draft October 7, 2020 22:59
@nicolo-ribaudo nicolo-ribaudo force-pushed the extensions-in-config branch 2 times, most recently from 7eb2a67 to 609969a Compare October 8, 2020 16:39
Comment on lines +20 to +32
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(`""`);
});
Copy link
Member Author

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

@nicolo-ribaudo nicolo-ribaudo force-pushed the extensions-in-config branch 3 times, most recently from 7afa235 to c19499e Compare October 9, 2020 13:49
Comment on lines +52 to +54
// 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
Copy link
Member Author

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).

@nicolo-ribaudo nicolo-ribaudo force-pushed the extensions-in-config branch 2 times, most recently from 6a797c6 to 288f4ef Compare October 9, 2020 15:36
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo/instantiate-presets-before-plugins branch from 960b087 to eab3c06 Compare October 9, 2020 15:54
@nicolo-ribaudo nicolo-ribaudo marked this pull request as ready for review October 9, 2020 15:54
@nicolo-ribaudo nicolo-ribaudo mentioned this pull request Oct 9, 2020
3 tasks
- 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 ["*"].
@nicolo-ribaudo nicolo-ribaudo changed the base branch from nicolo-ribaudo/instantiate-presets-before-plugins to feat-7.12.0/config-loading-updates October 9, 2020 16:24
// 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;
Copy link
Member Author

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 in Makefile 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;
Copy link
Member Author

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 in Makefile 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;
Copy link
Member Author

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 in Makefile to replace 7.11 with the actual new version.

@@ -0,0 +1 @@
console.log("DONE: foo.ts");
Copy link
Member Author

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 🎉

@nicolo-ribaudo
Copy link
Member Author

This was closed by accident because I deleted the target branch. I'll reopen.

@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 Jan 19, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 19, 2021
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: core PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants