Skip to content

Commit

Permalink
fix: pass browserslistEnv to resolveTargets
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Aug 20, 2021
1 parent 1d4bd31 commit 62eb2f1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -70,6 +70,7 @@ packages/babel-standalone/babel.min.js
/eslint/*/LICENSE
!/packages/babel-eslint-plugin/LICENSE
/.vscode
/.history

/dts

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Expand Up @@ -21,6 +21,7 @@ module.exports = {
"/test/helpers/",
"<rootDir>/test/warning\\.js",
"<rootDir>/build/",
"<rootDir>/.history/",
"_browser\\.js",
],
testEnvironment: "node",
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-helper-compilation-targets/package.json
Expand Up @@ -32,7 +32,8 @@
"devDependencies": {
"@babel/core": "workspace:*",
"@babel/helper-plugin-test-runner": "workspace:*",
"@types/semver": "^5.5.0"
"@types/semver": "^5.5.0",
"rimraf": "^3.0.0"
},
"engines": {
"node": ">=6.9.0"
Expand Down
9 changes: 6 additions & 3 deletions packages/babel-helper-compilation-targets/src/index.ts
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)) {
Expand Down
@@ -0,0 +1,3 @@
[custom]
extends @babel/browserslist-config
chrome >= 71
@@ -0,0 +1,44 @@
import { join, dirname } from "path";
import { promises } from "fs";
import rimraf from "rimraf";
import { fileURLToPath } from "url";
import getTargets from "../..";

const currentDir = dirname(fileURLToPath(import.meta.url));
let mocked = [];

async function mock(name, exports) {
const dir = join(
fileURLToPath(import.meta.url),
"..",
"..",
"..",
"node_modules",
name,
);
mocked.push(dir);
await promises.mkdir(dir, { recursive: true });
const content = "module.exports = " + JSON.stringify(exports);
await promises.writeFile(join(dir, "index.js"), content);
}

afterEach(() => {
mocked.map(dir => rimraf.sync(dir));
mocked = [];
});

it("pass env to configs used with extends", async () => {
await mock("@babel/browserslist-config", {
custom: ["firefox >= 75"],
defaults: ["chrome >= 5"],
});
const actual = getTargets(
{},
{
configPath: currentDir,
browserslistEnv: "custom",
},
);

expect(actual).toEqual({ chrome: "71.0.0", firefox: "75.0.0" });
});
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -458,6 +458,7 @@ __metadata:
"@babel/helper-validator-option": "workspace:^7.14.5"
"@types/semver": ^5.5.0
browserslist: ^4.16.6
rimraf: ^3.0.0
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
peerDependencies:
"@babel/core": ^7.0.0
Expand Down

0 comments on commit 62eb2f1

Please sign in to comment.