From de634437627969a455c3442c5c46580cd3f061f9 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 19 Dec 2017 20:21:13 -0800 Subject: [PATCH] Split babelrc and babelignore searching into two functions. --- .../src/config/build-config-chain.js | 22 +++- .../src/config/loading/files/configuration.js | 105 ++++++++++-------- .../src/config/loading/files/index-browser.js | 17 ++- 3 files changed, 89 insertions(+), 55 deletions(-) diff --git a/packages/babel-core/src/config/build-config-chain.js b/packages/babel-core/src/config/build-config-chain.js index 39c3bb2088..452490ac48 100644 --- a/packages/babel-core/src/config/build-config-chain.js +++ b/packages/babel-core/src/config/build-config-chain.js @@ -16,7 +16,8 @@ const debug = buildDebug("babel:config:config-chain"); import { loadPlugin, loadPreset, - findConfigs, + findBabelrc, + findBabelignore, loadConfig, type ConfigFile, } from "./loading/files"; @@ -114,9 +115,22 @@ export function buildRootChain( // resolve all .babelrc files if (opts.babelrc !== false && context.filename !== null) { - findConfigs(path.dirname(context.filename), envName).forEach(configFile => - builder.mergeConfigFile(configFile), - ); + const filename = context.filename; + const babelrcFile = findBabelrc(filename, context.envName); + if (babelrcFile) builder.mergeConfigFile(babelrcFile); + + const babelignoreFile = findBabelignore(filename); + if ( + babelignoreFile && + shouldIgnore( + context, + babelignoreFile.ignore, + null, + babelignoreFile.dirname, + ) + ) { + return null; + } } } catch (e) { if (e.code !== "BABEL_IGNORED_FILE") throw e; diff --git a/packages/babel-core/src/config/loading/files/configuration.js b/packages/babel-core/src/config/loading/files/configuration.js index a8caa01cdc..061f4ce3d7 100644 --- a/packages/babel-core/src/config/loading/files/configuration.js +++ b/packages/babel-core/src/config/loading/files/configuration.js @@ -15,68 +15,75 @@ export type ConfigFile = { options: {}, }; +export type IgnoreFile = { + filepath: string, + dirname: string, + ignore: Array, +}; + const BABELRC_FILENAME = ".babelrc"; const BABELRC_JS_FILENAME = ".babelrc.js"; const PACKAGE_FILENAME = "package.json"; const BABELIGNORE_FILENAME = ".babelignore"; -export function findConfigs( - dirname: string, +export function findBabelrc( + filepath: string, envName: string, -): Array { - let foundConfig = false; - let foundIgnore = false; - - const confs = []; - +): ConfigFile | null { + const dirname = path.dirname(filepath); let loc = dirname; while (true) { - if (!foundIgnore) { - const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME); - const ignore = readIgnoreConfig(ignoreLoc); + 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 (ignore) { - debug("Found ignore %o from %o.", ignore.filepath, dirname); - confs.push(ignore); - foundIgnore = true; + 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 (conf) { + debug("Found configuration %o from %o.", conf.filepath, dirname); + return conf; } - if (!foundConfig) { - 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 && previousConfig) { - throw new Error( - `Multiple configuration files found. Please remove one:\n- ${path.basename( - previousConfig.filepath, - )}\n- ${name}\nfrom ${loc}`, - ); - } - - return config || previousConfig; - }, null); - - if (conf) { - debug("Found configuration %o from %o.", conf.filepath, dirname); - confs.push(conf); - foundConfig = true; - } - } - - if (foundIgnore && foundConfig) break; - - if (loc === path.dirname(loc)) break; - - loc = path.dirname(loc); + const nextLoc = path.dirname(loc); + if (loc === nextLoc) break; + loc = nextLoc; } - return confs; + 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; } export function loadConfig( @@ -224,7 +231,7 @@ const readIgnoreConfig = makeStaticFileCache((filepath, content) => { return { filepath, dirname: path.dirname(filepath), - options: { ignore }, + ignore, }; }); 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 1d0b249225..bcd6b439a0 100644 --- a/packages/babel-core/src/config/loading/files/index-browser.js +++ b/packages/babel-core/src/config/loading/files/index-browser.js @@ -6,9 +6,22 @@ export type ConfigFile = { options: {}, }; +export type IgnoreFile = { + filepath: string, + dirname: string, + ignore: Array, +}; + +export function findBabelrc( + filepath: string, + envName: string, // eslint-disable-line no-unused-vars +): ConfigFile | null { + return null; +} + // eslint-disable-next-line no-unused-vars -export function findConfigs(dirname: string): Array { - return []; +export function findBabelignore(filepath: string): IgnoreFile | null { + return null; } export function loadConfig(name: string, dirname: string): ConfigFile {