From 5b4c73fcac6d995925f0205c36ae8228125ae577 Mon Sep 17 00:00:00 2001 From: Jaroslav Kubicek Date: Fri, 29 Nov 2019 18:49:24 +0100 Subject: [PATCH] refactor: Improve error message in @babel/core when root config is not found --- .../babel-core/src/config/files/configuration.js | 2 +- .../babel-core/src/config/files/index-browser.js | 2 ++ packages/babel-core/src/config/files/index.js | 1 + packages/babel-core/src/config/partial.js | 13 ++++++++++--- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/babel-core/src/config/files/configuration.js b/packages/babel-core/src/config/files/configuration.js index 515171ceba94..06c304d6b79e 100644 --- a/packages/babel-core/src/config/files/configuration.js +++ b/packages/babel-core/src/config/files/configuration.js @@ -18,7 +18,7 @@ import type { CallerMetadata } from "../validation/options"; const debug = buildDebug("babel:config:loading:files:configuration"); -const ROOT_CONFIG_FILENAMES = [ +export const ROOT_CONFIG_FILENAMES = [ "babel.config.js", "babel.config.cjs", "babel.config.json", diff --git a/packages/babel-core/src/config/files/index-browser.js b/packages/babel-core/src/config/files/index-browser.js index 1d2adccaa811..b21b43e7778d 100644 --- a/packages/babel-core/src/config/files/index-browser.js +++ b/packages/babel-core/src/config/files/index-browser.js @@ -51,6 +51,8 @@ export function loadConfig( throw new Error(`Cannot load ${name} relative to ${dirname} in a browser`); } +export const ROOT_CONFIG_FILENAMES = []; + // eslint-disable-next-line no-unused-vars export function resolvePlugin(name: string, dirname: string): string | null { return null; diff --git a/packages/babel-core/src/config/files/index.js b/packages/babel-core/src/config/files/index.js index 3c74f2d4eebb..464ef66253f4 100644 --- a/packages/babel-core/src/config/files/index.js +++ b/packages/babel-core/src/config/files/index.js @@ -14,6 +14,7 @@ export { findRelativeConfig, findRootConfig, loadConfig, + ROOT_CONFIG_FILENAMES, } from "./configuration"; export type { ConfigFile, diff --git a/packages/babel-core/src/config/partial.js b/packages/babel-core/src/config/partial.js index 9f5f85ed4edc..2cf96e8653ed 100644 --- a/packages/babel-core/src/config/partial.js +++ b/packages/babel-core/src/config/partial.js @@ -12,7 +12,12 @@ import { type RootMode, } from "./validation/options"; -import { findConfigUpwards, type ConfigFile, type IgnoreFile } from "./files"; +import { + findConfigUpwards, + ROOT_CONFIG_FILENAMES, + type ConfigFile, + type IgnoreFile, +} from "./files"; function resolveRootMode(rootDir: string, rootMode: RootMode): string { switch (rootMode) { @@ -31,7 +36,9 @@ function resolveRootMode(rootDir: string, rootMode: RootMode): string { throw Object.assign( (new Error( `Babel was run with rootMode:"upward" but a root could not ` + - `be found when searching upward from "${rootDir}"`, + `be found when searching upward from "${rootDir}".\n` + + `One of the following config files must be in the directory tree: ` + + `"${ROOT_CONFIG_FILENAMES.join(", ")}".`, ): any), { code: "BABEL_ROOT_NOT_FOUND", @@ -40,7 +47,7 @@ function resolveRootMode(rootDir: string, rootMode: RootMode): string { ); } default: - throw new Error(`Assertion failure - unknown rootMode value`); + throw new Error(`Assertion failure - unknown rootMode value.`); } }