Correctly handle relative browserslistConfigFile paths (#13031)
* Correctly handle relative `browserslistConfigFile` * Fix flow
This commit is contained in:
@@ -19,6 +19,8 @@ import type {
|
||||
PluginItem,
|
||||
} from "./validation/options";
|
||||
|
||||
import { resolveBrowserslistConfigFile } from "./resolve-targets";
|
||||
|
||||
// Represents a config object and functions to lazily load the descriptors
|
||||
// for the plugins and presets so we don't load the plugins/presets unless
|
||||
// the options object actually ends up being applicable.
|
||||
@@ -71,6 +73,19 @@ function* handlerOf<T>(value: T): Handler<T> {
|
||||
return value;
|
||||
}
|
||||
|
||||
function optionsWithResolvedBrowserslistConfigFile(
|
||||
options: ValidatedOptions,
|
||||
dirname: string,
|
||||
): ValidatedOptions {
|
||||
if (typeof options.browserslistConfigFile === "string") {
|
||||
options.browserslistConfigFile = resolveBrowserslistConfigFile(
|
||||
options.browserslistConfigFile,
|
||||
dirname,
|
||||
);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a set of descriptors from a given options object, preserving
|
||||
* descriptor identity based on the identity of the plugin/preset arrays
|
||||
@@ -83,7 +98,7 @@ export function createCachedDescriptors(
|
||||
): OptionsAndDescriptors {
|
||||
const { plugins, presets, passPerPreset } = options;
|
||||
return {
|
||||
options,
|
||||
options: optionsWithResolvedBrowserslistConfigFile(options, dirname),
|
||||
plugins: plugins
|
||||
? () => createCachedPluginDescriptors(plugins, dirname)(alias)
|
||||
: () => handlerOf([]),
|
||||
@@ -112,7 +127,7 @@ export function createUncachedDescriptors(
|
||||
let presets;
|
||||
|
||||
return {
|
||||
options,
|
||||
options: optionsWithResolvedBrowserslistConfigFile(options, dirname),
|
||||
*plugins() {
|
||||
if (!plugins) {
|
||||
plugins = yield* createPluginDescriptors(
|
||||
|
||||
@@ -126,7 +126,7 @@ export default function* loadPrivatePartialConfig(
|
||||
|
||||
const options: NormalizedOptions = {
|
||||
...merged,
|
||||
targets: resolveTargets(merged, absoluteRootDir, absoluteRootDir),
|
||||
targets: resolveTargets(merged, absoluteRootDir),
|
||||
|
||||
// Tack the passes onto the object itself so that, if this object is
|
||||
// passed back to Babel a second time, it will be in the right structure
|
||||
|
||||
@@ -3,12 +3,19 @@
|
||||
import type { ValidatedOptions } from "./validation/options";
|
||||
import getTargets, { type Targets } from "@babel/helper-compilation-targets";
|
||||
|
||||
export function resolveBrowserslistConfigFile(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
browserslistConfigFile: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
configFilePath: string,
|
||||
): string | void {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function resolveTargets(
|
||||
options: ValidatedOptions,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
root: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
configFilePath: string | void,
|
||||
): Targets {
|
||||
let { targets } = options;
|
||||
if (typeof targets === "string" || Array.isArray(targets)) {
|
||||
|
||||
@@ -11,10 +11,16 @@ import type { ValidatedOptions } from "./validation/options";
|
||||
import path from "path";
|
||||
import getTargets, { type Targets } from "@babel/helper-compilation-targets";
|
||||
|
||||
export function resolveBrowserslistConfigFile(
|
||||
browserslistConfigFile: string,
|
||||
configFileDir: string,
|
||||
): string | void {
|
||||
return path.resolve(configFileDir, browserslistConfigFile);
|
||||
}
|
||||
|
||||
export function resolveTargets(
|
||||
options: ValidatedOptions,
|
||||
root: string,
|
||||
configFilePath: string = root,
|
||||
): Targets {
|
||||
let { targets } = options;
|
||||
if (typeof targets === "string" || Array.isArray(targets)) {
|
||||
@@ -25,13 +31,17 @@ export function resolveTargets(
|
||||
targets = { ...targets, esmodules: "intersect" };
|
||||
}
|
||||
|
||||
const { browserslistConfigFile } = options;
|
||||
let configFile;
|
||||
if (typeof options.browserslistConfigFile === "string") {
|
||||
configFile = path.resolve(configFilePath, options.browserslistConfigFile);
|
||||
let ignoreBrowserslistConfig = false;
|
||||
if (typeof browserslistConfigFile === "string") {
|
||||
configFile = browserslistConfigFile;
|
||||
} else {
|
||||
ignoreBrowserslistConfig = browserslistConfigFile === false;
|
||||
}
|
||||
|
||||
return getTargets((targets: any), {
|
||||
ignoreBrowserslistConfig: options.browserslistConfigFile === false,
|
||||
ignoreBrowserslistConfig,
|
||||
configFile,
|
||||
configPath: root,
|
||||
browserslistEnv: options.browserslistEnv,
|
||||
|
||||
Reference in New Issue
Block a user