Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 26.9k
exclude unnecessary plugins for target browsers#8030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c6220603fa10beab14e01f6a6328File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,6 +8,8 @@ | ||
| const path = require('path'); | ||
| const getRequiredPlugins = require('./helpers/getRequiredPlugins'); | ||
| const validateBoolOption = (name, value, defaultValue) => { | ||
| if (typeof value === 'undefined') { | ||
| value = defaultValue; | ||
| @@ -64,15 +66,24 @@ module.exports = function(api, opts, env) { | ||
| ); | ||
| } | ||
| const targets = isEnvTest ? { node: 'current' } : undefined; | ||
| // Some plugins need custom overrides which inadvertantly enables them for | ||
| // browsers that don't really need them. Here we check whether these plugins | ||
| // are actually required and skip them if they are not. | ||
| const { | ||
| 'transform-destructuring': isDestructuringTransformRequired, | ||
| 'transform-regenerator': isRegeneratorTransformRequired, | ||
| 'proposal-object-rest-spread': isObjectRestSpreadProposalRequired, | ||
| } = getRequiredPlugins(targets); | ||
| return { | ||
| presets: [ | ||
| isEnvTest && [ | ||
| // ES features necessary for user's Node version | ||
| require('@babel/preset-env').default, | ||
| { | ||
| targets: { | ||
| node: 'current', | ||
| }, | ||
| targets, | ||
| }, | ||
| ], | ||
| (isEnvProduction || isEnvDevelopment) && [ | ||
| @@ -96,9 +107,12 @@ module.exports = function(api, opts, env) { | ||
| // Adds component stack to warning messages | ||
| // Adds __self attribute to JSX which React will use for some warnings | ||
| development: isEnvDevelopment || isEnvTest, | ||
| // Will use the native built-in instead of trying to polyfill | ||
| // Use native spread when possible. | ||
| // Or use the native built-in instead of trying to polyfill | ||
| // behavior for any plugins that require one. | ||
| useBuiltIns: true, | ||
| [isObjectRestSpreadProposalRequired | ||
| ? 'useBuiltIns' | ||
| : 'useSpread']: true, | ||
| }, | ||
| ], | ||
| isTypeScriptEnabled && [require('@babel/preset-typescript').default], | ||
| @@ -117,10 +131,11 @@ module.exports = function(api, opts, env) { | ||
| // Experimental macros support. Will be documented after it's had some time | ||
| // in the wild. | ||
| require('babel-plugin-macros'), | ||
| // Necessary to include regardless of the environment because | ||
| // in practice some other transforms (such as object-rest-spread) | ||
| // don't work without it: https://github.com/babel/babel/issues/7215 | ||
| [ | ||
| // Historically there was a bug that made this plugin required. | ||
| // https://github.com/babel/babel/issues/7215 | ||
| // This plugin is no longer required to make plugin-proposal-object-rest-spread work: | ||
| // https://github.com/babel/babel/pull/10275 | ||
| isDestructuringTransformRequired && [ | ||
| require('@babel/plugin-transform-destructuring').default, | ||
Comment on lines
134
to
139
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we need to undo this custom override when it's not needed. | ||
| { | ||
| // Use loose mode for performance: | ||
| @@ -156,15 +171,6 @@ module.exports = function(api, opts, env) { | ||
| ], | ||
| // Adds Numeric Separators | ||
| require('@babel/plugin-proposal-numeric-separator').default, | ||
| // The following two plugins use Object.assign directly, instead of Babel's | ||
| // extends helper. Note that this assumes `Object.assign` is available. | ||
| // { ...todo, completed: true } | ||
| [ | ||
| require('@babel/plugin-proposal-object-rest-spread').default, | ||
| { | ||
| useBuiltIns: true, | ||
| }, | ||
| ], | ||
| // Polyfills the runtime needed for async/await, generators, and friends | ||
| // https://babeljs.io/docs/en/babel-plugin-transform-runtime | ||
| [ | ||
| @@ -176,7 +182,7 @@ module.exports = function(api, opts, env) { | ||
| // explicitly resolving to match the provided helper functions. | ||
| // https://github.com/babel/babel/issues/10261 | ||
| version: require('@babel/runtime/package.json').version, | ||
| regenerator: true, | ||
| regenerator: isRegeneratorTransformRequired, | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we want to disable regenerator when it's not needed. | ||
| // https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules | ||
| // We should turn this on once the lowest version of Node LTS | ||
| // supports ES Modules. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Copyright (c) 2015-present, Facebook, Inc. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
| 'use strict'; | ||
| const { | ||
| default: getTargets, | ||
| isBrowsersQueryValid, | ||
| isRequired, | ||
| } = require('@babel/helper-compilation-targets'); | ||
| const data = require('@babel/compat-data/plugins'); | ||
| // Copying normalizeTargets (because it is not exported) | ||
| // https://github.com/babel/babel/blob/04354d155689405ba688d4b400702710f9cccc97/packages/babel-preset-env/src/normalize-options.js#L121-L129 | ||
| const normalizeTargets = targets => { | ||
| // TODO: Allow to use only query or strings as a targets from next breaking change. | ||
| if (isBrowsersQueryValid(targets)) { | ||
| return { browsers: targets }; | ||
| } | ||
| return { | ||
| ...targets, | ||
| }; | ||
| }; | ||
| // Test which plugins our target browsers require | ||
| module.exports = function getRequiredPlugins(targets) { | ||
| const requiredPlugins = {}; | ||
| const currentTargets = getTargets(normalizeTargets(targets)); | ||
| for (const name of Object.keys(data)) { | ||
| requiredPlugins[name] = isRequired(name, currentTargets); | ||
| } | ||
| return requiredPlugins; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we need to use native spread whenever possible. This requires knowing if our targets require 'proposal-object-rest-spread'.