diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index ef607ccdd0..da91fd83e2 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -8,6 +8,7 @@ import { type ValidatedOptions, type IgnoreList, type ConfigApplicableTest, + type BabelrcSearch, } from "./validation/options"; const debug = buildDebug("babel:config:config-chain"); @@ -19,6 +20,7 @@ import { loadConfig, type ConfigFile, type IgnoreFile, + type FilePackageData, } from "./files"; import { makeWeakCache, makeStrongCache } from "./caching"; @@ -128,12 +130,16 @@ export function buildRootChain( ); if (!programmaticChain) return null; - const { root: rootDir = ".", configFile: configFileName } = opts; + const { + root: rootDir = ".", + babelrc = undefined, + configFile: configFileName = true, + } = opts; let configFile; if (typeof configFileName === "string") { configFile = loadConfig(configFileName, context.cwd, context.envName); - } else if (configFileName === undefined || configFileName === true) { + } else if (configFileName === true) { configFile = findRootConfig( path.resolve(context.cwd, rootDir), context.envName, @@ -153,21 +159,24 @@ export function buildRootChain( ? findPackageData(context.filename) : null; - let ignore, babelrc; + let ignoreFile, babelrcFile; const fileChain = emptyChain(); // resolve all .babelrc files - if (opts.babelrc !== false && pkgData) { - ({ ignore, config: babelrc } = findRelativeConfig( + if (pkgData && babelrcLoadEnabled(context, pkgData, babelrc, rootDir)) { + ({ ignore: ignoreFile, config: babelrcFile } = findRelativeConfig( pkgData, context.envName, )); - if (ignore && shouldIgnore(context, ignore.ignore, null, ignore.dirname)) { + if ( + ignoreFile && + shouldIgnore(context, ignoreFile.ignore, null, ignoreFile.dirname) + ) { return null; } - if (babelrc) { - const result = loadFileChain(babelrc, context); + if (babelrcFile) { + const result = loadFileChain(babelrcFile, context); if (!result) return null; mergeChain(fileChain, result); @@ -185,12 +194,39 @@ export function buildRootChain( plugins: dedupDescriptors(chain.plugins), presets: dedupDescriptors(chain.presets), options: chain.options.map(o => normalizeOptions(o)), - ignore: ignore || undefined, - babelrc: babelrc || undefined, + ignore: ignoreFile || undefined, + babelrc: babelrcFile || 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. */ diff --git a/packages/babel-core/src/config/validation/option-assertions.js b/packages/babel-core/src/config/validation/option-assertions.js index b59454e875..a637651700 100644 --- a/packages/babel-core/src/config/validation/option-assertions.js +++ b/packages/babel-core/src/config/validation/option-assertions.js @@ -2,6 +2,7 @@ import type { ConfigFileSearch, + BabelrcSearch, IgnoreList, IgnoreItem, PluginList, @@ -183,6 +184,27 @@ export function assertConfigFileSearch( 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 { const arr = assertArray(key, value); if (arr) { diff --git a/packages/babel-core/src/config/validation/options.js b/packages/babel-core/src/config/validation/options.js index 51fe8c9c0d..e4b5f4cde8 100644 --- a/packages/babel-core/src/config/validation/options.js +++ b/packages/babel-core/src/config/validation/options.js @@ -14,6 +14,7 @@ import { assertPluginList, assertConfigApplicableTest, assertConfigFileSearch, + assertBabelrcSearch, assertFunction, assertSourceMaps, assertCompact, @@ -35,7 +36,7 @@ const ROOT_VALIDATORS: ValidatorSet = { filenameRelative: (assertString: Validator< $PropertyType, >), - babelrc: (assertBoolean: Validator< + babelrc: (assertBabelrcSearch: Validator< $PropertyType, >), code: (assertBoolean: Validator<$PropertyType>), @@ -156,7 +157,7 @@ export type ValidatedOptions = { cwd?: string, filename?: string, filenameRelative?: string, - babelrc?: boolean, + babelrc?: BabelrcSearch, code?: boolean, configFile?: ConfigFileSearch, root?: string, @@ -232,6 +233,7 @@ export type OverridesList = Array; export type ConfigApplicableTest = IgnoreItem | Array; export type ConfigFileSearch = string | boolean; +export type BabelrcSearch = boolean | string | Array; export type SourceMapsOption = boolean | "inline" | "both"; export type SourceTypeOption = "module" | "script" | "unambiguous"; export type CompactOption = boolean | "auto"; diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 101a184089..0977bad100 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -294,6 +294,7 @@ describe("api", function() { process.env.BABEL_ENV = "development"; const result = babel.transform("", { + cwd: path.join(__dirname, "fixtures", "config", "complex-plugin-config"), filename: path.join( __dirname, "fixtures", diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index fc3b344968..8aa83a7481 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -11,6 +11,7 @@ describe("buildConfigChain", function() { describe("single", () => { it("should process matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: fixture("nonexistant-fake"), @@ -22,6 +23,7 @@ describe("buildConfigChain", function() { it("should process matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: new RegExp(fixture("nonexistant-fake")), @@ -33,6 +35,7 @@ describe("buildConfigChain", function() { it("should process matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: p => p.indexOf(fixture("nonexistant-fake")) === 0, @@ -44,6 +47,7 @@ describe("buildConfigChain", function() { it("should process non-matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: fixture("nonexistant-fake-unknown"), @@ -55,6 +59,7 @@ describe("buildConfigChain", function() { it("should process non-matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: new RegExp(fixture("nonexistant-unknown")), @@ -66,6 +71,7 @@ describe("buildConfigChain", function() { it("should process non-matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: p => p.indexOf(fixture("nonexistant-unknown")) === 0, @@ -79,6 +85,7 @@ describe("buildConfigChain", function() { describe("array", () => { it("should process matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: [fixture("nonexistant-fake")], @@ -90,6 +97,7 @@ describe("buildConfigChain", function() { it("should process matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: [new RegExp(fixture("nonexistant-fake"))], @@ -101,6 +109,7 @@ describe("buildConfigChain", function() { it("should process matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: [p => p.indexOf(fixture("nonexistant-fake")) === 0], @@ -112,6 +121,7 @@ describe("buildConfigChain", function() { it("should process non-matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: [fixture("nonexistant-fake-unknown")], @@ -123,6 +133,7 @@ describe("buildConfigChain", function() { it("should process non-matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: [new RegExp(fixture("nonexistant-unknown"))], @@ -134,6 +145,7 @@ describe("buildConfigChain", function() { it("should process non-matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, test: [p => p.indexOf(fixture("nonexistant-unknown")) === 0], @@ -149,6 +161,7 @@ describe("buildConfigChain", function() { describe("single", () => { it("should process matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: fixture("nonexistant-fake"), @@ -160,6 +173,7 @@ describe("buildConfigChain", function() { it("should process matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: new RegExp(fixture("nonexistant-fake")), @@ -171,6 +185,7 @@ describe("buildConfigChain", function() { it("should process matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: p => p.indexOf(fixture("nonexistant-fake")) === 0, @@ -182,6 +197,7 @@ describe("buildConfigChain", function() { it("should process non-matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: fixture("nonexistant-fake-unknown"), @@ -193,6 +209,7 @@ describe("buildConfigChain", function() { it("should process non-matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: new RegExp(fixture("nonexistant-unknown")), @@ -204,6 +221,7 @@ describe("buildConfigChain", function() { it("should process non-matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: p => p.indexOf(fixture("nonexistant-unknown")) === 0, @@ -217,6 +235,7 @@ describe("buildConfigChain", function() { describe("array", () => { it("should process matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: [fixture("nonexistant-fake")], @@ -228,6 +247,7 @@ describe("buildConfigChain", function() { it("should process matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: [new RegExp(fixture("nonexistant-fake"))], @@ -239,6 +259,7 @@ describe("buildConfigChain", function() { it("should process matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: [p => p.indexOf(fixture("nonexistant-fake")) === 0], @@ -250,6 +271,7 @@ describe("buildConfigChain", function() { it("should process non-matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: [fixture("nonexistant-fake-unknown")], @@ -261,6 +283,7 @@ describe("buildConfigChain", function() { it("should process non-matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: [new RegExp(fixture("nonexistant-unknown"))], @@ -272,6 +295,7 @@ describe("buildConfigChain", function() { it("should process non-matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, include: [p => p.indexOf(fixture("nonexistant-unknown")) === 0], @@ -287,6 +311,7 @@ describe("buildConfigChain", function() { describe("single", () => { it("should process matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: fixture("nonexistant-fake"), @@ -298,6 +323,7 @@ describe("buildConfigChain", function() { it("should process matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: new RegExp(fixture("nonexistant-fake")), @@ -309,6 +335,7 @@ describe("buildConfigChain", function() { it("should process matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: p => p.indexOf(fixture("nonexistant-fake")) === 0, @@ -320,6 +347,7 @@ describe("buildConfigChain", function() { it("should process non-matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: fixture("nonexistant-fake-unknown"), @@ -331,6 +359,7 @@ describe("buildConfigChain", function() { it("should process non-matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: new RegExp(fixture("nonexistant-unknown")), @@ -342,6 +371,7 @@ describe("buildConfigChain", function() { it("should process non-matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: p => p.indexOf(fixture("nonexistant-unknown")) === 0, @@ -355,6 +385,7 @@ describe("buildConfigChain", function() { describe("array", () => { it("should process matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: [fixture("nonexistant-fake")], @@ -366,6 +397,7 @@ describe("buildConfigChain", function() { it("should process matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: [new RegExp(fixture("nonexistant-fake"))], @@ -377,6 +409,7 @@ describe("buildConfigChain", function() { it("should process matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: [p => p.indexOf(fixture("nonexistant-fake")) === 0], @@ -388,6 +421,7 @@ describe("buildConfigChain", function() { it("should process non-matching string values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: [fixture("nonexistant-fake-unknown")], @@ -399,6 +433,7 @@ describe("buildConfigChain", function() { it("should process non-matching RegExp values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: [new RegExp(fixture("nonexistant-unknown"))], @@ -410,6 +445,7 @@ describe("buildConfigChain", function() { it("should process non-matching function values", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, exclude: [p => p.indexOf(fixture("nonexistant-unknown")) === 0], @@ -424,6 +460,7 @@ describe("buildConfigChain", function() { describe("ignore", () => { it("should ignore files that match", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, ignore: [ @@ -441,6 +478,7 @@ describe("buildConfigChain", function() { it("should not ignore files that don't match", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, ignore: [ @@ -456,6 +494,7 @@ describe("buildConfigChain", function() { describe("only", () => { it("should ignore files that don't match", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, only: [ @@ -469,6 +508,7 @@ describe("buildConfigChain", function() { it("should not ignore files that match", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, only: [ @@ -484,6 +524,7 @@ describe("buildConfigChain", function() { describe("ignore/only", () => { it("should ignore files that match ignore and don't match only", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, ignore: [fixture("nonexistant-fake", "src.js")], @@ -495,6 +536,7 @@ describe("buildConfigChain", function() { it("should ignore files that match ignore and also only", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, ignore: [fixture("nonexistant-fake", "src.js")], @@ -506,6 +548,7 @@ describe("buildConfigChain", function() { it("should not ignore files that match only and not ignore", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, only: [fixture("nonexistant-fake", "src.js")], @@ -516,6 +559,7 @@ describe("buildConfigChain", function() { it("should not ignore files when no ignore/only are specified", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, }); @@ -525,6 +569,7 @@ describe("buildConfigChain", function() { it("should allow negation of only", () => { const opts1 = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, only: [ @@ -535,6 +580,7 @@ describe("buildConfigChain", function() { expect(opts1).toBeNull(); const opts2 = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, only: [ @@ -545,6 +591,7 @@ describe("buildConfigChain", function() { expect(opts2).not.toBeNull(); const opts3 = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "folder", "src.js"), babelrc: false, only: [ @@ -557,6 +604,7 @@ describe("buildConfigChain", function() { it("should allow negation of ignore", () => { const opts1 = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, ignore: [ @@ -568,6 +616,7 @@ describe("buildConfigChain", function() { // Tests disabled pending https://github.com/babel/babel/issues/6907 // const opts2 = loadOptions({ + // cwd: fixture("nonexistant-fake"), // filename: fixture("nonexistant-fake", "src.js"), // babelrc: false, // ignore: [ @@ -578,6 +627,7 @@ describe("buildConfigChain", function() { // expect(opts2).not.toBeNull(); // // const opts3 = loadOptions({ + // cwd: fixture("nonexistant-fake"), // filename: fixture("nonexistant-fake", "folder", "src.js"), // babelrc: false, // ignore: [ @@ -718,13 +768,13 @@ describe("buildConfigChain", function() { "package.json", ); - const opts1 = loadOptions({ filename }); - const opts2 = loadOptions({ filename }); + const opts1 = loadOptions({ filename, cwd: path.dirname(filename) }); + const opts2 = loadOptions({ filename, cwd: path.dirname(filename) }); touch(pkgJSON); - const opts3 = loadOptions({ filename }); - const opts4 = loadOptions({ filename }); + const opts3 = loadOptions({ filename, cwd: path.dirname(filename) }); + const opts4 = loadOptions({ filename, cwd: path.dirname(filename) }); expect(opts1.plugins).toHaveLength(1); expect(opts2.plugins).toHaveLength(1); @@ -752,13 +802,13 @@ describe("buildConfigChain", function() { ".babelrc", ); - const opts1 = loadOptions({ filename }); - const opts2 = loadOptions({ filename }); + const opts1 = loadOptions({ filename, cwd: path.dirname(filename) }); + const opts2 = loadOptions({ filename, cwd: path.dirname(filename) }); touch(babelrcFile); - const opts3 = loadOptions({ filename }); - const opts4 = loadOptions({ filename }); + const opts3 = loadOptions({ filename, cwd: path.dirname(filename) }); + const opts4 = loadOptions({ filename, cwd: path.dirname(filename) }); expect(opts1.plugins).toHaveLength(1); expect(opts2.plugins).toHaveLength(1); @@ -780,11 +830,19 @@ describe("buildConfigChain", function() { "src.js", ); - const opts1 = loadOptions({ filename }); - const opts2 = loadOptions({ filename }); + const opts1 = loadOptions({ filename, cwd: path.dirname(filename) }); + const opts2 = loadOptions({ filename, cwd: path.dirname(filename) }); - const opts3 = loadOptions({ filename, envName: "new-env" }); - const opts4 = loadOptions({ filename, envName: "new-env" }); + const opts3 = loadOptions({ + 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(opts2.plugins).toHaveLength(1); @@ -803,6 +861,7 @@ describe("buildConfigChain", function() { describe("overrides merging", () => { it("should apply matching overrides over base configs", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, comments: true, @@ -819,6 +878,7 @@ describe("buildConfigChain", function() { it("should not apply non-matching overrides over base configs", () => { const opts = loadOptions({ + cwd: fixture("nonexistant-fake"), filename: fixture("nonexistant-fake", "src.js"), babelrc: false, comments: true, @@ -860,9 +920,15 @@ describe("buildConfigChain", function() { it("should load .babelrc", () => { const filename = fixture("config-files", "babelrc", "src.js"); - expect(loadOptions({ filename })).toEqual({ + expect( + loadOptions({ + filename, + cwd: path.dirname(filename), + }), + ).toEqual({ ...getDefaults(), filename, + cwd: path.dirname(filename), comments: true, }); }); @@ -870,9 +936,10 @@ describe("buildConfigChain", function() { it("should load .babelrc.js", () => { const filename = fixture("config-files", "babelrc-js", "src.js"); - expect(loadOptions({ filename })).toEqual({ + expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({ ...getDefaults(), filename, + cwd: path.dirname(filename), comments: true, }); }); @@ -880,9 +947,10 @@ describe("buildConfigChain", function() { it("should load package.json#babel", () => { const filename = fixture("config-files", "pkg", "src.js"); - expect(loadOptions({ filename })).toEqual({ + expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({ ...getDefaults(), filename, + cwd: path.dirname(filename), comments: true, }); }); @@ -890,39 +958,40 @@ describe("buildConfigChain", function() { it("should load .babelignore", () => { 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", () => { const filename = fixture("config-files", "both-babelrc", "src.js"); - expect(() => loadOptions({ filename })).toThrow( - /Multiple configuration files found/, - ); + expect(() => + loadOptions({ filename, cwd: path.dirname(filename) }), + ).toThrow(/Multiple configuration files found/); }); it("should throw if there are both .babelrc and package.json", () => { const filename = fixture("config-files", "pkg-babelrc", "src.js"); - expect(() => loadOptions({ filename })).toThrow( - /Multiple configuration files found/, - ); + expect(() => + loadOptions({ filename, cwd: path.dirname(filename) }), + ).toThrow(/Multiple configuration files found/); }); it("should throw if there are both .babelrc.js and package.json", () => { const filename = fixture("config-files", "pkg-babelrc-js", "src.js"); - expect(() => loadOptions({ filename })).toThrow( - /Multiple configuration files found/, - ); + expect(() => + loadOptions({ filename, cwd: path.dirname(filename) }), + ).toThrow(/Multiple configuration files found/); }); it("should ignore package.json without a 'babel' property", () => { const filename = fixture("config-files", "pkg-ignored", "src.js"); - expect(loadOptions({ filename })).toEqual({ + expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({ ...getDefaults(), filename, + cwd: path.dirname(filename), comments: true, }); }); @@ -930,23 +999,25 @@ describe("buildConfigChain", function() { it("should show helpful errors for .babelrc", () => { const filename = fixture("config-files", "babelrc-error", "src.js"); - expect(() => loadOptions({ filename })).toThrow( - /Error while parsing config - /, - ); + expect(() => + loadOptions({ filename, cwd: path.dirname(filename) }), + ).toThrow(/Error while parsing config - /); }); it("should show helpful errors for .babelrc.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", () => { const filename = fixture("config-files", "pkg-error", "src.js"); - expect(() => loadOptions({ filename })).toThrow( - /Error while parsing JSON - /, - ); + expect(() => + loadOptions({ filename, cwd: path.dirname(filename) }), + ).toThrow(/Error while parsing JSON - /); }); }); }); diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index c2787f39c2..65eb0c6034 100644 --- a/packages/babel-core/test/config-loading.js +++ b/packages/babel-core/test/config-loading.js @@ -25,6 +25,7 @@ describe("@babel/core config loading", () => { function makeOpts(skipProgrammatic = false) { return { + cwd: path.dirname(FILEPATH), filename: FILEPATH, presets: skipProgrammatic ? null diff --git a/packages/babel-preset-env/test/debug-fixtures.js b/packages/babel-preset-env/test/debug-fixtures.js index d716ae55ba..6c746ed306 100644 --- a/packages/babel-preset-env/test/debug-fixtures.js +++ b/packages/babel-preset-env/test/debug-fixtures.js @@ -52,7 +52,9 @@ const buildTest = opts => { let args = [binLoc]; args = args.concat(opts.args); - const spawn = child.spawn(process.execPath, args); + const spawn = child.spawn(process.execPath, args, { + cwd: tmpLoc, + }); let stdout = ""; let stderr = "";