[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:
Artem Yavorsky 2017-12-22 17:42:06 +02:00 committed by Henry Zhu
parent c9a00fbae8
commit 035286a810
3 changed files with 79 additions and 21 deletions

View 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;
};

View File

@ -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,

View 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);
});
});
});