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

[preset-env] Add browserslistEnv option #11434

Merged
merged 6 commits into from May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/babel-helper-compilation-targets/src/index.js
Expand Up @@ -205,6 +205,7 @@ export default function getTargets(
const browsers = browserslist(browsersquery, {
path: options.configPath,
mobileToDesktop: true,
env: options.browserslistEnv,
});

const queryBrowsers = getLowestVersions(browsers);
Expand Down
@@ -0,0 +1,2 @@
[custom]
ie 11
@@ -0,0 +1,10 @@
import getTargets from "../..";

it("allows custom browserslist env", () => {
const actual = getTargets(
{},
{ configPath: __dirname, browserslistEnv: "custom" },
);

expect(actual).toEqual({ ie: "11.0.0" });
});
3 changes: 2 additions & 1 deletion packages/babel-preset-env/src/index.js
Expand Up @@ -228,6 +228,7 @@ export default declare((api, opts) => {
targets: optionsTargets,
useBuiltIns,
corejs: { version: corejs, proposals },
browserslistEnv,
} = normalizeOptions(opts);
// TODO: remove this in next major
let hasUglifyTarget = false;
Expand Down Expand Up @@ -257,7 +258,7 @@ export default declare((api, opts) => {
const targets = getTargets(
// $FlowIgnore optionsTargets doesn't have an "uglify" property anymore
(optionsTargets: InputTargets),
{ ignoreBrowserslistConfig, configPath },
{ ignoreBrowserslistConfig, configPath, browserslistEnv },
);
const include = transformIncludesAndExcludes(optionsInclude);
const exclude = transformIncludesAndExcludes(optionsExclude);
Expand Down
18 changes: 18 additions & 0 deletions packages/babel-preset-env/src/normalize-options.js
Expand Up @@ -152,6 +152,20 @@ export const validateBoolOption = (
return value;
};

export const validateStringOption = (
name: string,
value?: string,
defaultValue?: string,
) => {
if (typeof value === "undefined") {
value = defaultValue;
} else if (typeof value !== "string") {
throw new Error(`Preset env: '${name}' option must be a string.`);
}

return value;
};

export const validateIgnoreBrowserslistConfig = (
ignoreBrowserslistConfig: boolean,
) =>
Expand Down Expand Up @@ -295,5 +309,9 @@ export default function normalizeOptions(opts: Options) {
spec: validateBoolOption(TopLevelOptions.spec, opts.spec, false),
targets: normalizeTargets(opts.targets),
useBuiltIns: useBuiltIns,
browserslistEnv: validateStringOption(
TopLevelOptions.browserslistEnv,
opts.browserslistEnv,
),
};
}
1 change: 1 addition & 0 deletions packages/babel-preset-env/src/options.js
Expand Up @@ -15,6 +15,7 @@ export const TopLevelOptions = {
spec: "spec",
targets: "targets",
useBuiltIns: "useBuiltIns",
browserslistEnv: "browserslistEnv",
};

export const ModulesOption = {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-env/src/types.js
Expand Up @@ -34,6 +34,7 @@ export type Options = {
spec: boolean,
targets: { ...InputTargets, uglify?: boolean },
useBuiltIns: BuiltInsOption,
browserslistEnv: string,
};

// Babel
Expand Down
@@ -0,0 +1,2 @@
[custom]
ie 11
@@ -0,0 +1 @@
import "core-js";
@@ -0,0 +1,14 @@
module.exports = {
validateLogs: true,
ignoreOutput: true,
presets: [
[
"env",
{
debug: true,
browserslistEnv: "custom",
configPath: __dirname,
},
],
],
};
@@ -0,0 +1,44 @@
@babel/preset-env: `DEBUG` option

Using targets:
{
"ie": "11"
}

Using modules transform: auto

Using plugins:
proposal-nullish-coalescing-operator { "ie":"11" }
proposal-optional-chaining { "ie":"11" }
proposal-json-strings { "ie":"11" }
proposal-optional-catch-binding { "ie":"11" }
transform-parameters { "ie":"11" }
proposal-async-generator-functions { "ie":"11" }
proposal-object-rest-spread { "ie":"11" }
transform-dotall-regex { "ie":"11" }
proposal-unicode-property-regex { "ie":"11" }
transform-named-capturing-groups-regex { "ie":"11" }
transform-async-to-generator { "ie":"11" }
transform-exponentiation-operator { "ie":"11" }
transform-template-literals { "ie":"11" }
transform-literals { "ie":"11" }
transform-function-name { "ie":"11" }
transform-arrow-functions { "ie":"11" }
transform-classes { "ie":"11" }
transform-object-super { "ie":"11" }
transform-shorthand-properties { "ie":"11" }
transform-duplicate-keys { "ie":"11" }
transform-computed-properties { "ie":"11" }
transform-for-of { "ie":"11" }
transform-sticky-regex { "ie":"11" }
transform-unicode-regex { "ie":"11" }
transform-spread { "ie":"11" }
transform-destructuring { "ie":"11" }
transform-block-scoping { "ie":"11" }
transform-typeof-symbol { "ie":"11" }
transform-new-target { "ie":"11" }
transform-regenerator { "ie":"11" }
transform-modules-commonjs { "ie":"11" }
proposal-dynamic-import { "ie":"11" }

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
23 changes: 23 additions & 0 deletions packages/babel-preset-env/test/normalize-options.spec.js
Expand Up @@ -5,6 +5,7 @@ const normalizeOptions = require("../lib/normalize-options.js");
const {
checkDuplicateIncludeExcludes,
validateBoolOption,
validateStringOption,
validateModulesOption,
validateUseBuiltInsOption,
normalizePluginName,
Expand Down Expand Up @@ -191,6 +192,28 @@ describe("normalize-options", () => {
});
});

describe("validateStringOption", () => {
it("`undefined` option default", () => {
expect(validateStringOption("test", undefined, "default")).toBe(
"default",
);
});

it("`value` option returns value", () => {
expect(validateStringOption("test", "value", "default")).toBe("value");
});

it("no default returns undefined", () => {
expect(validateStringOption("test", undefined)).toBe(undefined);
});

it("array option is invalid", () => {
expect(() => {
validateStringOption("test", [], "default");
}).toThrow();
});
});

describe("checkDuplicateIncludeExcludes", function() {
it("should throw if duplicate names in both", function() {
expect(() => {
Expand Down