From 4f95657816d95b30b9b799d45a9adb985a8a3cdb Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Thu, 26 Sep 2019 13:00:58 -0700 Subject: [PATCH 1/3] Add support for babel.config.json root config --- .../src/config/files/configuration.js | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/babel-core/src/config/files/configuration.js b/packages/babel-core/src/config/files/configuration.js index ab3353f6fde3..1993b028fb74 100644 --- a/packages/babel-core/src/config/files/configuration.js +++ b/packages/babel-core/src/config/files/configuration.js @@ -19,6 +19,11 @@ import type { CallerMetadata } from "../validation/options"; const debug = buildDebug("babel:config:loading:files:configuration"); const BABEL_CONFIG_JS_FILENAME = "babel.config.js"; +const BABEL_CONFIG_JSON_FILENAME = "babel.config.json"; +const ROOT_CONFIG_FILENAMES = [ + BABEL_CONFIG_JS_FILENAME, + BABEL_CONFIG_JSON_FILENAME, +]; const BABELRC_FILENAME = ".babelrc"; const BABELRC_JS_FILENAME = ".babelrc.js"; @@ -27,7 +32,10 @@ const BABELIGNORE_FILENAME = ".babelignore"; export function findConfigUpwards(rootDir: string): string | null { let dirname = rootDir; while (true) { - if (fs.existsSync(path.join(dirname, BABEL_CONFIG_JS_FILENAME))) { + if ( + fs.existsSync(path.join(dirname, BABEL_CONFIG_JS_FILENAME)) || + fs.existsSync(path.join(dirname, BABEL_CONFIG_JSON_FILENAME)) + ) { return dirname; } @@ -110,11 +118,15 @@ export function findRootConfig( envName: string, caller: CallerMetadata | void, ): ConfigFile | null { - const filepath = path.resolve(dirname, BABEL_CONFIG_JS_FILENAME); - - const conf = readConfig(filepath, envName, caller); - if (conf) { - debug("Found root config %o in %o.", BABEL_CONFIG_JS_FILENAME, dirname); + let conf = null; + for (const filename of ROOT_CONFIG_FILENAMES) { + const filepath = path.resolve(dirname, filename); + + conf = readConfig(filepath, envName, caller); + if (conf) { + debug("Found root config %o in %o.", filename, dirname); + break; + } } return conf; } From 4d35e00543cda667b57d898bbdc0a8f94df022e8 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Thu, 26 Sep 2019 13:28:58 -0700 Subject: [PATCH 2/3] Throw if multiple configuration files are found --- .../src/config/files/configuration.js | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/babel-core/src/config/files/configuration.js b/packages/babel-core/src/config/files/configuration.js index 1993b028fb74..adf26e6c687f 100644 --- a/packages/babel-core/src/config/files/configuration.js +++ b/packages/babel-core/src/config/files/configuration.js @@ -118,17 +118,29 @@ export function findRootConfig( envName: string, caller: CallerMetadata | void, ): ConfigFile | null { - let conf = null; - for (const filename of ROOT_CONFIG_FILENAMES) { - const filepath = path.resolve(dirname, filename); - - conf = readConfig(filepath, envName, caller); - if (conf) { - debug("Found root config %o in %o.", filename, dirname); - break; - } + const config = ROOT_CONFIG_FILENAMES.reduce( + (previousConfig: ConfigFile | null, name) => { + const filepath = path.resolve(dirname, name); + const config = readConfig(filepath, envName, caller); + + if (config && previousConfig) { + throw new Error( + `Multiple configuration files found. Please remove one:\n` + + ` - ${path.basename(previousConfig.filepath)}\n` + + ` - ${name}\n` + + `from ${dirname}`, + ); + } + + return config || previousConfig; + }, + null, + ); + + if (config) { + debug("Found configuration %o from %o.", config.filepath, dirname); } - return conf; + return config; } export function loadConfig( From a78d3860c4706e60f837800989c427e2c1a3c283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 3 Oct 2019 10:02:08 +0200 Subject: [PATCH 3/3] Add tests --- packages/babel-core/test/config-chain.js | 46 +++++++++++++++++++ .../babel-config-js-and-json/babel.config.js | 3 ++ .../babel.config.json | 3 ++ .../babel-config-js/babel.config.js | 3 ++ .../babel-config-json/babel.config.json | 3 ++ 5 files changed, 58 insertions(+) create mode 100644 packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.js create mode 100644 packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.json create mode 100644 packages/babel-core/test/fixtures/config/config-files/babel-config-js/babel.config.js create mode 100644 packages/babel-core/test/fixtures/config/config-files/babel-config-json/babel.config.json diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index 32513b1a4519..c5f6e2ebf205 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -944,6 +944,52 @@ describe("buildConfigChain", function() { } }); + it("should load babel.config.json", () => { + const filename = fixture("config-files", "babel-config-json", "src.js"); + + expect( + loadOptions({ + filename, + cwd: path.dirname(filename), + }), + ).toEqual({ + ...getDefaults(), + filename: filename, + cwd: path.dirname(filename), + root: path.dirname(filename), + comments: true, + }); + }); + + it("should load babel.config.js", () => { + const filename = fixture("config-files", "babel-config-js", "src.js"); + + expect( + loadOptions({ + filename, + cwd: path.dirname(filename), + }), + ).toEqual({ + ...getDefaults(), + filename: filename, + cwd: path.dirname(filename), + root: path.dirname(filename), + comments: true, + }); + }); + + it("should whtow if both babel.config.json and babel.config.js are used", () => { + const filename = fixture( + "config-files", + "babel-config-js-and-json", + "src.js", + ); + + expect(() => + loadOptions({ filename, cwd: path.dirname(filename) }), + ).toThrow(/Multiple configuration files found/); + }); + it("should load .babelrc", () => { const filename = fixture("config-files", "babelrc", "src.js"); diff --git a/packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.js b/packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.js new file mode 100644 index 000000000000..409f4a98ac90 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.js @@ -0,0 +1,3 @@ +module.exports = { + comments: true +}; diff --git a/packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.json b/packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.json new file mode 100644 index 000000000000..4b7be6033f68 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.json @@ -0,0 +1,3 @@ +{ + "comments": true +} \ No newline at end of file diff --git a/packages/babel-core/test/fixtures/config/config-files/babel-config-js/babel.config.js b/packages/babel-core/test/fixtures/config/config-files/babel-config-js/babel.config.js new file mode 100644 index 000000000000..409f4a98ac90 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files/babel-config-js/babel.config.js @@ -0,0 +1,3 @@ +module.exports = { + comments: true +}; diff --git a/packages/babel-core/test/fixtures/config/config-files/babel-config-json/babel.config.json b/packages/babel-core/test/fixtures/config/config-files/babel-config-json/babel.config.json new file mode 100644 index 000000000000..4b7be6033f68 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files/babel-config-json/babel.config.json @@ -0,0 +1,3 @@ +{ + "comments": true +} \ No newline at end of file