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

add showConfig support #11588

Merged
merged 23 commits into from
Jul 30, 2020
Merged

add showConfig support #11588

merged 23 commits into from
Jul 30, 2020

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented May 19, 2020

Q                       A
Fixed Issues? Fixes #10617, closes #11018, closes #10538
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link babel/website#2302
Any Dependency Changes?
License MIT

showConfig will print the config chain applied on a specific file path by order of descending priority. It will also print the effective overrides and env config applied to a single file.

Example output (input, Full output)

(The <CWD> and <ROOTDIR> is not presented in the raw output, it is replaced by Babel's fixture runner.)

Babel configs on "<CWD>/src/index.js" (ascending priority):
config <CWD>/my-extended.js
{
  "sourceMaps": false,
  "presets": [
    "@foo/babel-preset-1"
  ]
}

config <CWD>/my-config.js
{
  "sourceType": "script",
  "plugins": [
    "[Function: (api) => ({/n  name: /"@foo//" + __dirname,/n  visitor ... ]"
  ],
  "extends": "./my-extended.js"
}

config <CWD>/my-config.js .env["test"]
{
  "plugins": [
    [
      "@foo/babel-plugin-3",
      {
        "noDocumentAll": true
      },
      "@foo/babel-plugin-three"
    ]
  ]
}

config <CWD>/my-config.js .overrides[0]
{
  "test": "src/index.js",
  "plugins": [
    [
      "@foo/babel-plugin-2",
      {
        "noDocumentAll": true
      }
    ]
  ]
}

config <CWD>/my-config.js .overrides[0].env["test"]
{
  "plugins": [
    "@foo/babel-plugin-1",
    [
      {
        "name": "@foo/inline-babel-plugin-1",
        "visitor": {}
      },
      {
        "noDocumentAll": true
      }
    ]
  ]
}

config <CWD>/.babelrc
{}

programmatic options from @babel/cli
{
  "sourceFileName": "./src/index.js",
  "presets": [
    "<ROOTDIR>/packages/babel-preset-react"
  ],
  "plugins": [
    "<ROOTDIR>/packages/babel-plugin-transform-arrow-functions",
    "<ROOTDIR>/packages/babel-plugin-transform-strict-mode",
    "<ROOTDIR>/packages/babel-plugin-transform-modules-commonjs"
  ],
  "configFile": "./my-config.js",
  "caller": {
    "name": "@babel/cli"
  },
  "filename": "./src/index.js"
}

Known limits

  • It does not expand the presets
  • It does not print the resolved config
  • It does not tell users why certain file is ignored/excluded

These limits can be addressed in other PRs.

Additional
This PR includes commits in #11544.

@JLHwung JLHwung added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: core labels May 19, 2020
@babel-bot
Copy link
Collaborator

babel-bot commented May 19, 2020

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 19, 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 a577902:

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

@JLHwung JLHwung force-pushed the print-config branch 3 times, most recently from 6089574 to 56516c5 Compare June 15, 2020 20:10
@JLHwung JLHwung force-pushed the print-config branch 3 times, most recently from 6cacd17 to 46978e8 Compare June 16, 2020 20:39
@JLHwung JLHwung marked this pull request as ready for review June 16, 2020 20:43
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome

packages/babel-core/src/config/config-chain.js Outdated Show resolved Hide resolved

mergeChain(fileChain, result);
}
}

