From 9a4b764bdea75c7f52bc8fc779610bd7cc69d8f9 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 26 Sep 2017 18:29:28 -0700 Subject: [PATCH] Centralize config processing in class. --- .../src/config/build-config-chain.js | 42 +++++++++++-------- .../src/config/loading/files/configuration.js | 2 +- .../src/config/loading/files/index-browser.js | 2 +- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/packages/babel-core/src/config/build-config-chain.js b/packages/babel-core/src/config/build-config-chain.js index fe213f439d..3edf48d623 100644 --- a/packages/babel-core/src/config/build-config-chain.js +++ b/packages/babel-core/src/config/build-config-chain.js @@ -7,7 +7,7 @@ import buildDebug from "debug"; const debug = buildDebug("babel:config:config-chain"); -import { findConfigs, loadConfig } from "./loading/files"; +import { findConfigs, loadConfig, type ConfigFile } from "./loading/files"; type ConfigItem = { type: "options" | "arguments", @@ -28,25 +28,13 @@ export default function buildConfigChain(opts: {}): Array | null { ); try { - builder.mergeConfig({ - type: "arguments", - options: opts, - alias: "base", - dirname: process.cwd(), - }); + builder.mergeConfigArguments(opts, process.cwd()); // resolve all .babelrc files if (opts.babelrc !== false && filename) { - findConfigs( - path.dirname(filename), - ).forEach(({ filepath, dirname, options }) => { - builder.mergeConfig({ - type: "options", - options, - alias: filepath, - dirname, - }); - }); + findConfigs(path.dirname(filename)).forEach(configFile => + builder.mergeConfigFile(configFile), + ); } } catch (e) { if (e.code !== "BABEL_IGNORED_FILE") throw e; @@ -65,6 +53,26 @@ class ConfigChainBuilder { this.file = file; } + mergeConfigArguments(opts, dirname) { + this.mergeConfig({ + type: "arguments", + options: opts, + alias: "base", + dirname, + }); + } + + mergeConfigFile(file: ConfigFile) { + const { filepath, dirname, options } = file; + + this.mergeConfig({ + type: "options", + options, + alias: filepath, + dirname, + }); + } + mergeConfig({ type, options: rawOpts, alias, dirname }) { if (rawOpts.ignore != null && !Array.isArray(rawOpts.ignore)) { throw new Error( diff --git a/packages/babel-core/src/config/loading/files/configuration.js b/packages/babel-core/src/config/loading/files/configuration.js index 861ab73039..09bd840230 100644 --- a/packages/babel-core/src/config/loading/files/configuration.js +++ b/packages/babel-core/src/config/loading/files/configuration.js @@ -10,7 +10,7 @@ import { makeStrongCache } from "../../caching"; const debug = buildDebug("babel:config:loading:files:configuration"); -type ConfigFile = { +export type ConfigFile = { filepath: string, dirname: string, options: {}, diff --git a/packages/babel-core/src/config/loading/files/index-browser.js b/packages/babel-core/src/config/loading/files/index-browser.js index 79d5325291..2c8f80d79f 100644 --- a/packages/babel-core/src/config/loading/files/index-browser.js +++ b/packages/babel-core/src/config/loading/files/index-browser.js @@ -1,6 +1,6 @@ // @flow -type ConfigFile = { +export type ConfigFile = { filepath: string, dirname: string, options: {},