Centralize config processing in class.

This commit is contained in:
Logan Smyth 2017-09-26 18:29:28 -07:00
parent 2d7cda4d28
commit 9a4b764bde
3 changed files with 27 additions and 19 deletions

View File

@ -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<ConfigItem> | 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(

View File

@ -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: {},

View File

@ -1,6 +1,6 @@
// @flow
type ConfigFile = {
export type ConfigFile = {
filepath: string,
dirname: string,
options: {},