if (context.showConfig) {
console.log(
// print config by the order of descending priority
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should print in the output that in case of conflicts the first config overrides the second?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to keep the output succinct. The implementation detail of config merging should be referenced from babel docs but not a debug output.

To reason how these config merges, we can print the resolved config in later PRs. Think of the Styles tab v.s. the Computed tab in the Chrome DevTools / Elements.

@nicolo-ribaudo nicolo-ribaudo added this to the 7.11.0 milestone Jun 17, 2020
} else if (typeof d.value === "function") {
// If the unloaded descriptor is a function, i.e. `plugins: [ require("my-plugin") ]`,
// we print the first 50 characters of the function source code and hopefully we can see
// `name: 'my-plugin'` in the source
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good hack, although I don't believe this will be the case for most plugins?

Even testing on https://codesandbox.io/s/lively-star-xv8bt, it will just print the code instead, the default plugin on ASTExplorer wouldn't even show the name until char 85.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Well since an object has no key orders, I don't think there exists an integer N such that the plugin name is included in the (0, N) substring.

My initial thought here is to provide user at least something more descriptive/recognizable than a generic [Function] string. I have come up with several approach:

  • Print Function name. It could not help much because we don't have such convention that the default export should be a named function
  • Use regex to extract name: "blablabla" from function source. It doesn't work with dynamic name and has false positive.
  • Actually load the descriptor. We have to initialize the API object earlier, and it may introduce breaking changes as we can end up loading a plugin twice.

So I end up with this approach and hope it could serve as an indicator so that users could know what this plugin is -- most of the time these are ad-hoc plugins not in node_modules.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use regex to extract name: "blablabla" from function source. It doesn't work with dynamic name

I think it would be really rare/weird to have a dynamic name, it's just a string? Yeah I don't think it'll be an issue in general but for standalone/repl which is a diff usecase altogether.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is something that we can revisit with experience: I believe that the hard-to-understand function name might not be a problem for a few reasons:

  1. Most people use "babel-plugin-foo" or require.resolve("babel-plugin-foo")
  2. .slice(0, 50) always includes the plugin function name (or at least the first 41 characters if it's longer). This can be useful for plugins directly defined in the config file, as we do.
  3. If the plugin is an actual anonymous function and the .slice(0, 50) substring doesn't contain any useful information, the person can easily see in which config file it is defined and check what's used at that index in the plugins array directly in the config file.

@JLHwung JLHwung force-pushed the print-config branch 2 times, most recently from c82e049 to bf61000 Compare June 22, 2020 19:35
@JLHwung JLHwung changed the base branch from master to main June 22, 2020 19:36
@JLHwung JLHwung force-pushed the print-config branch 2 times, most recently from c787103 to c6b74c8 Compare June 23, 2020 19:39
export function* resolveShowConfigPath(
dirname: string,
): Handler<string | null> {
const targetPath = process.env.BABEL_SHOW_CONFIG_FOR_PATH;
Copy link
Contributor Author

@JLHwung JLHwung Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think the following candidates are easier for typing?

BABEL_SHOW_CONFIG_FOR
BABEL_CONFIG_FOR

I don't think we can simply drop FOR because it looks like a boolean toggle

BABEL_SHOW_CONFIG=true

will never work as intended.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I slightly prefer BABEL_SHOW_CONFIG_FOR

@existentialism existentialism self-assigned this Jun 25, 2020
@nicolo-ribaudo nicolo-ribaudo self-requested a review June 29, 2020 23:19
@JLHwung JLHwung force-pushed the print-config branch 2 times, most recently from edbe64d to 3886071 Compare July 2, 2020 19:44
@existentialism
Copy link
Member

Should we add a test for .only?

@JLHwung JLHwung force-pushed the print-config branch 3 times, most recently from 7c1c2ac to 0d5fe2c Compare July 28, 2020 22:08
@JLHwung JLHwung merged commit 164a939 into babel:main Jul 30, 2020
@JLHwung JLHwung deleted the print-config branch July 30, 2020 13:24
@existentialism existentialism removed their assignment Jul 30, 2020
@alexkreidler
Copy link

Just a follow up: is there a reason you removed (bfdf5cf) the showConfig boolean option?

That seemed like the easiest way to use this feature.

@nicolo-ribaudo
Copy link
Member

The reason is that it would log the config for every file processed by Babel, generating an output so big that it would hardly be useful.

@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 26, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 26, 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.

RFC: Print Babel Configuration Feature Request: a flag to print the current config
6 participants