From c6d2f45cab257acbe9f1bea25d1ce60b8971f036 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Mon, 5 Nov 2018 08:51:27 -0800 Subject: [PATCH] Resolve babel.config.js 'babelrcRoots' values relative to the config file. (#8910) --- packages/babel-core/src/config/config-chain.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index b9e13a130797..1dcb7b83fdba 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -155,6 +155,7 @@ export function buildRootChain( } let { babelrc, babelrcRoots } = opts; + let babelrcRootsDirectory = context.cwd; const configFileChain = emptyChain(); if (configFile) { @@ -168,6 +169,7 @@ export function buildRootChain( babelrc = validatedFile.options.babelrc; } if (babelrcRoots === undefined) { + babelrcRootsDirectory = validatedFile.dirname; babelrcRoots = validatedFile.options.babelrcRoots; } @@ -185,7 +187,7 @@ export function buildRootChain( if ( (babelrc === true || babelrc === undefined) && pkgData && - babelrcLoadEnabled(context, pkgData, babelrcRoots) + babelrcLoadEnabled(context, pkgData, babelrcRoots, babelrcRootsDirectory) ) { ({ ignore: ignoreFile, config: babelrcFile } = findRelativeConfig( pkgData, @@ -229,6 +231,7 @@ function babelrcLoadEnabled( context: ConfigContext, pkgData: FilePackageData, babelrcRoots: BabelrcSearch | void, + babelrcRootsDirectory: string, ): boolean { if (typeof babelrcRoots === "boolean") return babelrcRoots; @@ -243,7 +246,9 @@ function babelrcLoadEnabled( let babelrcPatterns = babelrcRoots; if (!Array.isArray(babelrcPatterns)) babelrcPatterns = [babelrcPatterns]; babelrcPatterns = babelrcPatterns.map(pat => { - return typeof pat === "string" ? path.resolve(context.cwd, pat) : pat; + return typeof pat === "string" + ? path.resolve(babelrcRootsDirectory, pat) + : pat; }); // Fast path to avoid having to match patterns if the babelrc is just @@ -253,10 +258,12 @@ function babelrcLoadEnabled( } return babelrcPatterns.some(pat => { - if (typeof pat === "string") pat = pathPatternToRegex(pat, context.cwd); + if (typeof pat === "string") { + pat = pathPatternToRegex(pat, babelrcRootsDirectory); + } return pkgData.directories.some(directory => { - return matchPattern(pat, context.cwd, directory, context); + return matchPattern(pat, babelrcRootsDirectory, directory, context); }); }); }