add showConfig support (#11588)

* draft: showConfig support

* feat: pass through showConfig command options

* update test file

* refactor: add createLogger to makeChainWalker

* serializing dynamic plugin instance

* fix flow errors

* chore: add tests on extended config

* fix: do not print empty presets

* add more test cases

* add windows testcases

* address review comments

* throw error when showConfigPath does not exist

* print reason when showConfig is targetting an ignored file

* remove showConfig: boolean

* refactor: simplify environment flag name

* rename test fixtures

* fix: throw when SHOW_CONFIG_FOR is not a regular file

* cleanup test fixtures

* add test on only

* Update packages/babel-core/src/config/files/configuration.js

Co-authored-by: Brian Ng <bng412@gmail.com>

* address review comments

* update test fixtures

* feat: sort config items in ascending priority

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Huáng Jùnliàng
2020-07-30 09:24:19 -04:00
committed by GitHub
parent 374a253d0c
commit 164a93945d
57 changed files with 827 additions and 39 deletions

View File

@@ -0,0 +1,14 @@
{
"sourceType": "script",
"overrides": [
{
"test": "src/index.js",
"sourceType": "module"
}
],
"env": {
"test": {
"ignore": ["./src/index.js"]
}
}
}

View File

@@ -0,0 +1,7 @@
{
"args": ["./src/index.js"],
"env": {
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
},
"os": ["darwin", "linux"]
}

View File

@@ -0,0 +1 @@
No config is applied to "<CWD>/src/index.js" because it matches one of `ignore: ["./src/index.js"]` from "<CWD>"

View File

@@ -0,0 +1,13 @@
{
"sourceType": "script",
"overrides": [
{
"test": "src/foo.js",
"sourceType": "module"
},
{
"test": "src/bar.js",
"sourceType": "script"
}
]
}

View File

@@ -0,0 +1,7 @@
{
"args": ["./src", "--out-file", "./test.js"],
"os": ["win32"],
"env": {
"BABEL_SHOW_CONFIG_FOR": "./src/foo.js"
}
}

View File

@@ -0,0 +1,31 @@
Babel configs on "<CWD>/src/foo.js" (ascending priority):
config <CWD>/babel.config.json
{
"sourceType": "script"
}
config <CWD>/babel.config.json .overrides[0]
{
"test": "src/foo.js",
"sourceType": "module"
}
config <CWD>/.babelrc
{}
programmatic options from @babel/cli
{
"sourceFileName": "src/foo.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"
],
"caller": {
"name": "@babel/cli"
},
"filename": "src//foo.js"
}

View File

@@ -0,0 +1,13 @@
{
"sourceType": "script",
"overrides": [
{
"test": "src/foo.js",
"sourceType": "module"
},
{
"test": "src/bar.js",
"sourceType": "script"
}
]
}

View File

@@ -0,0 +1,7 @@
{
"args": ["./src", "--out-file", "./test.js"],
"os": ["darwin", "linux"],
"env": {
"BABEL_SHOW_CONFIG_FOR": "./src/foo.js"
}
}

View File

@@ -0,0 +1,31 @@
Babel configs on "<CWD>/src/foo.js" (ascending priority):
config <CWD>/babel.config.json
{
"sourceType": "script"
}
config <CWD>/babel.config.json .overrides[0]
{
"test": "src/foo.js",
"sourceType": "module"
}
config <CWD>/.babelrc
{}
programmatic options from @babel/cli
{
"sourceFileName": "src/foo.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"
],
"caller": {
"name": "@babel/cli"
},
"filename": "src/foo.js"
}

View File

@@ -0,0 +1,14 @@
{
"sourceType": "script",
"overrides": [
{
"test": "src/index.js",
"sourceType": "module"
}
],
"env": {
"test": {
"only": ["./src/unicorn.js"]
}
}
}

View File

@@ -0,0 +1,7 @@
{
"args": ["./src/index.js"],
"env": {
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
},
"os": ["darwin", "linux"]
}

View File

@@ -0,0 +1 @@
No config is applied to "<CWD>/src/index.js" because it fails to match one of `only: ["./src/unicorn.js"]` from "<CWD>"

View File

@@ -0,0 +1,13 @@
{
"sourceType": "script",
"overrides": [
{
"test": "src/foo.js",
"sourceType": "module"
},
{
"test": "src/bar.js",
"sourceType": "script"
}
]
}

View File

@@ -0,0 +1,7 @@
{
"args": ["./src", "-d", "lib"],
"env": {
"BABEL_SHOW_CONFIG_FOR": "./src/foo.js"
},
"os": ["win32"]
}

View File

@@ -0,0 +1 @@
"use strict";

View File

@@ -0,0 +1,32 @@
Babel configs on "<CWD>/src/foo.js" (ascending priority):
config <CWD>/babel.config.json
{
"sourceType": "script"
}
config <CWD>/babel.config.json .overrides[0]
{
"test": "src/foo.js",
"sourceType": "module"
}
config <CWD>/.babelrc
{}
programmatic options from @babel/cli
{
"sourceFileName": "../src/foo.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"
],
"caller": {
"name": "@babel/cli"
},
"filename": "src//foo.js"
}
Successfully compiled 1 file with Babel (123ms).

