Skip to content

Commit

Permalink
fix: support BROWSERSLIST{,_CONFIG} env (babel#16907)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored Oct 17, 2024
1 parent 33d705e commit d658738
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
22 changes: 14 additions & 8 deletions packages/babel-helper-compilation-targets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,20 @@ export default function getTargets(
!options.ignoreBrowserslistConfig && !hasTargets;

if (!browsers && shouldSearchForConfig) {
const configFile =
options.configFile ?? browserslist.findConfigFile(configPath);
if (configFile != null) {
onBrowserslistConfigFound?.(configFile);
browsers = browserslist.loadConfig({
config: configFile,
env: options.browserslistEnv,
});
// https://github.com/browserslist/browserslist/blob/8ae85caa905d130f4ca86f7a998a5b63abbbe582/node.js#L243
browsers = process.env.BROWSERSLIST;
if (!browsers) {
const configFile =
options.configFile ||
process.env.BROWSERSLIST_CONFIG ||
browserslist.findConfigFile(configPath);
if (configFile != null) {
onBrowserslistConfigFound?.(configFile);
browsers = browserslist.loadConfig({
config: configFile,
env: options.browserslistEnv,
});
}
}

if (browsers == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import browserslist from "browserslist";
import { join, dirname } from "path";
import { fileURLToPath } from "url";

import _getTargets from "../lib/index.js";
const getTargets = _getTargets.default || _getTargets;
import { itBabel8, itBabel7 } from "$repo-utils";
import { itBabel8, itBabel7, commonJS } from "$repo-utils";
const { require } = commonJS(import.meta.url);

describe("getTargets", () => {
it("parses", () => {
Expand Down Expand Up @@ -63,6 +61,28 @@ describe("getTargets", () => {
expect(parseFloat(actual.chrome)).toBeGreaterThan(4);
});

describe("when process.env.BROWSERSLIST is specified", () => {
afterAll(() => {
delete process.env.BROWSERSLIST;
});
it("should provide fallback to any targets option", () => {
process.env.BROWSERSLIST = "firefox 2";
expect(getTargets()).toEqual({ firefox: "2.0.0" });
});
});

describe("when process.env.BROWSERSLIST_CONFIG is specified", () => {
afterAll(() => {
delete process.env.BROWSERSLIST_CONFIG;
});
it("should provide fallback to any targets option", () => {
process.env.BROWSERSLIST_CONFIG = require.resolve(
"./fixtures/.browserslistrc",
);
expect(getTargets()).toEqual({ firefox: "30.0.0", chrome: "70.0.0" });
});
});

describe("validation", () => {
it("throws on invalid target name", () => {
const invalidTargetName = () => {
Expand Down Expand Up @@ -264,11 +284,7 @@ describe("getTargets", () => {
esmodules: "intersect",
},
{
configPath: join(
dirname(fileURLToPath(import.meta.url)),
"fixtures",
"foo.js",
),
configPath: require.resolve("./fixtures/.browserslistrc"),
},
),
).toMatchSnapshot();
Expand Down
21 changes: 21 additions & 0 deletions packages/babel-preset-env/test/index.skip-bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,25 @@ describe("babel-preset-env", () => {
it.todo(
"should add .browserslistrc to external dependencies when browserslistConfigFile is specified",
);

describe("when process.env.BROWSERSLIST_CONFIG is specified", () => {
afterEach(() => {
delete process.env.BROWSERSLIST_CONFIG;
});
it("should add process.env.BROWSERSLIST_CONFIG to external dependencies using preset-env's resolveTarget", () => {
const browserslistConfigFile = require.resolve(
"./regressions/.browserslistrc",
);
process.env.BROWSERSLIST_CONFIG = browserslistConfigFile;
const { externalDependencies } = babel.transformSync("", {
configFile: false,
presets: [[babelPresetEnv.default, { browserslistEnv: "development" }]],
});
expect(externalDependencies).toContain(browserslistConfigFile);
});

it.todo(
"should add process.env.BROWSERSLIST_CONFIG to external dependencies using core's resolveTarget",
);
});
});

0 comments on commit d658738

Please sign in to comment.