From 7c1c2ac9095aad7b2d00c76c57b650246dad1f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 28 Jul 2020 17:44:14 -0400 Subject: [PATCH] feat: sort config items in ascending priority --- .../options.json | 2 +- .../out-files/test.js | 1 + .../stdout.txt | 31 ++--- .../options.json | 2 +- .../out-files/test.js | 1 + .../stdout.txt | 30 ++--- .../env - SHOW_CONFIG_FOR-windows/stdout.txt | 29 +++-- .../babel/env - SHOW_CONFIG_FOR/stdout.txt | 121 +++++++++--------- .../babel-core/src/config/config-chain.js | 55 +++++--- 9 files changed, 148 insertions(+), 124 deletions(-) create mode 100644 packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/out-files/test.js create mode 100644 packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/out-files/test.js diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/options.json b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/options.json index b9fd75f44063..39dd94aba890 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/options.json +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/options.json @@ -1,5 +1,5 @@ { - "args": ["./src"], + "args": ["./src", "--out-file", "./test.js"], "os": ["win32"], "env": { "BABEL_SHOW_CONFIG_FOR": "./src/foo.js" diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/out-files/test.js b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/out-files/test.js new file mode 100644 index 000000000000..3918c74e4463 --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/out-files/test.js @@ -0,0 +1 @@ +"use strict"; diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt index 24fa10dadff7..fbdd672d7dab 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt @@ -1,3 +1,18 @@ +Babel configs on "/src/foo.js" (ascending priority): +config /babel.config.json +{ + "sourceType": "script" +} + +config /babel.config.json .overrides[0] +{ + "test": "src/foo.js", + "sourceType": "module" +} + +config /.babelrc +{} + programmatic options from @babel/cli { "sourceFileName": "src/foo.js", @@ -14,18 +29,4 @@ programmatic options from @babel/cli }, "filename": "src//foo.js" } - -config /.babelrc -{} - -config /babel.config.json -{ - "sourceType": "script" -} - -config /babel.config.json .overrides[0] -{ - "test": "src/foo.js", - "sourceType": "module" -} -"use strict"; +Successfully compiled 1 file with Babel (123ms). diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/options.json b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/options.json index bf8d448bb0ed..f825bf6da710 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/options.json +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/options.json @@ -1,5 +1,5 @@ { - "args": ["./src"], + "args": ["./src", "--out-file", "./test.js"], "os": ["darwin", "linux"], "env": { "BABEL_SHOW_CONFIG_FOR": "./src/foo.js" diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/out-files/test.js b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/out-files/test.js new file mode 100644 index 000000000000..3918c74e4463 --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/out-files/test.js @@ -0,0 +1 @@ +"use strict"; diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt index cbf08593bff2..0f982f0e98c4 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt @@ -1,3 +1,18 @@ +Babel configs on "/src/foo.js" (ascending priority): +config /babel.config.json +{ + "sourceType": "script" +} + +config /babel.config.json .overrides[0] +{ + "test": "src/foo.js", + "sourceType": "module" +} + +config /.babelrc +{} + programmatic options from @babel/cli { "sourceFileName": "src/foo.js", @@ -14,18 +29,3 @@ programmatic options from @babel/cli }, "filename": "src/foo.js" } - -config /.babelrc -{} - -config /babel.config.json -{ - "sourceType": "script" -} - -config /babel.config.json .overrides[0] -{ - "test": "src/foo.js", - "sourceType": "module" -} -"use strict"; diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt index fb0e45e93126..5d5860f58f82 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt @@ -1,3 +1,18 @@ +Babel configs on "/src/foo.js" (ascending priority): +config /babel.config.json +{ + "sourceType": "script" +} + +config /babel.config.json .overrides[0] +{ + "test": "src/foo.js", + "sourceType": "module" +} + +config /.babelrc +{} + programmatic options from @babel/cli { "sourceFileName": "../src/foo.js", @@ -14,18 +29,4 @@ programmatic options from @babel/cli }, "filename": "src//foo.js" } - -config /.babelrc -{} - -config /babel.config.json -{ - "sourceType": "script" -} - -config /babel.config.json .overrides[0] -{ - "test": "src/foo.js", - "sourceType": "module" -} Successfully compiled 1 file with Babel (123ms). diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt index d064e43a3fe2..b648f9d8bb48 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt @@ -1,52 +1,31 @@ -programmatic options from @babel/cli +Babel configs on "/src/index.js" (ascending priority): +config /my-extended.js { - "sourceFileName": "./src/index.js", + "sourceMaps": false, "presets": [ - "/packages/babel-preset-react" - ], - "plugins": [ - "/packages/babel-plugin-transform-arrow-functions", - "/packages/babel-plugin-transform-strict-mode", - "/packages/babel-plugin-transform-modules-commonjs" - ], - "configFile": "./my-config.js", - "caller": { - "name": "@babel/cli" - }, - "filename": "./src/index.js" -} - -config /.babelrc -{} - -config /my-config.js -{ - "sourceType": "script", - "plugins": [ - "[Function: (api) => ({/n name: /"@foo//" + __dirname,/n visitor ... ]" - ], - "extends": "./my-extended.js" + "@foo/babel-preset-1" + ] } -config /my-config.js .env["test"] +config /my-extended.js .env["test"] { - "plugins": [ + "presets": [ [ - "@foo/babel-plugin-3", + "@foo/babel-preset-3", { "noDocumentAll": true }, - "@foo/babel-plugin-three" + "@foo/babel-preset-three" ] ] } -config /my-config.js .overrides[0] +config /my-extended.js .overrides[0] { "test": "src/index.js", - "plugins": [ + "presets": [ [ - "@foo/babel-plugin-2", + "@foo/babel-preset-2", { "noDocumentAll": true } @@ -54,14 +33,19 @@ config /my-config.js .overrides[0] ] } -config /my-config.js .overrides[0].env["test"] +config /my-extended.js .overrides[0].env["test"] { - "plugins": [ - "@foo/babel-plugin-1", + "presets": [ + "@foo/babel-preset-1", [ { - "name": "@foo/inline-babel-plugin-1", - "visitor": {} + "name": "@foo/inline-babel-preset-1", + "plugins": [ + { + "name": "@foo/inline-babel-plugin-1", + "visitor": {} + } + ] }, { "noDocumentAll": true @@ -70,33 +54,34 @@ config /my-config.js .overrides[0].env["test"] ] } -config /my-extended.js +config /my-config.js { - "sourceMaps": false, - "presets": [ - "@foo/babel-preset-1" - ] + "sourceType": "script", + "plugins": [ + "[Function: (api) => ({/n name: /"@foo//" + __dirname,/n visitor ... ]" + ], + "extends": "./my-extended.js" } -config /my-extended.js .env["test"] +config /my-config.js .env["test"] { - "presets": [ + "plugins": [ [ - "@foo/babel-preset-3", + "@foo/babel-plugin-3", { "noDocumentAll": true }, - "@foo/babel-preset-three" + "@foo/babel-plugin-three" ] ] } -config /my-extended.js .overrides[0] +config /my-config.js .overrides[0] { "test": "src/index.js", - "presets": [ + "plugins": [ [ - "@foo/babel-preset-2", + "@foo/babel-plugin-2", { "noDocumentAll": true } @@ -104,19 +89,14 @@ config /my-extended.js .overrides[0] ] } -config /my-extended.js .overrides[0].env["test"] +config /my-config.js .overrides[0].env["test"] { - "presets": [ - "@foo/babel-preset-1", + "plugins": [ + "@foo/babel-plugin-1", [ { - "name": "@foo/inline-babel-preset-1", - "plugins": [ - { - "name": "@foo/inline-babel-plugin-1", - "visitor": {} - } - ] + "name": "@foo/inline-babel-plugin-1", + "visitor": {} }, { "noDocumentAll": true @@ -124,3 +104,24 @@ config /my-extended.js .overrides[0].env["test"] ] ] } + +config /.babelrc +{} + +programmatic options from @babel/cli +{ + "sourceFileName": "./src/index.js", + "presets": [ + "/packages/babel-preset-react" + ], + "plugins": [ + "/packages/babel-plugin-transform-arrow-functions", + "/packages/babel-plugin-transform-strict-mode", + "/packages/babel-plugin-transform-modules-commonjs" + ], + "configFile": "./my-config.js", + "caller": { + "name": "@babel/cli" + }, + "filename": "./src/index.js" +} diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index 7637022cf5d7..2c897139fb3d 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -240,10 +240,12 @@ export function* buildRootChain( if (context.showConfig) { console.log( - // print config by the order of descending priority - [programmaticReport, babelRcReport, configReport] - .filter(x => !!x) - .join("\n\n"), + // $FlowIgnore: context.showConfig implies context.filename is not null + `Babel configs on "${context.filename}" (ascending priority):\n` + + // print config by the order of ascending priority + [configReport, babelRcReport, programmaticReport] + .filter(x => !!x) + .join("\n\n"), ); return null; } @@ -493,33 +495,48 @@ function makeChainWalker({ return function* (input, context, files = new Set(), baseLogger) { const { dirname } = input; - const flattenedConfigs = []; - const logger = createLogger(input, context, baseLogger); + const flattenedConfigs: Array<{| + config: OptionsAndDescriptors, + index: ?number, + envName: ?string, + |}> = []; const rootOpts = root(input); if (configIsApplicable(rootOpts, dirname, context)) { - flattenedConfigs.push(rootOpts); - logger(rootOpts); + flattenedConfigs.push({ + config: rootOpts, + envName: undefined, + index: undefined, + }); const envOpts = env(input, context.envName); if (envOpts && configIsApplicable(envOpts, dirname, context)) { - flattenedConfigs.push(envOpts); - logger(envOpts, undefined, context.envName); + flattenedConfigs.push({ + config: envOpts, + envName: context.envName, + index: undefined, + }); } (rootOpts.options.overrides || []).forEach((_, index) => { const overrideOps = overrides(input, index); if (configIsApplicable(overrideOps, dirname, context)) { - flattenedConfigs.push(overrideOps); - logger(overrideOps, index); + flattenedConfigs.push({ + config: overrideOps, + index, + envName: undefined, + }); const overrideEnvOpts = overridesEnv(input, index, context.envName); if ( overrideEnvOpts && configIsApplicable(overrideEnvOpts, dirname, context) ) { - flattenedConfigs.push(overrideEnvOpts); - logger(overrideEnvOpts, index, context.envName); + flattenedConfigs.push({ + config: overrideEnvOpts, + index, + envName: context.envName, + }); } } }); @@ -529,7 +546,7 @@ function makeChainWalker({ // that we don't do extra work loading extended configs if a file is // ignored. if ( - flattenedConfigs.some(({ options: { ignore, only } }) => + flattenedConfigs.some(({ config: { options: { ignore, only } } }) => shouldIgnore(context, ignore, only, dirname), ) ) { @@ -537,12 +554,13 @@ function makeChainWalker({ } const chain = emptyChain(); + const logger = createLogger(input, context, baseLogger); - for (const op of flattenedConfigs) { + for (const { config, index, envName } of flattenedConfigs) { if ( !(yield* mergeExtendsChain( chain, - op.options, + config.options, dirname, context, files, @@ -552,7 +570,8 @@ function makeChainWalker({ return null; } - mergeChainOpts(chain, op); + logger(config, index, envName); + mergeChainOpts(chain, config); } return chain; };