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 babel.config.json #10501

Merged
merged 3 commits into from Oct 29, 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
36 changes: 30 additions & 6 deletions packages/babel-core/src/config/files/configuration.js
Expand Up @@ -19,6 +19,11 @@ 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,
];

const BABELRC_FILENAME = ".babelrc";
const BABELRC_JS_FILENAME = ".babelrc.js";
Expand All @@ -27,7 +32,10 @@ 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))) {
if (
fs.existsSync(path.join(dirname, BABEL_CONFIG_JS_FILENAME)) ||
fs.existsSync(path.join(dirname, BABEL_CONFIG_JSON_FILENAME))
) {
return dirname;
}

Expand Down Expand Up @@ -110,13 +118,29 @@ export function findRootConfig(
envName: string,
caller: CallerMetadata | void,
): ConfigFile | null {
const filepath = path.resolve(dirname, BABEL_CONFIG_JS_FILENAME);
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}`,
);
}

const conf = readConfig(filepath, envName, caller);
if (conf) {
debug("Found root config %o in %o.", BABEL_CONFIG_JS_FILENAME, dirname);
return config || previousConfig;
},
null,
);

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

export function loadConfig(
Expand Down
46 changes: 46 additions & 0 deletions packages/babel-core/test/config-chain.js
Expand Up @@ -944,6 +944,52 @@ describe("buildConfigChain", function() {
}
});

it("should load babel.config.json", () => {
const filename = fixture("config-files", "babel-config-json", "src.js");

expect(
loadOptions({
filename,
cwd: path.dirname(filename),
}),
).toEqual({
...getDefaults(),
filename: filename,
cwd: path.dirname(filename),
root: path.dirname(filename),
comments: true,
});
});

it("should load babel.config.js", () => {
const filename = fixture("config-files", "babel-config-js", "src.js");

expect(
loadOptions({
filename,
cwd: path.dirname(filename),
}),
).toEqual({
...getDefaults(),
filename: filename,
cwd: path.dirname(filename),
root: path.dirname(filename),
comments: true,
});
});

it("should whtow if both babel.config.json and babel.config.js are used", () => {
const filename = fixture(
"config-files",
"babel-config-js-and-json",
"src.js",
);

expect(() =>
loadOptions({ filename, cwd: path.dirname(filename) }),
).toThrow(/Multiple configuration files found/);
});

it("should load .babelrc", () => {
const filename = fixture("config-files", "babelrc", "src.js");

Expand Down
@@ -0,0 +1,3 @@
module.exports = {
comments: true
};
@@ -0,0 +1,3 @@
{
"comments": true
}
@@ -0,0 +1,3 @@
module.exports = {
comments: true
};
@@ -0,0 +1,3 @@
{
"comments": true
}