-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bug): fixes an issue with resolving .babelrc files when no explic…
…it path has been given. Closes #25
- Loading branch information
Showing
8 changed files
with
140 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {ITypescriptPluginBabelOptions} from "../../plugin/i-typescript-plugin-options"; | ||
|
||
export interface FindBabelConfigOptions { | ||
cwd: string; | ||
babelConfig?: ITypescriptPluginBabelOptions["babelConfig"]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {IBabelInputOptions} from "../../plugin/i-babel-options"; | ||
|
||
export interface FindBabelConfigResultBase { | ||
kind: "project" | "relative" | "dict"; | ||
} | ||
|
||
export interface FindBabelConfigDictResult extends FindBabelConfigResultBase { | ||
kind: "dict"; | ||
options: Partial<IBabelInputOptions>; | ||
} | ||
|
||
export interface FindBabelConfigProjectResult extends FindBabelConfigResultBase { | ||
kind: "project"; | ||
path: string; | ||
} | ||
|
||
export interface FindBabelConfigRelativeResult extends FindBabelConfigResultBase { | ||
kind: "relative"; | ||
path: string; | ||
} | ||
|
||
export type FindBabelConfigResult = FindBabelConfigDictResult | FindBabelConfigProjectResult | FindBabelConfigRelativeResult; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import {BABEL_CONFIG_JS_FILENAME} from "../../constant/constant"; | ||
import {join} from "path"; | ||
import {FindBabelConfigOptions} from "./find-babel-config-options"; | ||
import {ensureAbsolute} from "../path/path-util"; | ||
import {FindBabelConfigResult} from "./find-babel-config-result"; | ||
import {IBabelInputOptions} from "../../plugin/i-babel-options"; | ||
// @ts-ignore | ||
import {findConfigUpwards, findRelativeConfig} from "@babel/core/lib/config/files/configuration"; | ||
// @ts-ignore | ||
import {findPackageData} from "@babel/core/lib/config/files/package"; | ||
|
||
/** | ||
* Returns true if the given babelConfig is IBabelInputOptions | ||
* @param {FindBabelConfigOptions["babelConfig"]} babelConfig | ||
* @returns {babelConfig is IBabelInputOptions} | ||
*/ | ||
export function isBabelInputOptions(babelConfig?: FindBabelConfigOptions["babelConfig"]): babelConfig is Partial<IBabelInputOptions> { | ||
return babelConfig != null && typeof babelConfig !== "string"; | ||
} | ||
|
||
/** | ||
* Gets a Babel Config based on the given options | ||
* @param {GetBabelConfigOptions} options | ||
* @returns {GetBabelConfigResult} | ||
*/ | ||
export function findBabelConfig({babelConfig, cwd}: FindBabelConfigOptions): FindBabelConfigResult | undefined { | ||
// If babel options are provided directly | ||
if (isBabelInputOptions(babelConfig)) { | ||
return { | ||
kind: "dict", | ||
options: babelConfig | ||
}; | ||
} | ||
|
||
// If a config is given, determine its kind from its file name. | ||
if (babelConfig != null) { | ||
// This looks like a .babelrc or .babelrc.js file | ||
if (babelConfig.startsWith(".babel")) { | ||
return { | ||
kind: "relative", | ||
path: ensureAbsolute(cwd, babelConfig) | ||
}; | ||
} | ||
|
||
// This looks like a project-wide config (such as babel.config.js) | ||
else { | ||
return { | ||
kind: "project", | ||
path: ensureAbsolute(cwd, babelConfig) | ||
}; | ||
} | ||
} | ||
|
||
// In all other cases, try to resolve a config. Prioritize project-wide configs such as 'babel.config.js' | ||
// over relative ones such as '.babelrc' | ||
const projectConfigPath = findConfigUpwards(cwd) as string | null; | ||
|
||
// The resolved config will actually be the directory holding the config. | ||
// If it is defined, join the default config name to it | ||
if (projectConfigPath != null) { | ||
return { | ||
kind: "project", | ||
// The resolved config will actually be the directory holding the config. | ||
// If it is defined, join the default config name to it | ||
path: join(projectConfigPath, BABEL_CONFIG_JS_FILENAME) | ||
}; | ||
} | ||
|
||
// If no config have been resolved, it might be possible to resolve a .babelrc or .babelrc.js file | ||
const pkgData = findPackageData(join(cwd, "package.json")); | ||
const relativeConfigResult = findRelativeConfig(pkgData, process.env.NODE_ENV) as {config: {filepath: string} | null} | null; | ||
if (relativeConfigResult != null && relativeConfigResult.config != null) { | ||
return { | ||
kind: "relative", | ||
path: relativeConfigResult.config.filepath | ||
}; | ||
} | ||
|
||
// No config could be resolved | ||
return undefined; | ||
} |
6 changes: 2 additions & 4 deletions
6
...abel-config/i-get-babel-config-options.ts → ...-babel-config/get-babel-config-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
import {ITypescriptPluginBabelOptions} from "../../plugin/i-typescript-plugin-options"; | ||
import {IGetForcedBabelOptionsResult} from "../get-forced-babel-options/i-get-forced-babel-options-result"; | ||
import {IGetDefaultBabelOptionsResult} from "../get-default-babel-options/i-get-default-babel-options-result"; | ||
import {InputOptions} from "rollup"; | ||
import {FindBabelConfigOptions} from "./find-babel-config-options"; | ||
|
||
export interface IGetBabelConfigOptions { | ||
cwd: string; | ||
export interface GetBabelConfigOptions extends FindBabelConfigOptions { | ||
browserslist: string[] | undefined; | ||
rollupInputOptions: InputOptions; | ||
babelConfig?: ITypescriptPluginBabelOptions["babelConfig"]; | ||
forcedOptions?: IGetForcedBabelOptionsResult; | ||
defaultOptions?: IGetDefaultBabelOptionsResult; | ||
} |
2 changes: 1 addition & 1 deletion
2
...babel-config/i-get-babel-config-result.ts → ...t-babel-config/get-babel-config-result.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters