Split babelrc option into babelrcRoots.

This commit is contained in:
Logan Smyth
2018-04-22 13:00:28 -07:00
parent f7c26bf2bf
commit 8606b76438
5 changed files with 40 additions and 25 deletions

View File

@@ -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 |

View File

@@ -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;
}
/**

View File

@@ -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);

View File

@@ -36,9 +36,12 @@ const ROOT_VALIDATORS: ValidatorSet = {
filenameRelative: (assertString: Validator<
$PropertyType<ValidatedOptions, "filenameRelative">,
>),
babelrc: (assertBabelrcSearch: Validator<
babelrc: (assertBoolean: Validator<
$PropertyType<ValidatedOptions, "babelrc">,
>),
babelrcRoots: (assertBabelrcSearch: Validator<
$PropertyType<ValidatedOptions, "babelrcRoots">,
>),
code: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "code">>),
ast: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "ast">>),
@@ -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,

View File

@@ -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);
}