Skip to content

Commit

Permalink
Update to core-js@3
Browse files Browse the repository at this point in the history
  • Loading branch information
ianschmitz committed Apr 6, 2019
1 parent 91ddd61 commit 7114965
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/babel-preset-react-app/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = function(api, opts, env) {
useBuiltIns: 'entry',
// Set the corejs version we are using to avoid warnings in console
// This will need to change once we upgrade to corejs@3
corejs: 2,
corejs: 3,
// Do not transform modules to CJS
modules: false,
// Exclude transforms that make all code slower
Expand Down
59 changes: 46 additions & 13 deletions packages/react-app-polyfill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
This package includes polyfills for various browsers.
It includes minimum requirements and commonly used language features used by [Create React App](https://github.com/facebook/create-react-app) projects.

### Features

Each polyfill ensures the following language features are present:

1. `Promise` (for `async` / `await` support)
1. `window.fetch` (a Promise-based way to make web requests in the browser)
1. `Object.assign` (a helper required for Object Spread, i.e. `{ ...a, ...b }`)
1. `Symbol` (a built-in object used by `for...of` syntax and friends)
1. `Array.from` (a built-in static method used by array spread, i.e. `[...arr]`)

*If you need more features, you must include them manually.*

### Usage

First, install the package using Yarn or npm:
Expand All @@ -29,7 +17,19 @@ or
yarn add react-app-polyfill
```

Now, you can import the entry point for the minimal version you intend to support. For example, if you import the IE9 entry point, this will include IE10 and IE11 support.
## Supporting Internet Explorer

You can import the entry point for the minimal version you intend to support to ensure that the minimum langauge features are present that are required to use Create React App. For example, if you import the IE9 entry point, this will include IE10 and IE11 support.

These modules ensures the following language features are present:

1. `Promise` (for `async` / `await` support)
1. `window.fetch` (a Promise-based way to make web requests in the browser)
1. `Object.assign` (a helper required for Object Spread, i.e. `{ ...a, ...b }`)
1. `Symbol` (a built-in object used by `for...of` syntax and friends)
1. `Array.from` (a built-in static method used by array spread, i.e. `[...arr]`)

_If you need more features, see the [Polyfilling other language features](#polyfilling-other-language-features) section below._

#### Internet Explorer 9

Expand All @@ -48,3 +48,36 @@ import 'react-app-polyfill/ie11';

// ...
```

## Polyfilling other language features

You can also polyfill stable language features not available in your target browsers. If you're using this in Create React App, it will automatically use the `browserslist` you've defined to only include polyfills needed by your target browsers when importing the `stable` polyfill. **Make sure to follow the Internet Explorer steps above if you need to support Internet Explorer in your application**.

```js
// This must be the first line in src/index.js
import 'react-app-polyfill/stable';

// ...
```

If you are supporting Internet Explorer 9/11 you should include both the `ie9`/`ie11` and `stable` modules:

For IE9:

```js
// These must be the first lines in src/index.js
import 'react-app-polyfill/ie9';
import 'react-app-polyfill/stable';

// ...
```

For IE11:

```js
// These must be the first lines in src/index.js
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

// ...
```
4 changes: 2 additions & 2 deletions packages/react-app-polyfill/ie11.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ if (typeof window !== 'undefined') {
Object.assign = require('object-assign');

// Support for...of (a commonly used syntax feature that requires Symbols)
require('core-js/es6/symbol');
require('core-js/features/symbol');
// Support iterable spread (...Set, ...Map)
require('core-js/fn/array/from');
require('core-js/features/array/from');
28 changes: 3 additions & 25 deletions packages/react-app-polyfill/ie9.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,9 @@
*/
'use strict';

if (typeof Promise === 'undefined') {
// Rejection tracking prevents a common issue where React gets into an
// inconsistent state due to an error, but it gets swallowed by a Promise,
// and the user has no idea what causes React's erratic future behavior.
require('promise/lib/rejection-tracking').enable();
window.Promise = require('promise/lib/es6-extensions.js');
}

// Make sure we're in a Browser-like environment before importing polyfills
// This prevents `fetch()` from being imported in a Node test environment
if (typeof window !== 'undefined') {
// fetch() polyfill for making API calls.
require('whatwg-fetch');
}

// Object.assign() is commonly used with React.
// It will use the native implementation if it's present and isn't buggy.
Object.assign = require('object-assign');

// Support for...of (a commonly used syntax feature that requires Symbols)
require('core-js/es6/symbol');
// Support iterable spread (...Set, ...Map)
require('core-js/fn/array/from');
require('./ie11');

// React 16+ relies on Map, Set, and requestAnimationFrame
require('core-js/es6/map');
require('core-js/es6/set');
require('core-js/features/map');
require('core-js/features/set');
require('raf').polyfill(window);
6 changes: 4 additions & 2 deletions packages/react-app-polyfill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
"files": [
"ie9.js",
"ie11.js",
"jsdom.js"
"jsdom.js",
"stable.js"
],
"dependencies": {
"core-js": "2.6.5",
"core-js": "3.0.1",
"object-assign": "4.1.1",
"promise": "8.0.2",
"raf": "3.4.1",
"regenerator-runtime": "0.13.2",
"whatwg-fetch": "3.0.0"
}
}
13 changes: 13 additions & 0 deletions packages/react-app-polyfill/stable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* 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';

// Polyfill stable language features.
// It's recommended to use @babel/preset-env and browserslist
// to only include the polyfills necessary for the target browsers.
require('core-js/stable');
require('regenerator-runtime/runtime');

0 comments on commit 7114965

Please sign in to comment.