Resolve .browserslistrc as a project-wide file (#13028)
This commit is contained in:
parent
8c445e60ba
commit
b0d83daceb
@ -126,7 +126,7 @@ export default function* loadPrivatePartialConfig(
|
|||||||
|
|
||||||
const options: NormalizedOptions = {
|
const options: NormalizedOptions = {
|
||||||
...merged,
|
...merged,
|
||||||
targets: resolveTargets(merged, absoluteRootDir, filename),
|
targets: resolveTargets(merged, absoluteRootDir, absoluteRootDir),
|
||||||
|
|
||||||
// Tack the passes onto the object itself so that, if this object is
|
// 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
|
// passed back to Babel a second time, it will be in the right structure
|
||||||
|
|||||||
@ -8,7 +8,7 @@ export function resolveTargets(
|
|||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
root: string,
|
root: string,
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
filename: string | void,
|
configFilePath: string | void,
|
||||||
): Targets {
|
): Targets {
|
||||||
let { targets } = options;
|
let { targets } = options;
|
||||||
if (typeof targets === "string" || Array.isArray(targets)) {
|
if (typeof targets === "string" || Array.isArray(targets)) {
|
||||||
|
|||||||
@ -14,7 +14,7 @@ import getTargets, { type Targets } from "@babel/helper-compilation-targets";
|
|||||||
export function resolveTargets(
|
export function resolveTargets(
|
||||||
options: ValidatedOptions,
|
options: ValidatedOptions,
|
||||||
root: string,
|
root: string,
|
||||||
filename: string | void,
|
configFilePath: string = root,
|
||||||
): Targets {
|
): Targets {
|
||||||
let { targets } = options;
|
let { targets } = options;
|
||||||
if (typeof targets === "string" || Array.isArray(targets)) {
|
if (typeof targets === "string" || Array.isArray(targets)) {
|
||||||
@ -27,13 +27,13 @@ export function resolveTargets(
|
|||||||
|
|
||||||
let configFile;
|
let configFile;
|
||||||
if (typeof options.browserslistConfigFile === "string") {
|
if (typeof options.browserslistConfigFile === "string") {
|
||||||
configFile = path.resolve(root, options.browserslistConfigFile);
|
configFile = path.resolve(configFilePath, options.browserslistConfigFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
return getTargets((targets: any), {
|
return getTargets((targets: any), {
|
||||||
ignoreBrowserslistConfig: options.browserslistConfigFile === false,
|
ignoreBrowserslistConfig: options.browserslistConfigFile === false,
|
||||||
configFile,
|
configFile,
|
||||||
configPath: filename ?? root,
|
configPath: root,
|
||||||
browserslistEnv: options.browserslistEnv,
|
browserslistEnv: options.browserslistEnv,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
3
packages/babel-core/test/fixtures/targets/node_modules/dep/.babelrc.json
generated
vendored
Normal file
3
packages/babel-core/test/fixtures/targets/node_modules/dep/.babelrc.json
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"browserslistConfigFile": "./.browserslistrc"
|
||||||
|
}
|
||||||
@ -91,11 +91,24 @@ describe("browserslist", () => {
|
|||||||
).toEqual({ chrome: "80.0.0" });
|
).toEqual({ chrome: "80.0.0" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("loads .browserslistrc relative to the input file", () => {
|
it("loads .browserslistrc relative to the root", () => {
|
||||||
expect(
|
expect(
|
||||||
loadOptions({
|
loadOptions({
|
||||||
cwd: join(cwd, "fixtures", "targets"),
|
cwd: join(cwd, "fixtures", "targets"),
|
||||||
filename: "./nested/test.js",
|
filename: "./node_modules/dep/test.js",
|
||||||
|
}).targets,
|
||||||
|
).toEqual({ chrome: "80.0.0" });
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: browserslistConfig is currently resolved starting from the root
|
||||||
|
// rather than from the config file.
|
||||||
|
// eslint-disable-next-line jest/no-disabled-tests
|
||||||
|
it.skip("loads nested .browserslistrc files if explicitly specified", () => {
|
||||||
|
expect(
|
||||||
|
loadOptions({
|
||||||
|
cwd: join(cwd, "fixtures", "targets"),
|
||||||
|
filename: "./node_modules/dep/test.js",
|
||||||
|
babelrcRoots: ["./node_modules/dep/"],
|
||||||
}).targets,
|
}).targets,
|
||||||
).toEqual({ edge: "14.0.0" });
|
).toEqual({ edge: "14.0.0" });
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,17 +5,19 @@ import { fileURLToPath } from "url";
|
|||||||
|
|
||||||
describe("#12880", () => {
|
describe("#12880", () => {
|
||||||
it("read the .browserslistrc file when using @babel/core < 7.13.0", () => {
|
it("read the .browserslistrc file when using @babel/core < 7.13.0", () => {
|
||||||
|
const root = path.join(
|
||||||
|
path.dirname(fileURLToPath(import.meta.url)),
|
||||||
|
"regressions",
|
||||||
|
);
|
||||||
|
|
||||||
// The browserslistrc file contains "firefox 50".
|
// The browserslistrc file contains "firefox 50".
|
||||||
// a ** b is supported starting from firefox 52;
|
// a ** b is supported starting from firefox 52;
|
||||||
// a => b is supported starting from firefox 45.
|
// a => b is supported starting from firefox 45.
|
||||||
const out = babel7_12.transformSync("a ** b; a => b;", {
|
const out = babel7_12.transformSync("a ** b; a => b;", {
|
||||||
configFile: false,
|
configFile: false,
|
||||||
presets: [[env, { modules: false }]],
|
presets: [[env, { modules: false }]],
|
||||||
filename: path.join(
|
filename: path.join(root, "input.js"),
|
||||||
path.dirname(fileURLToPath(import.meta.url)),
|
root,
|
||||||
"regressions",
|
|
||||||
"input.js",
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(out.code).toMatchInlineSnapshot(`
|
expect(out.code).toMatchInlineSnapshot(`
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user