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

Avoid resolveTargets call if browsers is an empty array #14294

Merged
merged 15 commits into from Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
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,36 @@ describe("getTargets", () => {
},
);

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", () => {
let x = 0;

// 'test' is an unknown browser query, so methods of 'browserslist' library will throw an error
try {
getTargets({ esmodules: "intersect", browsers: ["test"] });
} catch {
x++;
}

expect(x).toBe(1);
Copy link
Member

Choose a reason for hiding this comment

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

Jest has native support for this:

expect(() => getTargets({ esmodules: "intersect", browsers: ["test"] })).toThrow();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea, I've fixed it.

});

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