Merge .babelrc and .babelignore searching into a single pass.

This commit is contained in:
Logan Smyth 2018-02-06 22:27:03 -08:00
parent d88173b9f8
commit 9a8ba76e1f
3 changed files with 55 additions and 70 deletions

View File

@ -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);

View File

@ -21,41 +21,58 @@ export type IgnoreFile = {
ignore: Array<string>,
};
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);

View File

@ -12,16 +12,16 @@ export type IgnoreFile = {
ignore: Array<string>,
};
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 {