Skip to content

Commit

Permalink
Add support for top-level targets to @babel/preset-env
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 19, 2020
1 parent 2cccc23 commit a196b73
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 28 deletions.
90 changes: 62 additions & 28 deletions packages/babel-preset-env/src/index.js
Expand Up @@ -206,6 +206,45 @@ export const getPolyfillPlugins = ({
return polyfillPlugins;
};

function getLocalTargets(
optionsTargets,
ignoreBrowserslistConfig,
configPath,
browserslistEnv,
) {
// TODO: remove this in next major
let hasUglifyTarget = false;

if (optionsTargets?.uglify) {
hasUglifyTarget = true;
delete optionsTargets.uglify;

console.log("");
console.log("The uglify target has been deprecated. Set the top level");
console.log("option `forceAllTransforms: true` instead.");
console.log("");
}

if (optionsTargets?.esmodules && optionsTargets.browsers) {
console.log("");
console.log(
"@babel/preset-env: esmodules and browsers targets have been specified together.",
);
console.log(
// $FlowIgnore
`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`,
);
console.log("");
}

const localTargets = getTargets(
// $FlowIgnore optionsTargets doesn't have an "uglify" property anymore
(optionsTargets: InputTargets),
{ ignoreBrowserslistConfig, configPath, browserslistEnv },
);
return { hasUglifyTarget, localTargets };
}

function supportsStaticESM(caller) {
return !!caller?.supportsStaticESM;
}
Expand All @@ -225,6 +264,9 @@ function supportsTopLevelAwait(caller) {
export default declare((api, opts) => {
api.assertVersion(7);

// TODO(Babel 8): api.targets() is always defined, no need to fallback
const babelTargets = api.targets?.() ?? {};

const {
bugfixes,
configPath,
Expand All @@ -242,41 +284,33 @@ export default declare((api, opts) => {
corejs: { version: corejs, proposals },
browserslistEnv,
} = normalizeOptions(opts);
// TODO: remove this in next major
let hasUglifyTarget = false;

if (optionsTargets?.uglify) {
hasUglifyTarget = true;
delete optionsTargets.uglify;

console.log("");
console.log("The uglify target has been deprecated. Set the top level");
console.log("option `forceAllTransforms: true` instead.");
console.log("");
}

if (optionsTargets?.esmodules && optionsTargets.browsers) {
console.log("");
console.log(
"@babel/preset-env: esmodules and browsers targets have been specified together.",
);
console.log(
// $FlowIgnore
`\`browsers\` target, \`${optionsTargets.browsers}\` will be ignored.`,
let targets = babelTargets;
let transformTargets = forceAllTransforms ? {} : babelTargets;

if (
// If any browserslist-related option is specified, fallback to the old
// behavior of not using the targets specified in the top-level options.
opts.targets ||
opts.configPath ||
opts.browserslistEnv ||
opts.ignoreBrowserslistConfig
) {
const { hasUglifyTarget, localTargets } = getLocalTargets(
optionsTargets,
ignoreBrowserslistConfig,
configPath,
browserslistEnv,
);
console.log("");

targets = localTargets;
if (hasUglifyTarget) transformTargets = {};
else if (!forceAllTransforms) transformTargets = localTargets;
}

const targets = getTargets(
// $FlowIgnore optionsTargets doesn't have an "uglify" property anymore
(optionsTargets: InputTargets),
{ ignoreBrowserslistConfig, configPath, browserslistEnv },
);
const include = transformIncludesAndExcludes(optionsInclude);
const exclude = transformIncludesAndExcludes(optionsExclude);

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

const compatData = getPluginList(shippedProposals, bugfixes);
const shouldSkipExportNamespaceFrom =
(modules === "auto" && api.caller?.(supportsExportNamespaceFrom)) ||
Expand Down
@@ -0,0 +1 @@
foo?.bar;
@@ -0,0 +1,8 @@
{
"validateLogs": true,
"ignoreOutput": true,
"targets": "chrome 80",
"presets": [
["env", { "debug": true, "targets": "chrome 60" }]
]
}
@@ -0,0 +1,26 @@
@babel/preset-env: `DEBUG` option

Using targets:
{
"chrome": "60"
}

Using modules transform: auto

Using plugins:
proposal-numeric-separator { "chrome":"60" }
proposal-logical-assignment-operators { "chrome":"60" }
proposal-nullish-coalescing-operator { "chrome":"60" }
proposal-optional-chaining { "chrome":"60" }
proposal-json-strings { "chrome":"60" }
proposal-optional-catch-binding { "chrome":"60" }
proposal-async-generator-functions { "chrome":"60" }
syntax-object-rest-spread { "chrome":"60" }
transform-dotall-regex { "chrome":"60" }
proposal-unicode-property-regex { "chrome":"60" }
transform-named-capturing-groups-regex { "chrome":"60" }
proposal-export-namespace-from { "chrome":"60" }
transform-modules-commonjs { "chrome":"60" }
proposal-dynamic-import { "chrome":"60" }

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
@@ -0,0 +1 @@
foo?.bar;
@@ -0,0 +1,8 @@
{
"validateLogs": true,
"ignoreOutput": true,
"targets": "chrome 80",
"presets": [
["env", { "debug": true }]
]
}
@@ -0,0 +1,23 @@
@babel/preset-env: `DEBUG` option

Using targets:
{
"chrome": "80"
}

Using modules transform: auto

Using plugins:
syntax-numeric-separator { "chrome":"80" }
proposal-logical-assignment-operators { "chrome":"80" }
syntax-nullish-coalescing-operator { "chrome":"80" }
syntax-optional-chaining { "chrome":"80" }
syntax-json-strings { "chrome":"80" }
syntax-optional-catch-binding { "chrome":"80" }
syntax-async-generators { "chrome":"80" }
syntax-object-rest-spread { "chrome":"80" }
transform-modules-commonjs { "chrome":"80" }
proposal-dynamic-import { "chrome":"80" }
proposal-export-namespace-from {}

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.

0 comments on commit a196b73

Please sign in to comment.