Only search for .babelrc files in the 'root' package by default.

This commit is contained in:
Logan Smyth 2018-02-06 23:22:20 -08:00
parent 485e37fcb0
commit a67eb25547
7 changed files with 181 additions and 46 deletions

View File

@ -8,6 +8,7 @@ import {
type ValidatedOptions, type ValidatedOptions,
type IgnoreList, type IgnoreList,
type ConfigApplicableTest, type ConfigApplicableTest,
type BabelrcSearch,
} from "./validation/options"; } from "./validation/options";
const debug = buildDebug("babel:config:config-chain"); const debug = buildDebug("babel:config:config-chain");
@ -19,6 +20,7 @@ import {
loadConfig, loadConfig,
type ConfigFile, type ConfigFile,
type IgnoreFile, type IgnoreFile,
type FilePackageData,
} from "./files"; } from "./files";
import { makeWeakCache, makeStrongCache } from "./caching"; import { makeWeakCache, makeStrongCache } from "./caching";
@ -128,12 +130,16 @@ export function buildRootChain(
); );
if (!programmaticChain) return null; if (!programmaticChain) return null;
const { root: rootDir = ".", configFile: configFileName } = opts; const {
root: rootDir = ".",
babelrc = undefined,
configFile: configFileName = true,
} = opts;
let configFile; let configFile;
if (typeof configFileName === "string") { if (typeof configFileName === "string") {
configFile = loadConfig(configFileName, context.cwd, context.envName); configFile = loadConfig(configFileName, context.cwd, context.envName);
} else if (configFileName === undefined || configFileName === true) { } else if (configFileName === true) {
configFile = findRootConfig( configFile = findRootConfig(
path.resolve(context.cwd, rootDir), path.resolve(context.cwd, rootDir),
context.envName, context.envName,
@ -153,21 +159,24 @@ export function buildRootChain(
? findPackageData(context.filename) ? findPackageData(context.filename)
: null; : null;
let ignore, babelrc; let ignoreFile, babelrcFile;
const fileChain = emptyChain(); const fileChain = emptyChain();
// resolve all .babelrc files // resolve all .babelrc files
if (opts.babelrc !== false && pkgData) { if (pkgData && babelrcLoadEnabled(context, pkgData, babelrc, rootDir)) {
({ ignore, config: babelrc } = findRelativeConfig( ({ ignore: ignoreFile, config: babelrcFile } = findRelativeConfig(
pkgData, pkgData,
context.envName, context.envName,
)); ));
if (ignore && shouldIgnore(context, ignore.ignore, null, ignore.dirname)) { if (
ignoreFile &&
shouldIgnore(context, ignoreFile.ignore, null, ignoreFile.dirname)
) {
return null; return null;
} }
if (babelrc) { if (babelrcFile) {
const result = loadFileChain(babelrc, context); const result = loadFileChain(babelrcFile, context);
if (!result) return null; if (!result) return null;
mergeChain(fileChain, result); mergeChain(fileChain, result);
@ -185,12 +194,39 @@ export function buildRootChain(
plugins: dedupDescriptors(chain.plugins), plugins: dedupDescriptors(chain.plugins),
presets: dedupDescriptors(chain.presets), presets: dedupDescriptors(chain.presets),
options: chain.options.map(o => normalizeOptions(o)), options: chain.options.map(o => normalizeOptions(o)),
ignore: ignore || undefined, ignore: ignoreFile || undefined,
babelrc: babelrc || undefined, babelrc: babelrcFile || undefined,
config: configFile || undefined, config: configFile || undefined,
}; };
} }
function babelrcLoadEnabled(
context: ConfigContext,
pkgData: FilePackageData,
babelrc: BabelrcSearch | void,
rootDir: string,
): boolean {
if (typeof babelrc === "boolean") return babelrc;
const absoluteRoot = path.resolve(context.cwd, rootDir);
// Fast path to avoid having to load micromatch if the babelrc is just
// loading in the standard root directory.
if (
babelrc === undefined ||
babelrc === rootDir ||
(Array.isArray(babelrc) && babelrc.length === 1 && babelrc[0] === rootDir)
) {
return pkgData.directories.indexOf(absoluteRoot) !== -1;
}
const babelrcRoots = (Array.isArray(babelrc) ? babelrc : [babelrc]).map(pat =>
path.resolve(context.cwd, pat),
);
return micromatch(pkgData.directories, babelrcRoots).length > 0;
}
/** /**
* Build a config chain for just the programmatic options passed into Babel. * Build a config chain for just the programmatic options passed into Babel.
*/ */

View File

@ -2,6 +2,7 @@
import type { import type {
ConfigFileSearch, ConfigFileSearch,
BabelrcSearch,
IgnoreList, IgnoreList,
IgnoreItem, IgnoreItem,
PluginList, PluginList,
@ -183,6 +184,27 @@ export function assertConfigFileSearch(
return value; return value;
} }
export function assertBabelrcSearch(
key: string,
value: mixed,
): BabelrcSearch | void {
if (value === undefined || typeof value === "boolean") return value;
if (Array.isArray(value)) {
value.forEach((item, i) => {
if (typeof item !== "string") {
throw new Error(`.${key}[${i}] must be a string.`);
}
});
} else if (typeof value !== "string") {
throw new Error(
`.${key} must be a undefined, a boolean, a string, ` +
`or an array of strings, got ${JSON.stringify(value)}`,
);
}
return (value: any);
}
export function assertPluginList(key: string, value: mixed): PluginList | void { export function assertPluginList(key: string, value: mixed): PluginList | void {
const arr = assertArray(key, value); const arr = assertArray(key, value);
if (arr) { if (arr) {

View File

@ -14,6 +14,7 @@ import {
assertPluginList, assertPluginList,
assertConfigApplicableTest, assertConfigApplicableTest,
assertConfigFileSearch, assertConfigFileSearch,
assertBabelrcSearch,
assertFunction, assertFunction,
assertSourceMaps, assertSourceMaps,
assertCompact, assertCompact,
@ -35,7 +36,7 @@ const ROOT_VALIDATORS: ValidatorSet = {
filenameRelative: (assertString: Validator< filenameRelative: (assertString: Validator<
$PropertyType<ValidatedOptions, "filenameRelative">, $PropertyType<ValidatedOptions, "filenameRelative">,
>), >),
babelrc: (assertBoolean: Validator< babelrc: (assertBabelrcSearch: Validator<
$PropertyType<ValidatedOptions, "babelrc">, $PropertyType<ValidatedOptions, "babelrc">,
>), >),
code: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "code">>), code: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "code">>),
@ -156,7 +157,7 @@ export type ValidatedOptions = {
cwd?: string, cwd?: string,
filename?: string, filename?: string,
filenameRelative?: string, filenameRelative?: string,
babelrc?: boolean, babelrc?: BabelrcSearch,
code?: boolean, code?: boolean,
configFile?: ConfigFileSearch, configFile?: ConfigFileSearch,
root?: string, root?: string,
@ -232,6 +233,7 @@ export type OverridesList = Array<ValidatedOptions>;
export type ConfigApplicableTest = IgnoreItem | Array<IgnoreItem>; export type ConfigApplicableTest = IgnoreItem | Array<IgnoreItem>;
export type ConfigFileSearch = string | boolean; export type ConfigFileSearch = string | boolean;
export type BabelrcSearch = boolean | string | Array<string>;
export type SourceMapsOption = boolean | "inline" | "both"; export type SourceMapsOption = boolean | "inline" | "both";
export type SourceTypeOption = "module" | "script" | "unambiguous"; export type SourceTypeOption = "module" | "script" | "unambiguous";
export type CompactOption = boolean | "auto"; export type CompactOption = boolean | "auto";

View File

@ -294,6 +294,7 @@ describe("api", function() {
process.env.BABEL_ENV = "development"; process.env.BABEL_ENV = "development";
const result = babel.transform("", { const result = babel.transform("", {
cwd: path.join(__dirname, "fixtures", "config", "complex-plugin-config"),
filename: path.join( filename: path.join(
__dirname, __dirname,
"fixtures", "fixtures",

View File

@ -11,6 +11,7 @@ describe("buildConfigChain", function() {
describe("single", () => { describe("single", () => {
it("should process matching string values", () => { it("should process matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: fixture("nonexistant-fake"), test: fixture("nonexistant-fake"),
@ -22,6 +23,7 @@ describe("buildConfigChain", function() {
it("should process matching RegExp values", () => { it("should process matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: new RegExp(fixture("nonexistant-fake")), test: new RegExp(fixture("nonexistant-fake")),
@ -33,6 +35,7 @@ describe("buildConfigChain", function() {
it("should process matching function values", () => { it("should process matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: p => p.indexOf(fixture("nonexistant-fake")) === 0, test: p => p.indexOf(fixture("nonexistant-fake")) === 0,
@ -44,6 +47,7 @@ describe("buildConfigChain", function() {
it("should process non-matching string values", () => { it("should process non-matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: fixture("nonexistant-fake-unknown"), test: fixture("nonexistant-fake-unknown"),
@ -55,6 +59,7 @@ describe("buildConfigChain", function() {
it("should process non-matching RegExp values", () => { it("should process non-matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: new RegExp(fixture("nonexistant-unknown")), test: new RegExp(fixture("nonexistant-unknown")),
@ -66,6 +71,7 @@ describe("buildConfigChain", function() {
it("should process non-matching function values", () => { it("should process non-matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: p => p.indexOf(fixture("nonexistant-unknown")) === 0, test: p => p.indexOf(fixture("nonexistant-unknown")) === 0,
@ -79,6 +85,7 @@ describe("buildConfigChain", function() {
describe("array", () => { describe("array", () => {
it("should process matching string values", () => { it("should process matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: [fixture("nonexistant-fake")], test: [fixture("nonexistant-fake")],
@ -90,6 +97,7 @@ describe("buildConfigChain", function() {
it("should process matching RegExp values", () => { it("should process matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: [new RegExp(fixture("nonexistant-fake"))], test: [new RegExp(fixture("nonexistant-fake"))],
@ -101,6 +109,7 @@ describe("buildConfigChain", function() {
it("should process matching function values", () => { it("should process matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: [p => p.indexOf(fixture("nonexistant-fake")) === 0], test: [p => p.indexOf(fixture("nonexistant-fake")) === 0],
@ -112,6 +121,7 @@ describe("buildConfigChain", function() {
it("should process non-matching string values", () => { it("should process non-matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: [fixture("nonexistant-fake-unknown")], test: [fixture("nonexistant-fake-unknown")],
@ -123,6 +133,7 @@ describe("buildConfigChain", function() {
it("should process non-matching RegExp values", () => { it("should process non-matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: [new RegExp(fixture("nonexistant-unknown"))], test: [new RegExp(fixture("nonexistant-unknown"))],
@ -134,6 +145,7 @@ describe("buildConfigChain", function() {
it("should process non-matching function values", () => { it("should process non-matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
test: [p => p.indexOf(fixture("nonexistant-unknown")) === 0], test: [p => p.indexOf(fixture("nonexistant-unknown")) === 0],
@ -149,6 +161,7 @@ describe("buildConfigChain", function() {
describe("single", () => { describe("single", () => {
it("should process matching string values", () => { it("should process matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: fixture("nonexistant-fake"), include: fixture("nonexistant-fake"),
@ -160,6 +173,7 @@ describe("buildConfigChain", function() {
it("should process matching RegExp values", () => { it("should process matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: new RegExp(fixture("nonexistant-fake")), include: new RegExp(fixture("nonexistant-fake")),
@ -171,6 +185,7 @@ describe("buildConfigChain", function() {
it("should process matching function values", () => { it("should process matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: p => p.indexOf(fixture("nonexistant-fake")) === 0, include: p => p.indexOf(fixture("nonexistant-fake")) === 0,
@ -182,6 +197,7 @@ describe("buildConfigChain", function() {
it("should process non-matching string values", () => { it("should process non-matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: fixture("nonexistant-fake-unknown"), include: fixture("nonexistant-fake-unknown"),
@ -193,6 +209,7 @@ describe("buildConfigChain", function() {
it("should process non-matching RegExp values", () => { it("should process non-matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: new RegExp(fixture("nonexistant-unknown")), include: new RegExp(fixture("nonexistant-unknown")),
@ -204,6 +221,7 @@ describe("buildConfigChain", function() {
it("should process non-matching function values", () => { it("should process non-matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: p => p.indexOf(fixture("nonexistant-unknown")) === 0, include: p => p.indexOf(fixture("nonexistant-unknown")) === 0,
@ -217,6 +235,7 @@ describe("buildConfigChain", function() {
describe("array", () => { describe("array", () => {
it("should process matching string values", () => { it("should process matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: [fixture("nonexistant-fake")], include: [fixture("nonexistant-fake")],
@ -228,6 +247,7 @@ describe("buildConfigChain", function() {
it("should process matching RegExp values", () => { it("should process matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: [new RegExp(fixture("nonexistant-fake"))], include: [new RegExp(fixture("nonexistant-fake"))],
@ -239,6 +259,7 @@ describe("buildConfigChain", function() {
it("should process matching function values", () => { it("should process matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: [p => p.indexOf(fixture("nonexistant-fake")) === 0], include: [p => p.indexOf(fixture("nonexistant-fake")) === 0],
@ -250,6 +271,7 @@ describe("buildConfigChain", function() {
it("should process non-matching string values", () => { it("should process non-matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: [fixture("nonexistant-fake-unknown")], include: [fixture("nonexistant-fake-unknown")],
@ -261,6 +283,7 @@ describe("buildConfigChain", function() {
it("should process non-matching RegExp values", () => { it("should process non-matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: [new RegExp(fixture("nonexistant-unknown"))], include: [new RegExp(fixture("nonexistant-unknown"))],
@ -272,6 +295,7 @@ describe("buildConfigChain", function() {
it("should process non-matching function values", () => { it("should process non-matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
include: [p => p.indexOf(fixture("nonexistant-unknown")) === 0], include: [p => p.indexOf(fixture("nonexistant-unknown")) === 0],
@ -287,6 +311,7 @@ describe("buildConfigChain", function() {
describe("single", () => { describe("single", () => {
it("should process matching string values", () => { it("should process matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: fixture("nonexistant-fake"), exclude: fixture("nonexistant-fake"),
@ -298,6 +323,7 @@ describe("buildConfigChain", function() {
it("should process matching RegExp values", () => { it("should process matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: new RegExp(fixture("nonexistant-fake")), exclude: new RegExp(fixture("nonexistant-fake")),
@ -309,6 +335,7 @@ describe("buildConfigChain", function() {
it("should process matching function values", () => { it("should process matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: p => p.indexOf(fixture("nonexistant-fake")) === 0, exclude: p => p.indexOf(fixture("nonexistant-fake")) === 0,
@ -320,6 +347,7 @@ describe("buildConfigChain", function() {
it("should process non-matching string values", () => { it("should process non-matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: fixture("nonexistant-fake-unknown"), exclude: fixture("nonexistant-fake-unknown"),
@ -331,6 +359,7 @@ describe("buildConfigChain", function() {
it("should process non-matching RegExp values", () => { it("should process non-matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: new RegExp(fixture("nonexistant-unknown")), exclude: new RegExp(fixture("nonexistant-unknown")),
@ -342,6 +371,7 @@ describe("buildConfigChain", function() {
it("should process non-matching function values", () => { it("should process non-matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: p => p.indexOf(fixture("nonexistant-unknown")) === 0, exclude: p => p.indexOf(fixture("nonexistant-unknown")) === 0,
@ -355,6 +385,7 @@ describe("buildConfigChain", function() {
describe("array", () => { describe("array", () => {
it("should process matching string values", () => { it("should process matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: [fixture("nonexistant-fake")], exclude: [fixture("nonexistant-fake")],
@ -366,6 +397,7 @@ describe("buildConfigChain", function() {
it("should process matching RegExp values", () => { it("should process matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: [new RegExp(fixture("nonexistant-fake"))], exclude: [new RegExp(fixture("nonexistant-fake"))],
@ -377,6 +409,7 @@ describe("buildConfigChain", function() {
it("should process matching function values", () => { it("should process matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: [p => p.indexOf(fixture("nonexistant-fake")) === 0], exclude: [p => p.indexOf(fixture("nonexistant-fake")) === 0],
@ -388,6 +421,7 @@ describe("buildConfigChain", function() {
it("should process non-matching string values", () => { it("should process non-matching string values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: [fixture("nonexistant-fake-unknown")], exclude: [fixture("nonexistant-fake-unknown")],
@ -399,6 +433,7 @@ describe("buildConfigChain", function() {
it("should process non-matching RegExp values", () => { it("should process non-matching RegExp values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: [new RegExp(fixture("nonexistant-unknown"))], exclude: [new RegExp(fixture("nonexistant-unknown"))],
@ -410,6 +445,7 @@ describe("buildConfigChain", function() {
it("should process non-matching function values", () => { it("should process non-matching function values", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
exclude: [p => p.indexOf(fixture("nonexistant-unknown")) === 0], exclude: [p => p.indexOf(fixture("nonexistant-unknown")) === 0],
@ -424,6 +460,7 @@ describe("buildConfigChain", function() {
describe("ignore", () => { describe("ignore", () => {
it("should ignore files that match", () => { it("should ignore files that match", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
ignore: [ ignore: [
@ -441,6 +478,7 @@ describe("buildConfigChain", function() {
it("should not ignore files that don't match", () => { it("should not ignore files that don't match", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
ignore: [ ignore: [
@ -456,6 +494,7 @@ describe("buildConfigChain", function() {
describe("only", () => { describe("only", () => {
it("should ignore files that don't match", () => { it("should ignore files that don't match", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
only: [ only: [
@ -469,6 +508,7 @@ describe("buildConfigChain", function() {
it("should not ignore files that match", () => { it("should not ignore files that match", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
only: [ only: [
@ -484,6 +524,7 @@ describe("buildConfigChain", function() {
describe("ignore/only", () => { describe("ignore/only", () => {
it("should ignore files that match ignore and don't match only", () => { it("should ignore files that match ignore and don't match only", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
ignore: [fixture("nonexistant-fake", "src.js")], ignore: [fixture("nonexistant-fake", "src.js")],
@ -495,6 +536,7 @@ describe("buildConfigChain", function() {
it("should ignore files that match ignore and also only", () => { it("should ignore files that match ignore and also only", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
ignore: [fixture("nonexistant-fake", "src.js")], ignore: [fixture("nonexistant-fake", "src.js")],
@ -506,6 +548,7 @@ describe("buildConfigChain", function() {
it("should not ignore files that match only and not ignore", () => { it("should not ignore files that match only and not ignore", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
only: [fixture("nonexistant-fake", "src.js")], only: [fixture("nonexistant-fake", "src.js")],
@ -516,6 +559,7 @@ describe("buildConfigChain", function() {
it("should not ignore files when no ignore/only are specified", () => { it("should not ignore files when no ignore/only are specified", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
}); });
@ -525,6 +569,7 @@ describe("buildConfigChain", function() {
it("should allow negation of only", () => { it("should allow negation of only", () => {
const opts1 = loadOptions({ const opts1 = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
only: [ only: [
@ -535,6 +580,7 @@ describe("buildConfigChain", function() {
expect(opts1).toBeNull(); expect(opts1).toBeNull();
const opts2 = loadOptions({ const opts2 = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
only: [ only: [
@ -545,6 +591,7 @@ describe("buildConfigChain", function() {
expect(opts2).not.toBeNull(); expect(opts2).not.toBeNull();
const opts3 = loadOptions({ const opts3 = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "folder", "src.js"), filename: fixture("nonexistant-fake", "folder", "src.js"),
babelrc: false, babelrc: false,
only: [ only: [
@ -557,6 +604,7 @@ describe("buildConfigChain", function() {
it("should allow negation of ignore", () => { it("should allow negation of ignore", () => {
const opts1 = loadOptions({ const opts1 = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
ignore: [ ignore: [
@ -568,6 +616,7 @@ describe("buildConfigChain", function() {
// Tests disabled pending https://github.com/babel/babel/issues/6907 // Tests disabled pending https://github.com/babel/babel/issues/6907
// const opts2 = loadOptions({ // const opts2 = loadOptions({
// cwd: fixture("nonexistant-fake"),
// filename: fixture("nonexistant-fake", "src.js"), // filename: fixture("nonexistant-fake", "src.js"),
// babelrc: false, // babelrc: false,
// ignore: [ // ignore: [
@ -578,6 +627,7 @@ describe("buildConfigChain", function() {
// expect(opts2).not.toBeNull(); // expect(opts2).not.toBeNull();
// //
// const opts3 = loadOptions({ // const opts3 = loadOptions({
// cwd: fixture("nonexistant-fake"),
// filename: fixture("nonexistant-fake", "folder", "src.js"), // filename: fixture("nonexistant-fake", "folder", "src.js"),
// babelrc: false, // babelrc: false,
// ignore: [ // ignore: [
@ -718,13 +768,13 @@ describe("buildConfigChain", function() {
"package.json", "package.json",
); );
const opts1 = loadOptions({ filename }); const opts1 = loadOptions({ filename, cwd: path.dirname(filename) });
const opts2 = loadOptions({ filename }); const opts2 = loadOptions({ filename, cwd: path.dirname(filename) });
touch(pkgJSON); touch(pkgJSON);
const opts3 = loadOptions({ filename }); const opts3 = loadOptions({ filename, cwd: path.dirname(filename) });
const opts4 = loadOptions({ filename }); const opts4 = loadOptions({ filename, cwd: path.dirname(filename) });
expect(opts1.plugins).toHaveLength(1); expect(opts1.plugins).toHaveLength(1);
expect(opts2.plugins).toHaveLength(1); expect(opts2.plugins).toHaveLength(1);
@ -752,13 +802,13 @@ describe("buildConfigChain", function() {
".babelrc", ".babelrc",
); );
const opts1 = loadOptions({ filename }); const opts1 = loadOptions({ filename, cwd: path.dirname(filename) });
const opts2 = loadOptions({ filename }); const opts2 = loadOptions({ filename, cwd: path.dirname(filename) });
touch(babelrcFile); touch(babelrcFile);
const opts3 = loadOptions({ filename }); const opts3 = loadOptions({ filename, cwd: path.dirname(filename) });
const opts4 = loadOptions({ filename }); const opts4 = loadOptions({ filename, cwd: path.dirname(filename) });
expect(opts1.plugins).toHaveLength(1); expect(opts1.plugins).toHaveLength(1);
expect(opts2.plugins).toHaveLength(1); expect(opts2.plugins).toHaveLength(1);
@ -780,11 +830,19 @@ describe("buildConfigChain", function() {
"src.js", "src.js",
); );
const opts1 = loadOptions({ filename }); const opts1 = loadOptions({ filename, cwd: path.dirname(filename) });
const opts2 = loadOptions({ filename }); const opts2 = loadOptions({ filename, cwd: path.dirname(filename) });
const opts3 = loadOptions({ filename, envName: "new-env" }); const opts3 = loadOptions({
const opts4 = loadOptions({ filename, envName: "new-env" }); filename,
envName: "new-env",
cwd: path.dirname(filename),
});
const opts4 = loadOptions({
filename,
envName: "new-env",
cwd: path.dirname(filename),
});
expect(opts1.plugins).toHaveLength(1); expect(opts1.plugins).toHaveLength(1);
expect(opts2.plugins).toHaveLength(1); expect(opts2.plugins).toHaveLength(1);
@ -803,6 +861,7 @@ describe("buildConfigChain", function() {
describe("overrides merging", () => { describe("overrides merging", () => {
it("should apply matching overrides over base configs", () => { it("should apply matching overrides over base configs", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
comments: true, comments: true,
@ -819,6 +878,7 @@ describe("buildConfigChain", function() {
it("should not apply non-matching overrides over base configs", () => { it("should not apply non-matching overrides over base configs", () => {
const opts = loadOptions({ const opts = loadOptions({
cwd: fixture("nonexistant-fake"),
filename: fixture("nonexistant-fake", "src.js"), filename: fixture("nonexistant-fake", "src.js"),
babelrc: false, babelrc: false,
comments: true, comments: true,
@ -860,9 +920,15 @@ describe("buildConfigChain", function() {
it("should load .babelrc", () => { it("should load .babelrc", () => {
const filename = fixture("config-files", "babelrc", "src.js"); const filename = fixture("config-files", "babelrc", "src.js");
expect(loadOptions({ filename })).toEqual({ expect(
loadOptions({
filename,
cwd: path.dirname(filename),
}),
).toEqual({
...getDefaults(), ...getDefaults(),
filename, filename,
cwd: path.dirname(filename),
comments: true, comments: true,
}); });
}); });
@ -870,9 +936,10 @@ describe("buildConfigChain", function() {
it("should load .babelrc.js", () => { it("should load .babelrc.js", () => {
const filename = fixture("config-files", "babelrc-js", "src.js"); const filename = fixture("config-files", "babelrc-js", "src.js");
expect(loadOptions({ filename })).toEqual({ expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
...getDefaults(), ...getDefaults(),
filename, filename,
cwd: path.dirname(filename),
comments: true, comments: true,
}); });
}); });
@ -880,9 +947,10 @@ describe("buildConfigChain", function() {
it("should load package.json#babel", () => { it("should load package.json#babel", () => {
const filename = fixture("config-files", "pkg", "src.js"); const filename = fixture("config-files", "pkg", "src.js");
expect(loadOptions({ filename })).toEqual({ expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
...getDefaults(), ...getDefaults(),
filename, filename,
cwd: path.dirname(filename),
comments: true, comments: true,
}); });
}); });
@ -890,39 +958,40 @@ describe("buildConfigChain", function() {
it("should load .babelignore", () => { it("should load .babelignore", () => {
const filename = fixture("config-files", "babelignore", "src.js"); const filename = fixture("config-files", "babelignore", "src.js");
expect(loadOptions({ filename })).toBeNull(); expect(loadOptions({ filename, cwd: path.dirname(filename) })).toBeNull();
}); });
it("should throw if there are both .babelrc and .babelrc.js", () => { it("should throw if there are both .babelrc and .babelrc.js", () => {
const filename = fixture("config-files", "both-babelrc", "src.js"); const filename = fixture("config-files", "both-babelrc", "src.js");
expect(() => loadOptions({ filename })).toThrow( expect(() =>
/Multiple configuration files found/, loadOptions({ filename, cwd: path.dirname(filename) }),
); ).toThrow(/Multiple configuration files found/);
}); });
it("should throw if there are both .babelrc and package.json", () => { it("should throw if there are both .babelrc and package.json", () => {
const filename = fixture("config-files", "pkg-babelrc", "src.js"); const filename = fixture("config-files", "pkg-babelrc", "src.js");
expect(() => loadOptions({ filename })).toThrow( expect(() =>
/Multiple configuration files found/, loadOptions({ filename, cwd: path.dirname(filename) }),
); ).toThrow(/Multiple configuration files found/);
}); });
it("should throw if there are both .babelrc.js and package.json", () => { it("should throw if there are both .babelrc.js and package.json", () => {
const filename = fixture("config-files", "pkg-babelrc-js", "src.js"); const filename = fixture("config-files", "pkg-babelrc-js", "src.js");
expect(() => loadOptions({ filename })).toThrow( expect(() =>
/Multiple configuration files found/, loadOptions({ filename, cwd: path.dirname(filename) }),
); ).toThrow(/Multiple configuration files found/);
}); });
it("should ignore package.json without a 'babel' property", () => { it("should ignore package.json without a 'babel' property", () => {
const filename = fixture("config-files", "pkg-ignored", "src.js"); const filename = fixture("config-files", "pkg-ignored", "src.js");
expect(loadOptions({ filename })).toEqual({ expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
...getDefaults(), ...getDefaults(),
filename, filename,
cwd: path.dirname(filename),
comments: true, comments: true,
}); });
}); });
@ -930,23 +999,25 @@ describe("buildConfigChain", function() {
it("should show helpful errors for .babelrc", () => { it("should show helpful errors for .babelrc", () => {
const filename = fixture("config-files", "babelrc-error", "src.js"); const filename = fixture("config-files", "babelrc-error", "src.js");
expect(() => loadOptions({ filename })).toThrow( expect(() =>
/Error while parsing config - /, loadOptions({ filename, cwd: path.dirname(filename) }),
); ).toThrow(/Error while parsing config - /);
}); });
it("should show helpful errors for .babelrc.js", () => { it("should show helpful errors for .babelrc.js", () => {
const filename = fixture("config-files", "babelrc-js-error", "src.js"); const filename = fixture("config-files", "babelrc-js-error", "src.js");
expect(() => loadOptions({ filename })).toThrow(/Babelrc threw an error/); expect(() =>
loadOptions({ filename, cwd: path.dirname(filename) }),
).toThrow(/Babelrc threw an error/);
}); });
it("should show helpful errors for package.json", () => { it("should show helpful errors for package.json", () => {
const filename = fixture("config-files", "pkg-error", "src.js"); const filename = fixture("config-files", "pkg-error", "src.js");
expect(() => loadOptions({ filename })).toThrow( expect(() =>
/Error while parsing JSON - /, loadOptions({ filename, cwd: path.dirname(filename) }),
); ).toThrow(/Error while parsing JSON - /);
}); });
}); });
}); });

View File

@ -25,6 +25,7 @@ describe("@babel/core config loading", () => {
function makeOpts(skipProgrammatic = false) { function makeOpts(skipProgrammatic = false) {
return { return {
cwd: path.dirname(FILEPATH),
filename: FILEPATH, filename: FILEPATH,
presets: skipProgrammatic presets: skipProgrammatic
? null ? null

View File

@ -52,7 +52,9 @@ const buildTest = opts => {
let args = [binLoc]; let args = [binLoc];
args = args.concat(opts.args); args = args.concat(opts.args);
const spawn = child.spawn(process.execPath, args); const spawn = child.spawn(process.execPath, args, {
cwd: tmpLoc,
});
let stdout = ""; let stdout = "";
let stderr = ""; let stderr = "";