Skip to content

Commit

Permalink
Use filter-items to excl / incl module plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamRamberg committed Jul 17, 2019
1 parent 560df5e commit a9a6832
Show file tree
Hide file tree
Showing 68 changed files with 479 additions and 84 deletions.
216 changes: 132 additions & 84 deletions packages/babel-preset-env/src/index.js
@@ -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";
Expand All @@ -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 } from "./types";

export { isPluginRequired } from "./filter-items";

const pluginListWithoutProposals = filterStageFromList(
Expand Down Expand Up @@ -56,6 +60,103 @@ export const transformIncludesAndExcludes = (opts: Array<string>): Object => {
);
};

export const getModulesPluginNames = ({
moduleTransformation,
shouldTransformESM,
shouldTransformDynamicImport,
}: {
moduleTransformation: $Values<ModuleTransformationsType> | null,
shouldTransformESM: boolean,
shouldTransformDynamicImport: boolean,
}) => {
const modulesPluginNames = [];
if (moduleTransformation) {
if (shouldTransformESM) {
// NOTE: not giving spec here yet to avoid compatibility issues when
// transform-modules-commonjs gets its spec mode
modulesPluginNames.push(moduleTransformation);
}

if (
shouldTransformDynamicImport &&
shouldTransformESM &&
moduleTransformation !== "transform-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);
}
Expand Down Expand Up @@ -115,118 +216,65 @@ export default declare((api, opts) => {

const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;

const transformations = filterItems(
const modulesPluginNames = getModulesPluginNames({
moduleTransformation: moduleTransformations[modules.toString()] || null,
// 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 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)) &&
!exclude.plugins.has(moduleTransformations[modules]);
const shouldTransformDynamicImport =
(modules !== "auto" ||
!api.caller ||
!api.caller(supportsDynamicImport)) &&
!exclude.plugins.has("proposal-dynamic-import");

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"));
}
const polyfillPlugins = getPolyfillPlugins({
useBuiltIns,
corejs,
polyfillTargets: targets,
include: include.builtIns,
exclude: exclude.builtIns,
proposals,
shippedProposals,
regenerator: pluginNames.has("transform-regenerator"),
debug,
});

transformations.forEach(pluginName =>
plugins.push([
const pluginUseBuiltIns = useBuiltIns !== false;
const plugins = Array.from(pluginNames)
.map(pluginName => [
getPlugin(pluginName),
{ spec, loose, useBuiltIns: pluginUseBuiltIns },
]),
);
])
.concat(polyfillPlugins);

if (debug) {
console.log("@babel/preset-env: `DEBUG` option");
console.log("\nUsing targets:");
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) {
console.log(
"\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 };
});
Expand Up @@ -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.
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down
Expand Up @@ -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:

Expand Down

0 comments on commit a9a6832

Please sign in to comment.