-
-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Open
heygrady
wants to merge
4
commits into
facebook:main
Choose a base branch
from
heygrady:preset-env-modern
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c622060
exclude unnecessary plugins for target browsers
heygrady 3fa10be
remove unnecessary plugin-proposal-object-rest-spread customization
heygrady ab14e01
use new babel packages designed for this use-case
heygrady f6a6328
proper usage of isRequired
heygrady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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, | ||
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. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/babel-preset-react-app/helpers/getRequiredPlugins.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'.