From 71149650ec219853b27784edaea6a586e23d7eaa Mon Sep 17 00:00:00 2001 From: Ian Schmitz Date: Sat, 6 Apr 2019 16:24:18 -0700 Subject: [PATCH] Update to core-js@3 --- packages/babel-preset-react-app/create.js | 2 +- packages/react-app-polyfill/README.md | 59 ++++++++++++++++++----- packages/react-app-polyfill/ie11.js | 4 +- packages/react-app-polyfill/ie9.js | 28 ++--------- packages/react-app-polyfill/package.json | 6 ++- packages/react-app-polyfill/stable.js | 13 +++++ 6 files changed, 69 insertions(+), 43 deletions(-) create mode 100644 packages/react-app-polyfill/stable.js diff --git a/packages/babel-preset-react-app/create.js b/packages/babel-preset-react-app/create.js index b645a22ede2..931892e6a9a 100644 --- a/packages/babel-preset-react-app/create.js +++ b/packages/babel-preset-react-app/create.js @@ -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 diff --git a/packages/react-app-polyfill/README.md b/packages/react-app-polyfill/README.md index 74972b2d338..cda82510469 100644 --- a/packages/react-app-polyfill/README.md +++ b/packages/react-app-polyfill/README.md @@ -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: @@ -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 @@ -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'; + +// ... +``` diff --git a/packages/react-app-polyfill/ie11.js b/packages/react-app-polyfill/ie11.js index e7e3d6b71a0..4a5bcd6bf38 100644 --- a/packages/react-app-polyfill/ie11.js +++ b/packages/react-app-polyfill/ie11.js @@ -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'); diff --git a/packages/react-app-polyfill/ie9.js b/packages/react-app-polyfill/ie9.js index 9f355f28f21..42e31fdb1fb 100644 --- a/packages/react-app-polyfill/ie9.js +++ b/packages/react-app-polyfill/ie9.js @@ -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); diff --git a/packages/react-app-polyfill/package.json b/packages/react-app-polyfill/package.json index b9b55d3a1e0..ad5e231575f 100644 --- a/packages/react-app-polyfill/package.json +++ b/packages/react-app-polyfill/package.json @@ -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" } } diff --git a/packages/react-app-polyfill/stable.js b/packages/react-app-polyfill/stable.js new file mode 100644 index 00000000000..6108630c7c0 --- /dev/null +++ b/packages/react-app-polyfill/stable.js @@ -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');