Skip to content

Commit

Permalink
Allow users to opt into automatically resolving 'root'.
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Sep 10, 2018
1 parent e372129 commit c4a4119
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
24 changes: 24 additions & 0 deletions packages/babel-core/src/config/files/configuration.js
Expand Up @@ -24,6 +24,30 @@ const BABELRC_FILENAME = ".babelrc";
const BABELRC_JS_FILENAME = ".babelrc.js";
const BABELIGNORE_FILENAME = ".babelignore";

export function findClosestConfig(rootDir: string): string {
let dirname = rootDir;
while (true) {
if (fs.existsSync(path.join(dirname, BABEL_CONFIG_JS_FILENAME))) {
return dirname;
}

const nextDir = path.dirname(dirname);
if (dirname === nextDir) break;
dirname = nextDir;
}

throw Object.assign(
(new Error(
`Babel was run with autoRoot:true but a root babel.config.js could not ` +
`be found when searching from "${rootDir}"`,
): any),
{
code: "BABEL_AUTOROOT_NOT_FOUND",
dirname: rootDir,
},
);
}

export function findRelativeConfig(
packageData: FilePackageData,
envName: string,
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-core/src/config/files/index-browser.js
Expand Up @@ -11,6 +11,14 @@ import type { CallerMetadata } from "../validation/options";

export type { ConfigFile, IgnoreFile, RelativeConfig, FilePackageData };

export function findClosestConfig(
rootDir: string, // eslint-disable-line no-unused-vars
): string {
throw new Error(
`Cannot search for filesystem config root when running in a browser`,
);
}

export function findPackageData(filepath: string): FilePackageData {
return {
filepath,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-core/src/config/files/index.js
Expand Up @@ -10,6 +10,7 @@ import typeof * as indexType from "./index";
export { findPackageData } from "./package";

export {
findClosestConfig,
findRelativeConfig,
findRootConfig,
loadConfig,
Expand Down
14 changes: 11 additions & 3 deletions packages/babel-core/src/config/partial.js
Expand Up @@ -8,7 +8,7 @@ import { buildRootChain, type ConfigContext } from "./config-chain";
import { getEnv } from "./helpers/environment";
import { validate, type ValidatedOptions } from "./validation/options";

import type { ConfigFile, IgnoreFile } from "./files";
import { findClosestConfig, type ConfigFile, type IgnoreFile } from "./files";

export default function loadPrivatePartialConfig(
inputOpts: mixed,
Expand All @@ -28,9 +28,17 @@ export default function loadPrivatePartialConfig(

const args = inputOpts ? validate("arguments", inputOpts) : {};

const { envName = getEnv(), cwd = ".", root: rootDir = ".", caller } = args;
const {
envName = getEnv(),
cwd = ".",
root: rootDir = ".",
caller,
autoRoot = false,
} = args;
const absoluteCwd = path.resolve(cwd);
const absoluteRootDir = path.resolve(absoluteCwd, rootDir);
let absoluteRootDir = path.resolve(absoluteCwd, rootDir);

if (autoRoot) absoluteRootDir = findClosestConfig(absoluteRootDir);

const context: ConfigContext = {
filename:
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -33,6 +33,7 @@ const ROOT_VALIDATORS: ValidatorSet = {
configFile: (assertConfigFileSearch: Validator<
$PropertyType<ValidatedOptions, "configFile">,
>),
autoRoot: (assertBoolean: Validator<$PropertyType<ValidatedOptions, "autoRoot">>),

caller: (assertCallerMetadata: Validator<
$PropertyType<ValidatedOptions, "caller">,
Expand Down Expand Up @@ -176,6 +177,7 @@ export type ValidatedOptions = {
babelrcRoots?: BabelrcSearch,
configFile?: ConfigFileSearch,
root?: string,
autoRoot?: boolean,
code?: boolean,
ast?: boolean,
inputSourceMap?: RootInputSourceMapOption,
Expand Down

0 comments on commit c4a4119

Please sign in to comment.