From 5ec9e919f51aa3a348b8d46a59d41d5f5bfd54e8 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Wed, 7 Jun 2017 16:33:09 -0700 Subject: [PATCH] RFC: Do not include package-local polyfills. This removes babel's core-js polyfills from the distributed NPM builds. This aids in further reducing app bundle size when targeting modern browsers, and allows flexibility in how these polyfills are provided if necessary, which can help fix React Native specific issues, as illustrated in #1799 and #1704 and #1818. The drawback is that polyfills for these things are now something that client developers will need to think about directly. Shipping Relay to older browsers before this diff should work fine, but after this diff will require global polyfills. As such, this diff may constitute at least a minor-breaking change. --- docs/modern/Introduction.md | 11 +++++++++++ gulpfile.js | 14 +------------- packages/relay-runtime/RelayRuntime.js | 15 +++++++++++++++ scripts/getBabelOptions.js | 1 - 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/docs/modern/Introduction.md b/docs/modern/Introduction.md index b7eca9b710fb6..f918de0e04ab7 100644 --- a/docs/modern/Introduction.md +++ b/docs/modern/Introduction.md @@ -48,6 +48,17 @@ Run the Relay Compiler after making changes to any GraphQL in your Relay applica Then after making edits to your application files, just run `yarn run relay` to generate new files, or `yarn run relay -- --watch` to run the compiler as a long-lived process which automatically generates new files whenever you save. +## JavaScript Environment + +The Relay Modern packages distributed on NPM use the widely-supported ES5 +version of JavaScript to support as many browser environments as possible. + +However, Relay Modern expects modern JavaScript global types (`Map`, `Set`, +`Promise`) to be defined. If you support older browsers and devices which may +not yet provide these natively, consider including a global polyfill in your +bundled application. + + ## Migrating to Relay Modern Migrating a Relay Classic app to Relay Modern doesn't require rewriting from diff --git a/gulpfile.js b/gulpfile.js index f36d1006d0c4f..0a44e79f542d9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -15,18 +15,6 @@ const babelOptions = require('./scripts/getBabelOptions')({ 'babel-core': 'babel-core', 'babel-generator': 'babel-generator', 'babel-polyfill': 'babel-polyfill', - 'babel-runtime/core-js/array/from': 'babel-runtime/core-js/array/from', - 'babel-runtime/core-js/json/stringify': 'babel-runtime/core-js/json/stringify', - 'babel-runtime/core-js/map': 'babel-runtime/core-js/map', - 'babel-runtime/core-js/object/assign': 'babel-runtime/core-js/object/assign', - 'babel-runtime/core-js/object/freeze': 'babel-runtime/core-js/object/freeze', - 'babel-runtime/core-js/object/get-own-property-names': 'babel-runtime/core-js/object/get-own-property-names', - 'babel-runtime/core-js/object/is-frozen': 'babel-runtime/core-js/object/is-frozen', - 'babel-runtime/core-js/object/keys': 'babel-runtime/core-js/object/keys', - 'babel-runtime/core-js/object/values': 'babel-runtime/core-js/object/values', - 'babel-runtime/core-js/promise': 'fbjs/lib/Promise', - 'babel-runtime/core-js/set': 'babel-runtime/core-js/set', - 'babel-runtime/core-js/weak-map': 'babel-runtime/core-js/weak-map', 'babel-runtime/helpers/asyncToGenerator': 'babel-runtime/helpers/asyncToGenerator', 'babel-runtime/helpers/classCallCheck': 'babel-runtime/helpers/classCallCheck', 'babel-runtime/helpers/defineProperty': 'babel-runtime/helpers/defineProperty', @@ -56,7 +44,7 @@ const babelOptions = require('./scripts/getBabelOptions')({ }, plugins: [ 'transform-flow-strip-types', - 'transform-runtime', + ['transform-runtime', {"polyfill": false}], ], postPlugins: [ 'transform-async-to-generator', diff --git a/packages/relay-runtime/RelayRuntime.js b/packages/relay-runtime/RelayRuntime.js index 9eb8c786bbc69..f8be80fb96917 100644 --- a/packages/relay-runtime/RelayRuntime.js +++ b/packages/relay-runtime/RelayRuntime.js @@ -35,6 +35,21 @@ export type { ConcreteFragment, } from 'RelayConcreteNode'; +// As early as possible, check for the existence of the JavaScript globals which +// Relay Runtime rely upon, and produce a clear message if they do not exist. +if (__DEV__) { + if ( + typeof Map !== 'function' || + typeof Set !== 'function' || + typeof Promise !== 'function' + ) { + throw new Error( + 'relay-runtime requires Map, Set, and Promise to be defined. ' + + 'Use a polyfill to provide these for older browsers.' + ); + } +} + /** * The public interface to Relay Runtime. */ diff --git a/scripts/getBabelOptions.js b/scripts/getBabelOptions.js index f0de6266c4a50..fd24b4eaef42d 100644 --- a/scripts/getBabelOptions.js +++ b/scripts/getBabelOptions.js @@ -19,7 +19,6 @@ module.exports = function(options) { }, options); const fbjsPreset = require('babel-preset-fbjs/configure')({ - autoImport: true, inlineRequires: true, rewriteModules: { map: assign({},