[preset-env] Add browserslistEnv option (#11434)
This commit is contained in:
parent
698fe8ef50
commit
c5ba345ac2
@ -205,6 +205,7 @@ export default function getTargets(
|
||||
const browsers = browserslist(browsersquery, {
|
||||
path: options.configPath,
|
||||
mobileToDesktop: true,
|
||||
env: options.browserslistEnv,
|
||||
});
|
||||
|
||||
const queryBrowsers = getLowestVersions(browsers);
|
||||
|
||||
@ -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" });
|
||||
});
|
||||
@ -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;
|
||||
@ -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);
|
||||
|
||||
@ -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,
|
||||
) =>
|
||||
@ -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,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ export const TopLevelOptions = {
|
||||
spec: "spec",
|
||||
targets: "targets",
|
||||
useBuiltIns: "useBuiltIns",
|
||||
browserslistEnv: "browserslistEnv",
|
||||
};
|
||||
|
||||
export const ModulesOption = {
|
||||
|
||||
@ -34,6 +34,7 @@ export type Options = {
|
||||
spec: boolean,
|
||||
targets: { ...InputTargets, uglify?: boolean },
|
||||
useBuiltIns: BuiltInsOption,
|
||||
browserslistEnv: string,
|
||||
};
|
||||
|
||||
// Babel
|
||||
|
||||
2
packages/babel-preset-env/test/fixtures/debug/browserslist-env/.browserslistrc
vendored
Normal file
2
packages/babel-preset-env/test/fixtures/debug/browserslist-env/.browserslistrc
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
[custom]
|
||||
ie 11
|
||||
1
packages/babel-preset-env/test/fixtures/debug/browserslist-env/input.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/debug/browserslist-env/input.mjs
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "core-js";
|
||||
14
packages/babel-preset-env/test/fixtures/debug/browserslist-env/options.js
vendored
Normal file
14
packages/babel-preset-env/test/fixtures/debug/browserslist-env/options.js
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
module.exports = {
|
||||
validateLogs: true,
|
||||
ignoreOutput: true,
|
||||
presets: [
|
||||
[
|
||||
"env",
|
||||
{
|
||||
debug: true,
|
||||
browserslistEnv: "custom",
|
||||
configPath: __dirname,
|
||||
},
|
||||
],
|
||||
],
|
||||
};
|
||||
44
packages/babel-preset-env/test/fixtures/debug/browserslist-env/stdout.txt
vendored
Normal file
44
packages/babel-preset-env/test/fixtures/debug/browserslist-env/stdout.txt
vendored
Normal file
@ -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.
|
||||
@ -5,6 +5,7 @@ const normalizeOptions = require("../lib/normalize-options.js");
|
||||
const {
|
||||
checkDuplicateIncludeExcludes,
|
||||
validateBoolOption,
|
||||
validateStringOption,
|
||||
validateModulesOption,
|
||||
validateUseBuiltInsOption,
|
||||
normalizePluginName,
|
||||
@ -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(() => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user