Skip to content

Commit

Permalink
Add support for .cjs config files (#10599)
Browse files Browse the repository at this point in the history
* Remove duplicate config loading logic and errors

* Add support for .cjs config files

* Add tests

* [tests] Fallback for fs.promises on node 6
  • Loading branch information
nicolo-ribaudo committed Nov 3, 2019
1 parent 58a646b commit bea1b0d
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 214 deletions.
108 changes: 41 additions & 67 deletions packages/babel-core/src/config/files/configuration.js
Expand Up @@ -18,26 +18,22 @@ import type { CallerMetadata } from "../validation/options";

const debug = buildDebug("babel:config:loading:files:configuration");

const BABEL_CONFIG_JS_FILENAME = "babel.config.js";
const BABEL_CONFIG_JSON_FILENAME = "babel.config.json";
const ROOT_CONFIG_FILENAMES = [
BABEL_CONFIG_JS_FILENAME,
BABEL_CONFIG_JSON_FILENAME,
"babel.config.js",
"babel.config.cjs",
"babel.config.json",
];
const RELATIVE_CONFIG_FILENAMES = [".babelrc", ".babelrc.js", ".babelrc.cjs"];

const BABELRC_FILENAME = ".babelrc";
const BABELRC_JS_FILENAME = ".babelrc.js";
const BABELIGNORE_FILENAME = ".babelignore";

export function findConfigUpwards(rootDir: string): string | null {
let dirname = rootDir;
while (true) {
if (
fs.existsSync(path.join(dirname, BABEL_CONFIG_JS_FILENAME)) ||
fs.existsSync(path.join(dirname, BABEL_CONFIG_JSON_FILENAME))
) {
return dirname;
}
const configFileFound = ROOT_CONFIG_FILENAMES.some(filename =>
fs.existsSync(path.join(dirname, filename)),
);
if (configFileFound) return dirname;

const nextDir = path.dirname(dirname);
if (dirname === nextDir) break;
Expand All @@ -59,45 +55,15 @@ export function findRelativeConfig(

for (const loc of packageData.directories) {
if (!config) {
config = [BABELRC_FILENAME, BABELRC_JS_FILENAME].reduce(
(previousConfig: ConfigFile | null, name) => {
const filepath = path.join(loc, name);
const config = readConfig(filepath, envName, caller);

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

const pkgConfig =
config = loadOneConfig(
RELATIVE_CONFIG_FILENAMES,
loc,
envName,
caller,
packageData.pkg && packageData.pkg.dirname === loc
? packageToBabelConfig(packageData.pkg)
: null;

if (pkgConfig) {
if (config) {
throw new Error(
`Multiple configuration files found. Please remove one:\n` +
` - ${path.basename(pkgConfig.filepath)}#babel\n` +
` - ${path.basename(config.filepath)}\n` +
`from ${loc}`,
);
}
config = pkgConfig;
}

if (config) {
debug("Found configuration %o from %o.", config.filepath, dirname);
}
: null,
);
}

if (!ignore) {
Expand All @@ -118,24 +84,31 @@ export function findRootConfig(
envName: string,
caller: CallerMetadata | void,
): ConfigFile | null {
const config = ROOT_CONFIG_FILENAMES.reduce(
(previousConfig: ConfigFile | null, name) => {
const filepath = path.resolve(dirname, name);
const config = readConfig(filepath, envName, caller);

if (config && previousConfig) {
throw new Error(
`Multiple configuration files found. Please remove one:\n` +
` - ${path.basename(previousConfig.filepath)}\n` +
` - ${name}\n` +
`from ${dirname}`,
);
}
return loadOneConfig(ROOT_CONFIG_FILENAMES, dirname, envName, caller);
}

function loadOneConfig(
names: string[],
dirname: string,
envName: string,
caller: CallerMetadata | void,
previousConfig?: ConfigFile | null = null,
): ConfigFile | null {
const config = names.reduce((previousConfig: ConfigFile | null, name) => {
const filepath = path.resolve(dirname, name);
const config = readConfig(filepath, envName, caller);

if (config && previousConfig) {
throw new Error(
`Multiple configuration files found. Please remove one:\n` +
` - ${path.basename(previousConfig.filepath)}\n` +
` - ${name}\n` +
`from ${dirname}`,
);
}

return config || previousConfig;
},
null,
);
return config || previousConfig;
}, previousConfig);

if (config) {
debug("Found configuration %o from %o.", config.filepath, dirname);
Expand Down Expand Up @@ -165,7 +138,8 @@ export function loadConfig(
* throw if there are parsing errors while loading a config.
*/
function readConfig(filepath, envName, caller): ConfigFile | null {
return path.extname(filepath) === ".js"
const ext = path.extname(filepath);
return ext === ".js" || ext === ".cjs"
? readConfigJS(filepath, { envName, caller })
: readConfigJSON5(filepath);
}
Expand Down

0 comments on commit bea1b0d

Please sign in to comment.