Skip to content

Commit

Permalink
Avoid resolveTargets call if browsers is an empty array (#14294)
Browse files Browse the repository at this point in the history
Co-authored-by: gaosheng08 <gaosheng08@meituan.com>
  • Loading branch information
dev-itsheng and dev-itsheng committed Mar 14, 2022
1 parent 4820ea0 commit 8dcf3ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/babel-helper-compilation-targets/src/index.ts
Expand Up @@ -219,7 +219,10 @@ export default function getTargets(
esmodules = false;
}

if (browsers) {
// If current value of `browsers` is undefined (`ignoreBrowserslistConfig` should be `false`)
// or an empty array (without any user config, use default config),
// we don't need to call `resolveTargets` to execute the related methods of `browserslist` library.
if (browsers?.length) {
const queryBrowsers = resolveTargets(browsers, options.browserslistEnv);

if (esmodules === "intersect") {
Expand Down
Expand Up @@ -293,6 +293,31 @@ describe("getTargets", () => {
},
);

(process.env.BABEL_8_BREAKING ? it.skip : it)(
"'browsers' option will have no effect if it is an empty array - Babel 7",
() => {
expect(getTargets({ esmodules: "intersect", browsers: [] })).toEqual(
getTargets({ esmodules: "intersect" }),
);
},
);

it("The final 'browsers' handled variable will have no effect if it is an empty array", () => {
expect(getTargets({ esmodules: "intersect", browsers: [] })).toEqual(
getTargets(
{ esmodules: "intersect" },
{ ignoreBrowserslistConfig: true },
),
);
});

it("'resolveTargets' will be called rightly if 'browsers' is an array with some value", () => {
// 'test' is an unknown browser query, so methods of 'browserslist' library will throw an error
expect(() =>
getTargets({ esmodules: "intersect", browsers: ["test"] }),
).toThrow();
});

(process.env.BABEL_8_BREAKING ? it : it.skip)(
"'intersect' behaves like no-op if no browsers are specified",
() => {
Expand Down

0 comments on commit 8dcf3ef

Please sign in to comment.