From 9a8ba76e1f8e3c74b93c785559f87e06bf07b400 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 6 Feb 2018 22:27:03 -0800 Subject: [PATCH] Merge .babelrc and .babelignore searching into a single pass. --- .../babel-core/src/config/config-chain.js | 26 ++---- .../src/config/files/configuration.js | 83 +++++++++---------- .../src/config/files/index-browser.js | 16 ++-- 3 files changed, 55 insertions(+), 70 deletions(-) diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index 3316ce1c3b..81adcdff79 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -12,12 +12,7 @@ import { const debug = buildDebug("babel:config:config-chain"); -import { - findBabelrc, - findBabelignore, - loadConfig, - type ConfigFile, -} from "./files"; +import { findRelativeConfig, loadConfig, type ConfigFile } from "./files"; import { makeWeakCache, makeStrongCache } from "./caching"; @@ -124,22 +119,15 @@ export function buildRootChain( // resolve all .babelrc files if (opts.babelrc !== false && context.filename !== null) { const filename = context.filename; - const babelignoreFile = findBabelignore(filename); - if ( - babelignoreFile && - shouldIgnore( - context, - babelignoreFile.ignore, - null, - babelignoreFile.dirname, - ) - ) { + + const { ignore, config } = findRelativeConfig(filename, context.envName); + + if (ignore && shouldIgnore(context, ignore.ignore, null, ignore.dirname)) { return null; } - const babelrcFile = findBabelrc(filename, context.envName); - if (babelrcFile) { - const result = loadFileChain(babelrcFile, context); + if (config) { + const result = loadFileChain(config, context); if (!result) return null; mergeChain(fileChain, result); diff --git a/packages/babel-core/src/config/files/configuration.js b/packages/babel-core/src/config/files/configuration.js index 5617f3e473..73786b89fb 100644 --- a/packages/babel-core/src/config/files/configuration.js +++ b/packages/babel-core/src/config/files/configuration.js @@ -21,41 +21,58 @@ export type IgnoreFile = { ignore: Array, }; +export type RelativeConfig = { + config: ConfigFile | null, + ignore: IgnoreFile | null, +}; + const BABELRC_FILENAME = ".babelrc"; const BABELRC_JS_FILENAME = ".babelrc.js"; const PACKAGE_FILENAME = "package.json"; const BABELIGNORE_FILENAME = ".babelignore"; -export function findBabelrc( +export function findRelativeConfig( filepath: string, envName: string, -): ConfigFile | null { +): RelativeConfig { + let config = null; + let ignore = null; + const dirname = path.dirname(filepath); let loc = dirname; while (true) { - const conf = [ - BABELRC_FILENAME, - BABELRC_JS_FILENAME, - PACKAGE_FILENAME, - ].reduce((previousConfig: ConfigFile | null, name) => { - const filepath = path.join(loc, name); - const config = readConfig(filepath, envName); + if (!config) { + config = [BABELRC_FILENAME, BABELRC_JS_FILENAME, PACKAGE_FILENAME].reduce( + (previousConfig: ConfigFile | null, name) => { + const filepath = path.join(loc, name); + const config = readConfig(filepath, envName); - if (config && previousConfig) { - throw new Error( - `Multiple configuration files found. Please remove one:\n` + - ` - ${path.basename(previousConfig.filepath)}\n` + - ` - ${name}\n` + - `from ${loc}`, - ); + if (config && previousConfig) { + throw new Error( + `Multiple configuration files found. Please remove one:\n` + + ` - ${path.basename(previousConfig.filepath)}\n` + + ` - ${name}\n` + + `from ${loc}`, + ); + } + + return config || previousConfig; + }, + null, + ); + + if (config) { + debug("Found configuration %o from %o.", config.filepath, dirname); } + } - return config || previousConfig; - }, null); + if (!ignore) { + const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME); + ignore = readIgnoreConfig(ignoreLoc); - if (conf) { - debug("Found configuration %o from %o.", conf.filepath, dirname); - return conf; + if (ignore) { + debug("Found ignore %o from %o.", ignore.filepath, dirname); + } } const nextLoc = path.dirname(loc); @@ -63,27 +80,7 @@ export function findBabelrc( loc = nextLoc; } - return null; -} - -export function findBabelignore(filepath: string): IgnoreFile | null { - const dirname = path.dirname(filepath); - let loc = dirname; - while (true) { - const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME); - const ignore = readIgnoreConfig(ignoreLoc); - - if (ignore) { - debug("Found ignore %o from %o.", ignore.filepath, dirname); - return ignore; - } - - const nextLoc = path.dirname(loc); - if (loc === nextLoc) break; - loc = nextLoc; - } - - return null; + return { config, ignore }; } export function loadConfig( @@ -106,7 +103,7 @@ export function loadConfig( * Read the given config file, returning the result. Returns null if no config was found, but will * throw if there are parsing errors while loading a config. */ -function readConfig(filepath, envName) { +function readConfig(filepath, envName): ConfigFile | null { return path.extname(filepath) === ".js" ? readConfigJS(filepath, { envName }) : readConfigFile(filepath); diff --git a/packages/babel-core/src/config/files/index-browser.js b/packages/babel-core/src/config/files/index-browser.js index bcd6b439a0..07b8079568 100644 --- a/packages/babel-core/src/config/files/index-browser.js +++ b/packages/babel-core/src/config/files/index-browser.js @@ -12,16 +12,16 @@ export type IgnoreFile = { ignore: Array, }; -export function findBabelrc( +export type RelativeConfig = { + config: ConfigFile | null, + ignore: IgnoreFile | null, +}; + +export function findRelativeConfig( filepath: string, envName: string, // eslint-disable-line no-unused-vars -): ConfigFile | null { - return null; -} - -// eslint-disable-next-line no-unused-vars -export function findBabelignore(filepath: string): IgnoreFile | null { - return null; +): RelativeConfig { + return { config: null, ignore: null }; } export function loadConfig(name: string, dirname: string): ConfigFile {