diff --git a/.gitignore b/.gitignore index 45afd8fef7f0..37c66b084feb 100644 --- a/.gitignore +++ b/.gitignore @@ -70,6 +70,8 @@ packages/babel-standalone/babel.min.js /eslint/*/LICENSE !/packages/babel-eslint-plugin/LICENSE /.vscode +# local directory for VSCode Extension - https://marketplace.visualstudio.com/items?itemName=xyz.local-history +/.history /dts diff --git a/jest.config.js b/jest.config.js index 2a03ce4e4202..276e156144d9 100644 --- a/jest.config.js +++ b/jest.config.js @@ -21,6 +21,7 @@ module.exports = { "/test/helpers/", "/test/warning\\.js", "/build/", + "/.history/", // local directory for VSCode Extension - https://marketplace.visualstudio.com/items?itemName=xyz.local-history "_browser\\.js", ], testEnvironment: "node", diff --git a/packages/babel-helper-compilation-targets/src/index.ts b/packages/babel-helper-compilation-targets/src/index.ts index c4077623583b..2919080b86d7 100644 --- a/packages/babel-helper-compilation-targets/src/index.ts +++ b/packages/babel-helper-compilation-targets/src/index.ts @@ -154,8 +154,11 @@ function generateTargets(inputTargets: InputTargets): Targets { return input as any as Targets; } -function resolveTargets(queries: Browsers): Targets { - const resolved = browserslist(queries, { mobileToDesktop: true }); +function resolveTargets(queries: Browsers, env?: string): Targets { + const resolved = browserslist(queries, { + mobileToDesktop: true, + env, + }); return getLowestVersions(resolved); } @@ -217,7 +220,7 @@ export default function getTargets( } if (browsers) { - const queryBrowsers = resolveTargets(browsers); + const queryBrowsers = resolveTargets(browsers, options.browserslistEnv); if (esmodules === "intersect") { for (const browser of Object.keys(queryBrowsers)) { diff --git a/packages/babel-helper-compilation-targets/test/browserslist-extends/browserslist-extends.spec.js b/packages/babel-helper-compilation-targets/test/browserslist-extends/browserslist-extends.spec.js new file mode 100644 index 000000000000..876f30777eb0 --- /dev/null +++ b/packages/babel-helper-compilation-targets/test/browserslist-extends/browserslist-extends.spec.js @@ -0,0 +1,37 @@ +import { dirname, resolve } from "path"; +import { fileURLToPath } from "url"; +import getTargets from "../../lib"; + +const currentDir = dirname(fileURLToPath(import.meta.url)); + +const oldEnv = process.env.BROWSERSLIST_DANGEROUS_EXTEND; + +beforeAll(() => { + process.env.BROWSERSLIST_DANGEROUS_EXTEND = true; +}); + +afterAll(() => { + process.env.BROWSERSLIST_DANGEROUS_EXTEND = oldEnv; +}); + +it("pass env to configs used with extends", async () => { + const actual = getTargets( + { + browsers: [ + `extends ${resolve( + currentDir, + "fixtures", + "@babel", + "browserslist-config-fixture.cjs", + )}`, + "chrome >= 71", + ], + }, + { + configPath: currentDir, + browserslistEnv: "custom", + }, + ); + + expect(actual).toEqual({ chrome: "71.0.0", firefox: "75.0.0" }); +}); diff --git a/packages/babel-helper-compilation-targets/test/browserslist-extends/fixtures/@babel/browserslist-config-fixture.cjs b/packages/babel-helper-compilation-targets/test/browserslist-extends/fixtures/@babel/browserslist-config-fixture.cjs new file mode 100644 index 000000000000..b6108c2910f3 --- /dev/null +++ b/packages/babel-helper-compilation-targets/test/browserslist-extends/fixtures/@babel/browserslist-config-fixture.cjs @@ -0,0 +1,4 @@ +module.exports = { + custom: ["firefox >= 75"], + defaults: ["chrome >= 5"], +};