From 6dd9d1683392d568184f955192d63ecc377088ac Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Esun Date: Thu, 13 Jul 2017 03:30:06 -0700 Subject: [PATCH] Move Array to $ReadOnlyArray. Separate polyfill list into a file. Summary: Move the returned type of `getPolyfills` from a standard `Array` to a read-only one, so that we make sure the array is not modified once created. Also, refactor the list of polyfills included by default to a generic, central file, then require it both from the CLI utils as well as the development server. Reviewed By: jeanlauliac Differential Revision: D5406553 fbshipit-source-id: ab980288bb1c625338de469da77dd6fc70bcedbc --- local-cli/server/runServer.js | 2 +- local-cli/util/Config.js | 15 +++------------ package.json | 1 + rn-cli.config.js | 3 +++ rn-get-polyfills.js | 23 +++++++++++++++++++++++ 5 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 rn-get-polyfills.js diff --git a/local-cli/server/runServer.js b/local-cli/server/runServer.js index 4d9bac42b14139..60fa76b9b7e58d 100644 --- a/local-cli/server/runServer.js +++ b/local-cli/server/runServer.js @@ -152,12 +152,12 @@ function getPackagerServer(args, config) { blacklistRE: config.getBlacklistRE(), cacheVersion: '3', extraNodeModules: config.extraNodeModules, + getPolyfills: config.getPolyfills, getTransformOptions: config.getTransformOptions, hasteImpl: config.hasteImpl, maxWorkers: args.maxWorkers, platforms: defaultPlatforms.concat(args.platforms), polyfillModuleNames: config.getPolyfillModuleNames(), - getPolyfills: config.getPolyfills, postMinifyProcess: config.postMinifyProcess, postProcessModules: config.postProcessModules, projectRoots: args.projectRoots, diff --git a/local-cli/util/Config.js b/local-cli/util/Config.js index 71d58e8cc1a22b..f9b16416a367b8 100644 --- a/local-cli/util/Config.js +++ b/local-cli/util/Config.js @@ -13,6 +13,7 @@ const blacklist = require('metro-bundler/src/blacklist'); const findSymlinksPaths = require('./findSymlinksPaths'); const fs = require('fs'); +const getPolyfills = require('../../rn-get-polyfills'); const invariant = require('fbjs/lib/invariant'); const path = require('path'); @@ -94,7 +95,7 @@ export type ConfigT = { * An optional list of polyfills to include in the bundle. The list defaults * to a set of common polyfills for Number, String, Array, Object... */ - getPolyfills: ({platform: string}) => Array, + getPolyfills: ({platform: ?string}) => $ReadOnlyArray, /** * An optional function that can modify the code and source map of bundle @@ -177,17 +178,7 @@ const Config = { getSourceExts: () => [], getTransformModulePath: () => require.resolve('metro-bundler/src/transformer.js'), getTransformOptions: async () => ({}), - getPolyfills: ({platform}) => [ - require.resolve('../../Libraries/polyfills/Object.es6.js'), - require.resolve('../../Libraries/polyfills/console.js'), - require.resolve('../../Libraries/polyfills/error-guard.js'), - require.resolve('../../Libraries/polyfills/Number.es6.js'), - require.resolve('../../Libraries/polyfills/String.prototype.es6.js'), - require.resolve('../../Libraries/polyfills/Array.prototype.es6.js'), - require.resolve('../../Libraries/polyfills/Array.es6.js'), - require.resolve('../../Libraries/polyfills/Object.es7.js'), - require.resolve('../../Libraries/polyfills/babelHelpers.js'), - ], + getPolyfills, postMinifyProcess: x => x, postProcessModules: modules => modules, postProcessModulesForBuck: modules => modules, diff --git a/package.json b/package.json index 251553ab2738b6..c206eedc11258e 100644 --- a/package.json +++ b/package.json @@ -99,6 +99,7 @@ "jest-preset.json", "jest", "lib", + "rn-get-polyfills.js", "setupBabel.js", "Libraries", "LICENSE", diff --git a/rn-cli.config.js b/rn-cli.config.js index a03254b8bfd165..59ac25a1499327 100644 --- a/rn-cli.config.js +++ b/rn-cli.config.js @@ -8,6 +8,8 @@ */ 'use strict'; +const getPolyfills = require('./rn-get-polyfills'); + /** * This cli config is needed for development purposes, e.g. for running * integration tests during local development or on CI services. @@ -16,4 +18,5 @@ module.exports = { extraNodeModules: { 'react-native': __dirname, }, + getPolyfills, }; diff --git a/rn-get-polyfills.js b/rn-get-polyfills.js new file mode 100644 index 00000000000000..a228d6f1f91875 --- /dev/null +++ b/rn-get-polyfills.js @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +'use strict'; + +module.exports = () => [ + require.resolve('./Libraries/polyfills/Object.es6.js'), + require.resolve('./Libraries/polyfills/console.js'), + require.resolve('./Libraries/polyfills/error-guard.js'), + require.resolve('./Libraries/polyfills/Number.es6.js'), + require.resolve('./Libraries/polyfills/String.prototype.es6.js'), + require.resolve('./Libraries/polyfills/Array.prototype.es6.js'), + require.resolve('./Libraries/polyfills/Array.es6.js'), + require.resolve('./Libraries/polyfills/Object.es7.js'), + require.resolve('./Libraries/polyfills/babelHelpers.js'), +];