Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve .browserslistrc as a project-wide file #13028

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/babel-core/src/config/partial.js
Expand Up @@ -126,7 +126,7 @@ export default function* loadPrivatePartialConfig(

const options: NormalizedOptions = {
...merged,
targets: resolveTargets(merged, absoluteRootDir, filename),
targets: resolveTargets(merged, absoluteRootDir, 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
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/config/resolve-targets-browser.js
Expand Up @@ -8,7 +8,7 @@ export function resolveTargets(
// eslint-disable-next-line no-unused-vars
root: string,
// eslint-disable-next-line no-unused-vars
filename: string | void,
configFilePath: string | void,
): Targets {
let { targets } = options;
if (typeof targets === "string" || Array.isArray(targets)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-core/src/config/resolve-targets.js
Expand Up @@ -14,7 +14,7 @@ import getTargets, { type Targets } from "@babel/helper-compilation-targets";
export function resolveTargets(
options: ValidatedOptions,
root: string,
filename: string | void,
configFilePath: string = root,
): Targets {
let { targets } = options;
if (typeof targets === "string" || Array.isArray(targets)) {
Expand All @@ -27,13 +27,13 @@ export function resolveTargets(

let configFile;
if (typeof options.browserslistConfigFile === "string") {
configFile = path.resolve(root, options.browserslistConfigFile);
configFile = path.resolve(configFilePath, options.browserslistConfigFile);
}

return getTargets((targets: any), {
ignoreBrowserslistConfig: options.browserslistConfigFile === false,
configFile,
configPath: filename ?? root,
configPath: root,
browserslistEnv: options.browserslistEnv,
});
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions packages/babel-core/test/targets.js
Expand Up @@ -91,11 +91,24 @@ describe("browserslist", () => {
).toEqual({ chrome: "80.0.0" });
});

it("loads .browserslistrc relative to the input file", () => {
it("loads .browserslistrc relative to the root", () => {
expect(
loadOptions({
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", () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a separate bug, I found it while attempting to write a test. I'll fix it in a follow-up PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(
loadOptions({
cwd: join(cwd, "fixtures", "targets"),
filename: "./node_modules/dep/test.js",
babelrcRoots: ["./node_modules/dep/"],
}).targets,
).toEqual({ edge: "14.0.0" });
});
Expand Down
12 changes: 7 additions & 5 deletions packages/babel-preset-env/test/regressions.js
Expand Up @@ -5,17 +5,19 @@ import { fileURLToPath } from "url";

describe("#12880", () => {
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".
// a ** b is supported starting from firefox 52;
// a => b is supported starting from firefox 45.
const out = babel7_12.transformSync("a ** b; a => b;", {
configFile: false,
presets: [[env, { modules: false }]],
filename: path.join(
path.dirname(fileURLToPath(import.meta.url)),
"regressions",
"input.js",
),
filename: path.join(root, "input.js"),
root,
});

expect(out.code).toMatchInlineSnapshot(`
Expand Down