Skip to content

Commit f4f20a2

Browse files
authoredMar 14, 2019
Support browserslist in @babel/preset-env (#6608)
* Add browserslist support to @babel/preset-env * Support @babel/polyfill in entry point
1 parent 88d6f02 commit f4f20a2

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed
 

‎docusaurus/docs/supported-browsers-features.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar_label: Supported Browsers and Features
66

77
## Supported Browsers
88

9-
By default, the generated project supports all modern browsers. Support for Internet Explorer 9, 10, and 11 requires [polyfills](https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md).
9+
By default, the generated project supports all modern browsers. Support for Internet Explorer 9, 10, and 11 requires polyfills. For a minimum set of polyfills to support older browsers, use [react-app-polyfill](https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md). To polyfill other language features, see the [Adding Polyfills](#adding-polyfills) section below
1010

1111
## Supported Language Features
1212

@@ -26,3 +26,34 @@ While we recommend using experimental proposals with some caution, Facebook heav
2626
Note that **this project includes no [polyfills](https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md)** by default.
2727

2828
If you use any other ES6+ features that need **runtime support** (such as `Array.from()` or `Symbol`), make sure you are [including the appropriate polyfills manually](https://github.com/facebook/create-react-app/blob/master/packages/react-app-polyfill/README.md), or that the browsers you are targeting already support them.
29+
30+
## Adding Polyfills
31+
32+
You can install [`@babel/polyfill`](https://babeljs.io/docs/en/babel-polyfill) as a dependency in your application, and import it at the very top of your app's entry point (`src/index.js` or `src/index.tsx`) to emulate a full ES2015+ environment. Your `browerslist` configuration will be used to only include the polyfills necessary by your target browsers.
33+
34+
## Configuring Supported Browsers
35+
36+
By default, the generated project includes a [`browerslist`](https://github.com/browserslist/browserslist) configuration in your `package.json` file to target a broad range of browsers based on global usage (`> 0.2%`) for production builds, and modern browsers for development. This gives a good development experience, especially when using language features such as async/await, but still provides high compatibility with many browsers in production.
37+
38+
The `browserslist` configuration controls the outputted JavaScript so that the emitted code will be compatible with the browsers specified. The `production` list will be used when creating a production build by running the `build` script, and the `development` list will be used when running the `start` script. You can use [https://browserl.ist](https://browserl.ist/?q=%3E+0.2%25%2C+not+dead%2C+not+op_mini+all) to see the browsers supported by your configured `browserslist`.
39+
40+
Here is an example `browserslist` that is specified in `package.json`:
41+
42+
```json
43+
"browserslist": {
44+
"production": [
45+
">0.2%",
46+
"not dead",
47+
"not op_mini all"
48+
],
49+
"development": [
50+
"last 1 chrome version",
51+
"last 1 firefox version",
52+
"last 1 safari version"
53+
]
54+
}
55+
```
56+
57+
> Note that this does not include polyfills automatically for you. You will still need to polyfill language features (see above) as needed based on the browsers your are supporting.
58+
59+
> When editing the `browerslist` config, you may notice that your changes don't get picked up right away. This is due to an [issue in babel-loader](https://github.com/babel/babel-loader/issues/690) not detecting the change in your `package.json`. An easy solution is to delete the `node_modules/.cache` folder and try again.

‎packages/babel-preset-react-app/create.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,8 @@ module.exports = function(api, opts, env) {
7979
// Latest stable ECMAScript features
8080
require('@babel/preset-env').default,
8181
{
82-
// We want Create React App to be IE 9 compatible until React itself
83-
// no longer works with IE 9
84-
targets: {
85-
ie: 9,
86-
},
87-
// Users cannot override this behavior because this Babel
88-
// configuration is highly tuned for ES5 support
89-
ignoreBrowserslistConfig: true,
90-
// If users import all core-js they're probably not concerned with
91-
// bundle size. We shouldn't rely on magic to try and shrink it.
92-
useBuiltIns: false,
82+
// Allow importing @babel/polyfill in entrypoint and use browserlist to select polyfills
83+
useBuiltIns: 'entry',
9384
// Do not transform modules to CJS
9485
modules: false,
9586
// Exclude transforms that make all code slower

‎packages/react-dev-utils/browsersHelper.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ const inquirer = require('inquirer');
1313
const pkgUp = require('pkg-up');
1414
const fs = require('fs');
1515

16-
const defaultBrowsers = [
17-
'>0.2%',
18-
'not dead',
19-
'not ie <= 11',
20-
'not op_mini all',
21-
];
16+
const defaultBrowsers = {
17+
production: ['>0.2%', 'not dead', 'not op_mini all'],
18+
development: [
19+
'last 1 chrome version',
20+
'last 1 firefox version',
21+
'last 1 safari version',
22+
],
23+
};
2224

2325
function shouldSetBrowsers(isInteractive) {
2426
if (!isInteractive) {

‎packages/react-scripts/package.json

+12-6
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@
7878
"optionalDependencies": {
7979
"fsevents": "1.2.7"
8080
},
81-
"browserslist": [
82-
">0.2%",
83-
"not dead",
84-
"not ie <= 11",
85-
"not op_mini all"
86-
]
81+
"browserslist": {
82+
"production": [
83+
">0.2%",
84+
"not dead",
85+
"not op_mini all"
86+
],
87+
"development": [
88+
"last 1 chrome version",
89+
"last 1 firefox version",
90+
"last 1 safari version"
91+
]
92+
}
8793
}

0 commit comments

Comments
 (0)