[preset-env] Include / exclude module plugins properly (#10218)

* Include / exclude module plugins properly

* Use filter-items to excl / incl module plugins

* Move transform selection to getModulesPluginNames

* Remove unnecessary spread operator

Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>

* Adjust tests to changes in #10218
This commit is contained in:
Adam Ramberg 2019-09-05 15:58:29 +02:00 committed by Nicolò Ribaudo
parent d05bd9edc8
commit fcb77de901
73 changed files with 537 additions and 103 deletions

View File

@ -1,5 +1,6 @@
//@flow
import { SemVer } from "semver";
import { logPluginOrPolyfill } from "./debug";
import getOptionSpecificExcludesFor from "./get-option-specific-excludes";
import filterItems from "./filter-items";
@ -20,6 +21,9 @@ import availablePlugins from "./available-plugins";
import { filterStageFromList, prettifyTargets } from "./utils";
import { declare } from "@babel/helper-plugin-utils";
import typeof ModuleTransformationsType from "./module-transformations";
import type { BuiltInsOption, Targets, ModuleOption } from "./types";
export { isPluginRequired } from "./filter-items";
const pluginListWithoutProposals = filterStageFromList(
@ -56,6 +60,103 @@ export const transformIncludesAndExcludes = (opts: Array<string>): Object => {
);
};
export const getModulesPluginNames = ({
modules,
transformations,
shouldTransformESM,
shouldTransformDynamicImport,
}: {
modules: ModuleOption,
transformations: ModuleTransformationsType,
shouldTransformESM: boolean,
shouldTransformDynamicImport: boolean,
}) => {
const modulesPluginNames = [];
if (modules !== false && transformations[modules]) {
if (shouldTransformESM) {
modulesPluginNames.push(transformations[modules]);
}
if (
shouldTransformDynamicImport &&
shouldTransformESM &&
modules !== "umd"
) {
modulesPluginNames.push("proposal-dynamic-import");
} else {
if (shouldTransformDynamicImport) {
console.warn(
"Dynamic import can only be supported when transforming ES modules" +
" to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.",
);
}
modulesPluginNames.push("syntax-dynamic-import");
}
} else {
modulesPluginNames.push("syntax-dynamic-import");
}
return modulesPluginNames;
};
export const getPolyfillPlugins = ({
useBuiltIns,
corejs,
polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
regenerator,
debug,
}: {
useBuiltIns: BuiltInsOption,
corejs: typeof SemVer | null | false,
polyfillTargets: Targets,
include: Set<string>,
exclude: Set<string>,
proposals: boolean,
shippedProposals: boolean,
regenerator: boolean,
debug: boolean,
}) => {
const polyfillPlugins = [];
if (useBuiltIns === "usage" || useBuiltIns === "entry") {
const pluginOptions = {
corejs,
polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
regenerator,
debug,
};
if (corejs) {
if (useBuiltIns === "usage") {
if (corejs.major === 2) {
polyfillPlugins.push([addCoreJS2UsagePlugin, pluginOptions]);
} else {
polyfillPlugins.push([addCoreJS3UsagePlugin, pluginOptions]);
}
if (regenerator) {
polyfillPlugins.push([addRegeneratorUsagePlugin, pluginOptions]);
}
} else {
if (corejs.major === 2) {
polyfillPlugins.push([replaceCoreJS2EntryPlugin, pluginOptions]);
} else {
polyfillPlugins.push([replaceCoreJS3EntryPlugin, pluginOptions]);
if (!regenerator) {
polyfillPlugins.push([removeRegeneratorEntryPlugin, pluginOptions]);
}
}
}
}
}
return polyfillPlugins;
};
function supportsStaticESM(caller) {
return !!(caller && caller.supportsStaticESM);
}
@ -115,58 +216,46 @@ export default declare((api, opts) => {
const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
const transformations = filterItems(
const modulesPluginNames = getModulesPluginNames({
modules,
transformations: moduleTransformations,
// TODO: Remove the 'api.caller' check eventually. Just here to prevent
// unnecessary breakage in the short term for users on older betas/RCs.
shouldTransformESM:
modules !== "auto" || !api.caller || !api.caller(supportsStaticESM),
shouldTransformDynamicImport:
modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport),
});
const pluginNames = filterItems(
shippedProposals ? pluginList : pluginListWithoutProposals,
include.plugins,
exclude.plugins,
transformTargets,
null,
modulesPluginNames,
getOptionSpecificExcludesFor({ loose }),
pluginSyntaxMap,
);
const plugins = [];
const polyfillPlugins = getPolyfillPlugins({
useBuiltIns,
corejs,
polyfillTargets: targets,
include: include.builtIns,
exclude: exclude.builtIns,
proposals,
shippedProposals,
regenerator: pluginNames.has("transform-regenerator"),
debug,
});
const pluginUseBuiltIns = useBuiltIns !== false;
if (modules !== false && moduleTransformations[modules]) {
// TODO: Remove the 'api.caller' check eventually. Just here to prevent
// unnecessary breakage in the short term for users on older betas/RCs.
const shouldTransformESM =
modules !== "auto" || !api.caller || !api.caller(supportsStaticESM);
const shouldTransformDynamicImport =
modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport);
if (shouldTransformESM) {
// NOTE: not giving spec here yet to avoid compatibility issues when
// transform-modules-commonjs gets its spec mode
plugins.push([getPlugin(moduleTransformations[modules]), { loose }]);
}
if (
shouldTransformDynamicImport &&
shouldTransformESM &&
modules !== "umd"
) {
plugins.push([getPlugin("proposal-dynamic-import"), { loose }]);
} else {
if (shouldTransformDynamicImport) {
console.warn(
"Dynamic import can only be supported when transforming ES modules" +
" to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.",
);
}
plugins.push(getPlugin("syntax-dynamic-import"));
}
} else {
plugins.push(getPlugin("syntax-dynamic-import"));
}
transformations.forEach(pluginName =>
plugins.push([
const plugins = Array.from(pluginNames)
.map(pluginName => [
getPlugin(pluginName),
{ spec, loose, useBuiltIns: pluginUseBuiltIns },
]),
);
])
.concat(polyfillPlugins);
if (debug) {
console.log("@babel/preset-env: `DEBUG` option");
@ -174,8 +263,8 @@ export default declare((api, opts) => {
console.log(JSON.stringify(prettifyTargets(targets), null, 2));
console.log(`\nUsing modules transform: ${modules.toString()}`);
console.log("\nUsing plugins:");
transformations.forEach(transform => {
logPluginOrPolyfill(transform, targets, pluginList);
pluginNames.forEach(pluginName => {
logPluginOrPolyfill(pluginName, targets, pluginList);
});
if (!useBuiltIns) {
@ -183,46 +272,10 @@ export default declare((api, opts) => {
"\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.",
);
} else {
// NOTE: Polyfill plugins are outputting debug info internally
console.log(`\nUsing polyfills with \`${useBuiltIns}\` option:`);
}
}
if (useBuiltIns === "usage" || useBuiltIns === "entry") {
const regenerator = transformations.has("transform-regenerator");
const pluginOptions = {
corejs,
polyfillTargets: targets,
include: include.builtIns,
exclude: exclude.builtIns,
proposals,
shippedProposals,
regenerator,
debug,
};
if (corejs) {
if (useBuiltIns === "usage") {
if (corejs.major === 2) {
plugins.push([addCoreJS2UsagePlugin, pluginOptions]);
} else {
plugins.push([addCoreJS3UsagePlugin, pluginOptions]);
}
if (regenerator) {
plugins.push([addRegeneratorUsagePlugin, pluginOptions]);
}
} else {
if (corejs.major === 2) {
plugins.push([replaceCoreJS2EntryPlugin, pluginOptions]);
} else {
plugins.push([replaceCoreJS3EntryPlugin, pluginOptions]);
if (!regenerator) {
plugins.push([removeRegeneratorEntryPlugin, pluginOptions]);
}
}
}
}
}
return { plugins };
});

View File

@ -32,22 +32,28 @@ const validateTopLevelOptions = (options: Options) => {
}
};
const allPluginsList = [
...Object.keys(pluginsList),
const allPluginsList = Object.keys(pluginsList);
// NOTE: Since module plugins are handled seperatly compared to other plugins (via the "modules" option) it
// should only be possible to exclude and not include module plugins, otherwise it's possible that preset-env
// will add a module plugin twice.
const modulePlugins = [
"proposal-dynamic-import",
...Object.keys(moduleTransformations).map(m => moduleTransformations[m]),
];
const validIncludesAndExcludesWithoutCoreJS = new Set(allPluginsList);
const validIncludesAndExcludesWithCoreJS2 = new Set([
const getValidIncludesAndExcludes = (
type: "include" | "exclude",
corejs: number | false,
) =>
new Set([
...allPluginsList,
...Object.keys(corejs2Polyfills),
...defaultWebIncludes,
]);
const validIncludesAndExcludesWithCoreJS3 = new Set([
...allPluginsList,
...Object.keys(corejs3Polyfills),
...(type === "exclude" ? modulePlugins : []),
...(corejs
? corejs == 2
? [...Object.keys(corejs2Polyfills), ...defaultWebIncludes]
: Object.keys(corejs3Polyfills)
: []),
]);
const pluginToRegExp = (plugin: PluginListItem) => {
@ -59,14 +65,14 @@ const pluginToRegExp = (plugin: PluginListItem) => {
}
};
const selectPlugins = (regexp: RegExp | null, corejs: number | false) =>
Array.from(
corejs
? corejs == 2
? validIncludesAndExcludesWithCoreJS2
: validIncludesAndExcludesWithCoreJS3
: validIncludesAndExcludesWithoutCoreJS,
).filter(item => regexp instanceof RegExp && regexp.test(item));
const selectPlugins = (
regexp: RegExp | null,
type: "include" | "exclude",
corejs: number | false,
) =>
Array.from(getValidIncludesAndExcludes(type, corejs)).filter(
item => regexp instanceof RegExp && regexp.test(item),
);
const flatten = <T>(array: Array<Array<T>>): Array<T> => [].concat(...array);
@ -78,7 +84,7 @@ const expandIncludesAndExcludes = (
if (plugins.length === 0) return [];
const selectedPlugins = plugins.map(plugin =>
selectPlugins(pluginToRegExp(plugin), corejs),
selectPlugins(pluginToRegExp(plugin), type, corejs),
);
const invalidRegExpList = plugins.filter(
(p, i) => selectedPlugins[i].length === 0,

View File

@ -40,6 +40,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Successfully compiled 1 file with Babel.

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-optional-catch-binding { "android":"4" }
transform-named-capturing-groups-regex { "android":"4" }
transform-reserved-words { "android":"4" }
transform-modules-commonjs { "android":"4" }
proposal-dynamic-import { "android":"4" }
Using polyfills with `entry` option:

View File

@ -34,6 +34,8 @@ Using plugins:
transform-named-capturing-groups-regex { "electron":"0.36" }
transform-member-expression-literals { "electron":"0.36" }
transform-property-literals { "electron":"0.36" }
transform-modules-commonjs { "electron":"0.36" }
proposal-dynamic-import { "electron":"0.36" }
Using polyfills with `entry` option:

View File

@ -40,6 +40,7 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
syntax-dynamic-import { "chrome":"55" }
Using polyfills with `entry` option:

View File

@ -20,6 +20,8 @@ Using plugins:
proposal-json-strings { "node":"6" }
proposal-optional-catch-binding { "node":"6" }
transform-named-capturing-groups-regex { "node":"6" }
transform-modules-commonjs { "node":"6" }
proposal-dynamic-import { "node":"6" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -42,6 +42,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
proposal-optional-catch-binding { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
transform-named-capturing-groups-regex { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
transform-modules-commonjs { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
proposal-dynamic-import { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
Using polyfills with `entry` option:

View File

@ -50,6 +50,8 @@ Using plugins:
transform-named-capturing-groups-regex { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
transform-member-expression-literals { "electron":"0.36" }
transform-property-literals { "electron":"0.36" }
transform-modules-commonjs { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
proposal-dynamic-import { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
Using polyfills with `entry` option:

View File

@ -39,6 +39,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-optional-catch-binding { "chrome":"54", "ie":"10", "node":"6.10" }
transform-named-capturing-groups-regex { "chrome":"54", "ie":"10", "node":"6.10" }
transform-modules-commonjs { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-dynamic-import { "chrome":"54", "ie":"10", "node":"6.10" }
Using polyfills with `entry` option:

View File

@ -39,6 +39,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "ie":"10", "node":"6" }
proposal-optional-catch-binding { "chrome":"54", "ie":"10", "node":"6" }
transform-named-capturing-groups-regex { "chrome":"54", "ie":"10", "node":"6" }
transform-modules-commonjs { "chrome":"54", "ie":"10", "node":"6" }
proposal-dynamic-import { "chrome":"54", "ie":"10", "node":"6" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-optional-catch-binding { "android":"4" }
transform-named-capturing-groups-regex { "android":"4" }
transform-reserved-words { "android":"4" }
transform-modules-commonjs { "android":"4" }
proposal-dynamic-import { "android":"4" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -34,6 +34,8 @@ Using plugins:
transform-named-capturing-groups-regex { "electron":"0.36" }
transform-member-expression-literals { "electron":"0.36" }
transform-property-literals { "electron":"0.36" }
transform-modules-commonjs { "electron":"0.36" }
proposal-dynamic-import { "electron":"0.36" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -40,6 +40,7 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
syntax-dynamic-import { "chrome":"55" }
Using polyfills with `entry` option:

View File

@ -20,6 +20,8 @@ Using plugins:
proposal-json-strings { "node":"6" }
proposal-optional-catch-binding { "node":"6" }
transform-named-capturing-groups-regex { "node":"6" }
transform-modules-commonjs { "node":"6" }
proposal-dynamic-import { "node":"6" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -42,6 +42,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
proposal-optional-catch-binding { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
transform-named-capturing-groups-regex { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
transform-modules-commonjs { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
proposal-dynamic-import { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -14,6 +14,8 @@ Using plugins:
proposal-json-strings { "samsung":"8.2" }
proposal-optional-catch-binding { "samsung":"8.2" }
transform-named-capturing-groups-regex { "samsung":"8.2" }
transform-modules-commonjs { "samsung":"8.2" }
proposal-dynamic-import { "samsung":"8.2" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -50,6 +50,8 @@ Using plugins:
transform-named-capturing-groups-regex { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
transform-member-expression-literals { "electron":"0.36" }
transform-property-literals { "electron":"0.36" }
transform-modules-commonjs { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
proposal-dynamic-import { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" }
Using polyfills with `entry` option:

View File

@ -39,6 +39,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-optional-catch-binding { "chrome":"54", "ie":"10", "node":"6.10" }
transform-named-capturing-groups-regex { "chrome":"54", "ie":"10", "node":"6.10" }
transform-modules-commonjs { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-dynamic-import { "chrome":"54", "ie":"10", "node":"6.10" }
Using polyfills with `entry` option:

View File

@ -39,6 +39,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-optional-catch-binding { "chrome":"54", "ie":"10", "node":"6.10" }
transform-named-capturing-groups-regex { "chrome":"54", "ie":"10", "node":"6.10" }
transform-modules-commonjs { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-dynamic-import { "chrome":"54", "ie":"10", "node":"6.10" }
Using polyfills with `entry` option:

View File

@ -39,6 +39,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-optional-catch-binding { "chrome":"54", "ie":"10", "node":"6.10" }
transform-named-capturing-groups-regex { "chrome":"54", "ie":"10", "node":"6.10" }
transform-modules-commonjs { "chrome":"54", "ie":"10", "node":"6.10" }
proposal-dynamic-import { "chrome":"54", "ie":"10", "node":"6.10" }
Using polyfills with `entry` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -39,6 +39,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "ie":"10", "node":"6" }
proposal-optional-catch-binding { "chrome":"54", "ie":"10", "node":"6" }
transform-named-capturing-groups-regex { "chrome":"54", "ie":"10", "node":"6" }
transform-modules-commonjs { "chrome":"54", "ie":"10", "node":"6" }
proposal-dynamic-import { "chrome":"54", "ie":"10", "node":"6" }
Using polyfills with `entry` option:

View File

@ -20,6 +20,8 @@ Using plugins:
proposal-json-strings { "node":"6" }
proposal-optional-catch-binding { "node":"6" }
transform-named-capturing-groups-regex { "node":"6" }
transform-modules-commonjs { "node":"6" }
proposal-dynamic-import { "node":"6" }
Using polyfills with `entry` option:

View File

@ -38,6 +38,8 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
transform-modules-commonjs {}
proposal-dynamic-import {}
Using polyfills with `entry` option:

View File

@ -43,6 +43,7 @@ Using plugins:
transform-member-expression-literals {}
transform-property-literals {}
transform-reserved-words {}
syntax-dynamic-import { "chrome":"55" }
Using polyfills with `entry` option:

View File

@ -39,6 +39,8 @@ Using plugins:
proposal-json-strings { "chrome":"54", "ie":"10", "node":"6" }
proposal-optional-catch-binding { "chrome":"54", "ie":"10", "node":"6" }
transform-named-capturing-groups-regex { "chrome":"54", "ie":"10", "node":"6" }
transform-modules-commonjs { "chrome":"54", "ie":"10", "node":"6" }
proposal-dynamic-import { "chrome":"54", "ie":"10", "node":"6" }
Using polyfills with `entry` option:

View File

@ -27,6 +27,8 @@ Using plugins:
proposal-json-strings { "firefox":"52", "node":"7.4" }
proposal-optional-catch-binding { "firefox":"52", "node":"7.4" }
transform-named-capturing-groups-regex { "firefox":"52", "node":"7.4" }
transform-modules-commonjs { "firefox":"52", "node":"7.4" }
proposal-dynamic-import { "firefox":"52", "node":"7.4" }
Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Successfully compiled 1 file with Babel.

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -15,6 +15,8 @@ Using plugins:
proposal-json-strings { "chrome":"55" }
proposal-optional-catch-binding { "chrome":"55" }
transform-named-capturing-groups-regex { "chrome":"55" }
transform-modules-commonjs { "chrome":"55" }
proposal-dynamic-import { "chrome":"55" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -12,6 +12,8 @@ Using plugins:
syntax-object-rest-spread { "chrome":"71" }
syntax-json-strings { "chrome":"71" }
syntax-optional-catch-binding { "chrome":"71" }
transform-modules-commonjs { "chrome":"71" }
proposal-dynamic-import { "chrome":"71" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -15,6 +15,8 @@ Using plugins:
proposal-json-strings { "chrome":"55" }
proposal-optional-catch-binding { "chrome":"55" }
transform-named-capturing-groups-regex { "chrome":"55" }
transform-modules-commonjs { "chrome":"55" }
proposal-dynamic-import { "chrome":"55" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -38,6 +38,8 @@ Using plugins:
proposal-json-strings { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-optional-catch-binding { "chrome":"52", "firefox":"50", "ie":"11" }
transform-named-capturing-groups-regex { "chrome":"52", "firefox":"50", "ie":"11" }
transform-modules-commonjs { "chrome":"52", "firefox":"50", "ie":"11" }
proposal-dynamic-import { "chrome":"52", "firefox":"50", "ie":"11" }
Using polyfills with `usage` option:

View File

@ -26,6 +26,7 @@ Using plugins:
proposal-json-strings { "chrome":"61", "edge":"16", "firefox":"60", "ios":"10.3", "opera":"48", "safari":"10.1" }
proposal-optional-catch-binding { "chrome":"61", "edge":"16", "ios":"10.3", "opera":"48", "safari":"10.1" }
transform-named-capturing-groups-regex { "chrome":"61", "edge":"16", "firefox":"60", "ios":"10.3", "opera":"48", "safari":"10.1" }
syntax-dynamic-import { "chrome":"61", "edge":"16", "firefox":"60", "ios":"10.3", "opera":"48", "safari":"10.1" }
Using polyfills with `usage` option:

View File

@ -26,6 +26,7 @@ Using plugins:
proposal-json-strings { "chrome":"61", "edge":"16", "firefox":"60", "ios":"10.3", "opera":"48", "safari":"10.1" }
proposal-optional-catch-binding { "chrome":"61", "edge":"16", "ios":"10.3", "opera":"48", "safari":"10.1" }
transform-named-capturing-groups-regex { "chrome":"61", "edge":"16", "firefox":"60", "ios":"10.3", "opera":"48", "safari":"10.1" }
syntax-dynamic-import { "chrome":"61", "edge":"16", "firefox":"60", "ios":"10.3", "opera":"48", "safari":"10.1" }
Using polyfills with `usage` option:

View File

@ -20,5 +20,7 @@ Using plugins:
proposal-json-strings { "safari":"10" }
proposal-optional-catch-binding { "safari":"10" }
transform-named-capturing-groups-regex { "safari":"10" }
transform-modules-commonjs { "safari":"10" }
proposal-dynamic-import { "safari":"10" }
Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.

View File

@ -1,6 +1,19 @@
"use strict";
const babelPresetEnv = require("../lib/index");
const addCoreJS2UsagePlugin = require("../lib/polyfills/corejs2/usage-plugin")
.default;
const addCoreJS3UsagePlugin = require("../lib/polyfills/corejs3/usage-plugin")
.default;
const addRegeneratorUsagePlugin = require("../lib/polyfills/regenerator/usage-plugin")
.default;
const replaceCoreJS2EntryPlugin = require("../lib/polyfills/corejs2/entry-plugin")
.default;
const replaceCoreJS3EntryPlugin = require("../lib/polyfills/corejs3/entry-plugin")
.default;
const removeRegeneratorEntryPlugin = require("../lib/polyfills/regenerator/entry-plugin")
.default;
const transformations = require("../lib/module-transformations").default;
describe("babel-preset-env", () => {
describe("transformIncludesAndExcludes", () => {
@ -24,4 +37,215 @@ describe("babel-preset-env", () => {
});
});
});
describe("getModulesPluginNames", () => {
describe("modules is set to false", () => {
it("returns only syntax-dynamic-import", () => {
expect(
babelPresetEnv.getModulesPluginNames({
modules: false,
transformations,
shouldTransformESM: false,
shouldTransformDynamicImport: false,
}),
).toEqual(["syntax-dynamic-import"]);
});
});
describe("modules is not set to false", () => {
describe("ESMs should not be transformed", () => {
it("returns syntax-dynamic-import", () => {
expect(
babelPresetEnv.getModulesPluginNames({
modules: "commonjs",
transformations,
shouldTransformESM: false,
shouldTransformDynamicImport: false,
}),
).toEqual(["syntax-dynamic-import"]);
});
});
describe("ESMs should be transformed", () => {
describe("dynamic imports should not be transformed", () => {
it("returns specified modules transform and syntax-dynamic-import", () => {
expect(
babelPresetEnv.getModulesPluginNames({
modules: "commonjs",
transformations,
shouldTransformESM: true,
shouldTransformDynamicImport: false,
}),
).toEqual(["transform-modules-commonjs", "syntax-dynamic-import"]);
});
});
describe("dynamic imports should be transformed", () => {
it("returns specified modules transform and proposal-dynamic-import", () => {
expect(
babelPresetEnv.getModulesPluginNames({
modules: "systemjs",
transformations,
shouldTransformESM: true,
shouldTransformDynamicImport: true,
}),
).toEqual([
"transform-modules-systemjs",
"proposal-dynamic-import",
]);
});
});
});
});
});
describe("getPolyfillPlugins", () => {
const staticProps = {
polyfillTargets: [],
include: new Set(),
exclude: new Set(),
proposals: false,
shippedProposals: false,
debug: false,
};
describe("useBuiltIns is false", () => {
it("returns no polyfill plugins", () => {
expect(
babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: false,
corejs: false,
regenerator: false,
},
staticProps,
),
),
).toEqual([]);
});
});
describe("useBuiltIns is not false", () => {
describe("corejs is not given", () => {
it("returns no polyfill plugins", () => {
expect(
babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: "usage",
corejs: false,
regenerator: false,
},
staticProps,
),
),
).toEqual([]);
});
});
describe("useBuiltIns is set to usage", () => {
describe("using corejs 2", () => {
it("returns an array with core js 2 usage plugin", () => {
const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: "usage",
corejs: { major: 2 },
regenerator: false,
},
staticProps,
),
);
expect(polyfillPlugins.length).toBe(1);
expect(polyfillPlugins[0][0]).toEqual(addCoreJS2UsagePlugin);
});
});
describe("using corejs 3", () => {
describe("regenerator is set to false", () => {
it("returns an array with core js 3 usage plugin", () => {
const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: "usage",
corejs: { major: 3 },
regenerator: false,
},
staticProps,
),
);
expect(polyfillPlugins.length).toBe(1);
expect(polyfillPlugins[0][0]).toEqual(addCoreJS3UsagePlugin);
});
});
describe("regenerator is set to true", () => {
it("returns an array with core js 3 usage plugin and add regenerator usage plugin", () => {
const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: "usage",
corejs: { major: 3 },
regenerator: true,
},
staticProps,
),
);
expect(polyfillPlugins.length).toBe(2);
expect(polyfillPlugins[0][0]).toEqual(addCoreJS3UsagePlugin);
expect(polyfillPlugins[1][0]).toEqual(addRegeneratorUsagePlugin);
});
});
});
});
describe("useBuiltIns is set to entry", () => {
describe("using corejs 2", () => {
it("returns an array with core js 2 entry plugin", () => {
const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: "entry",
corejs: { major: 2 },
regenerator: true,
},
staticProps,
),
);
expect(polyfillPlugins.length).toBe(1);
expect(polyfillPlugins[0][0]).toEqual(replaceCoreJS2EntryPlugin);
});
});
describe("using corejs 3", () => {
describe("regenerator is set to true", () => {
it("returns an array with core js 3 entry plugin", () => {
const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: "entry",
corejs: { major: 3 },
regenerator: true,
},
staticProps,
),
);
expect(polyfillPlugins.length).toBe(1);
expect(polyfillPlugins[0][0]).toEqual(replaceCoreJS3EntryPlugin);
});
});
describe("regenerator is set to false", () => {
it("returns an array with core js 3 entry plugin and remove regenerator entry plugin", () => {
const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
Object.assign(
{
useBuiltIns: "entry",
corejs: { major: 3 },
regenerator: false,
},
staticProps,
),
);
expect(polyfillPlugins.length).toBe(2);
expect(polyfillPlugins[0][0]).toEqual(replaceCoreJS3EntryPlugin);
expect(polyfillPlugins[1][0]).toEqual(
removeRegeneratorEntryPlugin,
);
});
});
});
});
});
});
});

View File

@ -84,6 +84,24 @@ describe("normalize-options", () => {
});
});
});
it("throws when including module plugins", () => {
expect(() =>
normalizeOptions.default({ include: ["proposal-dynamic-import"] }),
).toThrow();
expect(() =>
normalizeOptions.default({ include: ["transform-modules-amd"] }),
).toThrow();
});
it("allows exclusion of module plugins ", () => {
expect(() =>
normalizeOptions.default({ exclude: ["proposal-dynamic-import"] }),
).not.toThrow();
expect(() =>
normalizeOptions.default({ exclude: ["transform-modules-commonjs"] }),
).not.toThrow();
});
});
describe("Config format validation", () => {