[preset-env] Move all defaults to the separate module (#7084)
* preset-env: Move all defaults to the separate module. * preset-env: Add test cases for defaults.
This commit is contained in:
parent
c9a00fbae8
commit
035286a810
25
packages/babel-preset-env/src/defaults.js
Normal file
25
packages/babel-preset-env/src/defaults.js
Normal file
@ -0,0 +1,25 @@
|
||||
import type { Targets } from "./types";
|
||||
|
||||
const defaultWebIncludes = ["web.timers", "web.immediate", "web.dom.iterable"];
|
||||
const defaultExcludesForLooseMode = ["transform-typeof-symbol"];
|
||||
|
||||
export const getPlatformSpecificDefaultFor = (
|
||||
targets: Targets,
|
||||
): ?Array<string> => {
|
||||
const targetNames = Object.keys(targets);
|
||||
const isAnyTarget = !targetNames.length;
|
||||
const isWebTarget = targetNames.some(name => name !== "node");
|
||||
|
||||
return isAnyTarget || isWebTarget ? defaultWebIncludes : null;
|
||||
};
|
||||
|
||||
export const getOptionSpecificExcludesFor = ({
|
||||
loose,
|
||||
}: {
|
||||
loose: boolean,
|
||||
}): ?Array<string> => {
|
||||
if (loose) {
|
||||
return defaultExcludesForLooseMode;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
@ -3,7 +3,10 @@
|
||||
import semver from "semver";
|
||||
import builtInsList from "../data/built-ins.json";
|
||||
import { logPlugin } from "./debug";
|
||||
import { defaultWebIncludes } from "./default-includes";
|
||||
import {
|
||||
getPlatformSpecificDefaultFor,
|
||||
getOptionSpecificExcludesFor,
|
||||
} from "./defaults";
|
||||
import moduleTransformations from "./module-transformations";
|
||||
import normalizeOptions from "./normalize-options.js";
|
||||
import pluginList from "../data/plugins.json";
|
||||
@ -115,26 +118,6 @@ export const transformIncludesAndExcludes = (opts: Array<string>): Object => {
|
||||
);
|
||||
};
|
||||
|
||||
const getPlatformSpecificDefaultFor = (targets: Targets): ?Array<string> => {
|
||||
const targetNames = Object.keys(targets);
|
||||
const isAnyTarget = !targetNames.length;
|
||||
const isWebTarget = targetNames.some(name => name !== "node");
|
||||
|
||||
return isAnyTarget || isWebTarget ? defaultWebIncludes : null;
|
||||
};
|
||||
|
||||
const getOptionSpecificExcludesFor = ({
|
||||
loose,
|
||||
}: {
|
||||
loose: boolean,
|
||||
}): Array<string> => {
|
||||
const defaultExcludes = [];
|
||||
if (loose) {
|
||||
defaultExcludes.push("transform-typeof-symbol");
|
||||
}
|
||||
return defaultExcludes;
|
||||
};
|
||||
|
||||
const filterItems = (
|
||||
list,
|
||||
includes,
|
||||
|
||||
50
packages/babel-preset-env/test/defaults.js
Normal file
50
packages/babel-preset-env/test/defaults.js
Normal file
@ -0,0 +1,50 @@
|
||||
"use strict";
|
||||
|
||||
const defaults = require("../lib/defaults.js");
|
||||
const assert = require("assert");
|
||||
|
||||
const {
|
||||
getPlatformSpecificDefaultFor,
|
||||
getOptionSpecificExcludesFor,
|
||||
} = defaults;
|
||||
|
||||
describe("defaults", () => {
|
||||
describe("getPlatformSpecificDefaultFor", () => {
|
||||
it("should return web polyfills for non-`node` platform", () => {
|
||||
const defaultWebIncludesForChromeAndNode = getPlatformSpecificDefaultFor({
|
||||
chrome: "63",
|
||||
node: "8",
|
||||
});
|
||||
assert.deepEqual(defaultWebIncludesForChromeAndNode, [
|
||||
"web.timers",
|
||||
"web.immediate",
|
||||
"web.dom.iterable",
|
||||
]);
|
||||
});
|
||||
|
||||
it("shouldn't return web polyfills for node platform", () => {
|
||||
const defaultWebIncludesForChromeAndNode = getPlatformSpecificDefaultFor({
|
||||
node: "8",
|
||||
});
|
||||
assert.equal(defaultWebIncludesForChromeAndNode, null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getOptionSpecificExcludesFor", () => {
|
||||
it("should return correct excludes for `loose` mode", () => {
|
||||
const defaultWebIncludesForChromeAndNode = getOptionSpecificExcludesFor({
|
||||
loose: true,
|
||||
});
|
||||
assert.deepEqual(defaultWebIncludesForChromeAndNode, [
|
||||
"transform-typeof-symbol",
|
||||
]);
|
||||
});
|
||||
|
||||
it("shouldn't return excludes for non-`loose` mode", () => {
|
||||
const defaultWebIncludesForChromeAndNode = getOptionSpecificExcludesFor({
|
||||
loose: false,
|
||||
});
|
||||
assert.deepEqual(defaultWebIncludesForChromeAndNode, null);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user