View File

@@ -0,0 +1,40 @@
module.exports = {
sourceType: "script",
plugins: [require("@foo/babel-plugin-1")],
extends: "./my-extended.js",
overrides: [
{
test: "src/index.js",
plugins: [["@foo/babel-plugin-2", { noDocumentAll: true }]],
env: {
test: {
plugins: [
"@foo/babel-plugin-1",
[
{ name: "@foo/inline-babel-plugin-1", visitor: { Program() {} } },
{ noDocumentAll: true },
],
],
},
},
},
{
exclude: "src/index.js",
plugins: ["@foo/babel-plugin-4"],
},
],
env: {
test: {
plugins: [
[
"@foo/babel-plugin-3",
{ noDocumentAll: true },
"@foo/babel-plugin-three",
],
],
},
development: {
plugins: ["@foo/babel-plugin-4"],
},
},
};

View File

@@ -0,0 +1,47 @@
module.exports = {
sourceMaps: false,
presets: ["@foo/babel-preset-1"],
overrides: [
{
test: "src/index.js",
presets: [["@foo/babel-preset-2", { noDocumentAll: true }]],
env: {
test: {
presets: [
"@foo/babel-preset-1",
[
{
name: "@foo/inline-babel-preset-1",
plugins: [
{
name: "@foo/inline-babel-plugin-1",
visitor: { Program() {} },
},
],
},
{ noDocumentAll: true },
],
],
},
},
},
{
exclude: "src/index.js",
presets: ["@foo/babel-preset-4"],
},
],
env: {
test: {
presets: [
[
"@foo/babel-preset-3",
{ noDocumentAll: true },
"@foo/babel-preset-three",
],
],
},
development: {
presets: ["@foo/babel-preset-4"],
},
},
};

View File

@@ -0,0 +1,6 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
visitor: {
Program() {}
}
})

View File

@@ -0,0 +1,6 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
visitor: {
Program() {}
}
})

View File

@@ -0,0 +1,6 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
visitor: {
Program() {}
}
})

View File

@@ -0,0 +1,6 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
visitor: {
Program() {}
}
})

View File

@@ -0,0 +1,4 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
plugins: ["@foo/" + __dirname.replace("preset", "plugin")]
})

View File

@@ -0,0 +1,4 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
plugins: ["@foo/" + __dirname.replace("preset", "plugin")]
})

View File

@@ -0,0 +1,4 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
plugins: ["@foo/" + __dirname.replace("preset", "plugin")]
})

View File

@@ -0,0 +1,4 @@
module.exports = (api) => ({
name: "@foo/" + __dirname,
plugins: ["@foo/" + __dirname.replace("preset", "plugin")]
})

View File

@@ -0,0 +1,8 @@
{
"args": ["--config-file", "./my-config.js", "./src/index.js"],
"env": {
"BABEL_ENV": "test",
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
},
"os": ["darwin", "linux"]
}

View File

@@ -0,0 +1,127 @@
Babel configs on "<CWD>/src/index.js" (ascending priority):
config <CWD>/my-extended.js
{
"sourceMaps": false,
"presets": [
"@foo/babel-preset-1"
]
}
config <CWD>/my-extended.js .env["test"]
{
"presets": [
[
"@foo/babel-preset-3",
{
"noDocumentAll": true
},
"@foo/babel-preset-three"
]
]
}
config <CWD>/my-extended.js .overrides[0]
{
"test": "src/index.js",
"presets": [
[
"@foo/babel-preset-2",
{
"noDocumentAll": true
}
]
]
}
config <CWD>/my-extended.js .overrides[0].env["test"]
{
"presets": [
"@foo/babel-preset-1",
[
{
"name": "@foo/inline-babel-preset-1",
"plugins": [
{
"name": "@foo/inline-babel-plugin-1",
"visitor": {}
}
]
},
{
"noDocumentAll": true
}
]
]
}
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"
}

View File

@@ -0,0 +1,13 @@
{
"sourceType": "script",
"overrides": [
{
"test": "src/foo.js",
"sourceType": "module"
},
{
"test": "src/bar.js",
"sourceType": "script"
}
]
}

View File

@@ -0,0 +1,8 @@
{
"args": ["./src", "-d", "lib"],
"env": {
"BABEL_SHOW_CONFIG_FOR": "./src"
},
"os": ["win32"],
"stderrContains": true
}

View File

@@ -0,0 +1 @@
Error: <CWD>\src: BABEL_SHOW_CONFIG_FOR must refer to a regular file, directories are not supported.

View File

@@ -0,0 +1,13 @@
{
"sourceType": "script",
"overrides": [
{
"test": "src/foo.js",
"sourceType": "module"
},
{
"test": "src/bar.js",
"sourceType": "script"
}
]
}

View File

@@ -0,0 +1,8 @@
{
"args": ["./src", "-d", "lib"],
"env": {
"BABEL_SHOW_CONFIG_FOR": "./src"
},
"os": ["darwin", "linux"],
"stderrContains": true
}

View File

@@ -0,0 +1 @@
Error: <CWD>/src: BABEL_SHOW_CONFIG_FOR must refer to a regular file, directories are not supported.