Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .cjs config files #10599

Merged
merged 4 commits into from Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about .mjs? Would that be an issue in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support configurations which must be loaded asynchronously (like es modules) yet.

"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