diff --git a/packages/babel-core/README.md b/packages/babel-core/README.md index 8a08cf2d94..aeb600c947 100644 --- a/packages/babel-core/README.md +++ b/packages/babel-core/README.md @@ -218,7 +218,8 @@ Following is a table of the options you can use: | `auxiliaryCommentBefore` | `null` | Attach a comment before all non-user injected code | | `root` | `"."` | Specify the "root" folder that defines the location to search for "babel.config.js", and the default folder to allow `.babelrc` files inside of.| | `configFile` | `undefined` | The config file to load Babel's config from. Defaults to searching for "babel.config.js" inside the "root" folder. `false` will disable searching for config files.| -| `babelrc` | `(root)` | Specify whether or not to use .babelrc and .babelignore files. Not available when using the CLI, [use `--no-babelrc` instead](https://babeljs.io/docs/usage/cli/#babel-ignoring-babelrc). `false` to disable searching, and `true` to always search, a string path of the package to search inside of, or an array of paths to packages to search inside of. | +| `babelrc` | `true` | Specify whether or not to use .babelrc and .babelignore files. Not available when using the CLI, [use `--no-babelrc` instead](https://babeljs.io/docs/usage/cli/#babel-ignoring-babelrc) | +| `babelrcRoots` | `(root)` | Specify which packages should be search for .babelrc files when they are being compiled. `true` to _always_ search, or a path string or an array of paths to packages to search inside of. Defaults to only searching the "root" package. | | `envName` | env vars | Defaults to environment variable `BABEL_ENV` if set, or else `NODE_ENV` if set, or else it defaults to `"development"` | | `code` | `true` | Enable code generation | | `comments` | `true` | Output comments in generated output | diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index da91fd83e2..1038652bc2 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -132,18 +132,18 @@ export function buildRootChain( const { root: rootDir = ".", - babelrc = undefined, + babelrc = true, + babelrcRoots, configFile: configFileName = true, } = opts; + const absoluteRoot = path.resolve(context.cwd, rootDir); + let configFile; if (typeof configFileName === "string") { configFile = loadConfig(configFileName, context.cwd, context.envName); } else if (configFileName === true) { - configFile = findRootConfig( - path.resolve(context.cwd, rootDir), - context.envName, - ); + configFile = findRootConfig(absoluteRoot, context.envName); } const configFileChain = emptyChain(); @@ -162,7 +162,11 @@ export function buildRootChain( let ignoreFile, babelrcFile; const fileChain = emptyChain(); // resolve all .babelrc files - if (pkgData && babelrcLoadEnabled(context, pkgData, babelrc, rootDir)) { + if ( + babelrc && + pkgData && + babelrcLoadEnabled(context, pkgData, babelrcRoots, absoluteRoot) + ) { ({ ignore: ignoreFile, config: babelrcFile } = findRelativeConfig( pkgData, context.envName, @@ -203,28 +207,28 @@ export function buildRootChain( function babelrcLoadEnabled( context: ConfigContext, pkgData: FilePackageData, - babelrc: BabelrcSearch | void, - rootDir: string, + babelrcRoots: BabelrcSearch | void, + absoluteRoot: string, ): boolean { - if (typeof babelrc === "boolean") return babelrc; - - const absoluteRoot = path.resolve(context.cwd, rootDir); + if (typeof babelrcRoots === "boolean") return babelrcRoots; // 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) - ) { + if (babelrcRoots === undefined) { return pkgData.directories.indexOf(absoluteRoot) !== -1; } - const babelrcRoots = (Array.isArray(babelrc) ? babelrc : [babelrc]).map(pat => - path.resolve(context.cwd, pat), - ); + let babelrcPatterns = babelrcRoots; + if (!Array.isArray(babelrcPatterns)) babelrcPatterns = [babelrcPatterns]; + babelrcPatterns = babelrcPatterns.map(pat => path.resolve(context.cwd, pat)); - return micromatch(pkgData.directories, babelrcRoots).length > 0; + // Fast path to avoid having to load micromatch if the babelrc is just + // loading in the standard root directory. + if (babelrcPatterns.length === 1 && babelrcPatterns[0] === absoluteRoot) { + return pkgData.directories.indexOf(absoluteRoot) !== -1; + } + + return micromatch(pkgData.directories, babelrcPatterns).length > 0; } /** diff --git a/packages/babel-core/src/config/partial.js b/packages/babel-core/src/config/partial.js index e0fa76b81a..2a491f5e69 100644 --- a/packages/babel-core/src/config/partial.js +++ b/packages/babel-core/src/config/partial.js @@ -128,7 +128,7 @@ class PartialConfig { * this.babelrc directly. */ hasFilesystemConfig(): boolean { - return this.babelrc !== undefined; + return this.babelrc !== undefined || this.config !== undefined; } } Object.freeze(PartialConfig.prototype); diff --git a/packages/babel-core/src/config/validation/options.js b/packages/babel-core/src/config/validation/options.js index e4b5f4cde8..4b6024cf03 100644 --- a/packages/babel-core/src/config/validation/options.js +++ b/packages/babel-core/src/config/validation/options.js @@ -36,9 +36,12 @@ const ROOT_VALIDATORS: ValidatorSet = { filenameRelative: (assertString: Validator< $PropertyType, >), - babelrc: (assertBabelrcSearch: Validator< + babelrc: (assertBoolean: Validator< $PropertyType, >), + babelrcRoots: (assertBabelrcSearch: Validator< + $PropertyType, + >), code: (assertBoolean: Validator<$PropertyType>), ast: (assertBoolean: Validator<$PropertyType>), @@ -157,10 +160,11 @@ export type ValidatedOptions = { cwd?: string, filename?: string, filenameRelative?: string, - babelrc?: BabelrcSearch, - code?: boolean, + babelrc?: boolean, + babelrcRoots?: BabelrcSearch, configFile?: ConfigFileSearch, root?: string, + code?: boolean, ast?: boolean, inputSourceMap?: RootInputSourceMapOption, envName?: string, diff --git a/packages/babel-register/test/index.js b/packages/babel-register/test/index.js index 67eef99204..5643bdcf6f 100644 --- a/packages/babel-register/test/index.js +++ b/packages/babel-register/test/index.js @@ -1,4 +1,5 @@ import fs from "fs"; +import path from "path"; let currentHook; let currentOptions; @@ -39,6 +40,11 @@ describe("@babel/register", function() { let babelRegister; function setupRegister(config = { babelrc: false }) { + config = { + cwd: path.dirname(testFile), + ...config, + }; + babelRegister = require(registerFile); babelRegister.default(config); }