From 3e1542b48599d705f4038f7448f61f0907e549e0 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Fri, 10 Mar 2017 08:28:42 -0800 Subject: [PATCH] packager: Resolver: remove opt defaults, simplify load() Summary: Not having default everywhere (keeping them at the top level instead) makes for a code that is easier to understand, and more robust as different pieces of code cannot default to different values. This changeset also unifies the option types (ex. `platform`). Reviewed By: cpojer Differential Revision: D4688882 fbshipit-source-id: b5f407601386336f937a0ac1f68c666acc89dfd8 --- packager/src/Bundler/index.js | 6 ++++-- packager/src/Resolver/index.js | 23 ++++++----------------- packager/src/node-haste/index.js | 4 ++-- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/packager/src/Bundler/index.js b/packager/src/Bundler/index.js index c7428fdf2..b4dcf6471 100644 --- a/packager/src/Bundler/index.js +++ b/packager/src/Bundler/index.js @@ -24,6 +24,7 @@ const ModuleTransport = require('../lib/ModuleTransport'); const imageSize = require('image-size'); const path = require('path'); const denodeify = require('denodeify'); +const defaults = require('../../defaults'); const { sep: pathSeparator, @@ -179,10 +180,11 @@ class Bundler { hasteImpl: opts.hasteImpl, minifyCode: this._transformer.minify, moduleFormat: opts.moduleFormat, - platforms: opts.platforms, + platforms: new Set(opts.platforms), polyfillModuleNames: opts.polyfillModuleNames, projectRoots: opts.projectRoots, - providesModuleNodeModules: opts.providesModuleNodeModules, + providesModuleNodeModules: + opts.providesModuleNodeModules || defaults.providesModuleNodeModules, reporter: opts.reporter, resetCache: opts.resetCache, transformCode: diff --git a/packager/src/Resolver/index.js b/packager/src/Resolver/index.js index 30587c95b..31d53c9d3 100644 --- a/packager/src/Resolver/index.js +++ b/packager/src/Resolver/index.js @@ -33,19 +33,19 @@ type Options = { assetExts: Array, blacklistRE?: RegExp, cache: Cache, - extraNodeModules?: {}, + extraNodeModules: ?{}, getTransformCacheKey: GetTransformCacheKey, globalTransformCache: ?GlobalTransformCache, hasteImpl?: HasteImpl, minifyCode: MinifyCode, - platforms: Array, + platforms: Set, polyfillModuleNames?: Array, projectRoots: Array, - providesModuleNodeModules?: Array, + providesModuleNodeModules: Array, reporter: Reporter, resetCache: boolean, transformCode: TransformCode, - watch?: boolean, + watch: boolean, }; class Resolver { @@ -61,15 +61,10 @@ class Resolver { } static async load(opts: Options): Promise { - const depGraph = await DependencyGraph.load({ + const depGraphOpts = Object.assign(Object.create(opts), { assetDependencies: ['react-native/Libraries/Image/AssetRegistry'], - assetExts: opts.assetExts, - cache: opts.cache, - extraNodeModules: opts.extraNodeModules, extensions: ['js', 'json'], forceNodeFilesystemAPI: false, - getTransformCacheKey: opts.getTransformCacheKey, - globalTransformCache: opts.globalTransformCache, ignoreFilePath(filepath) { return filepath.indexOf('__tests__') !== -1 || (opts.blacklistRE != null && opts.blacklistRE.test(filepath)); @@ -80,17 +75,11 @@ class Resolver { hasteImpl: opts.hasteImpl, resetCache: opts.resetCache, }, - platforms: new Set(opts.platforms), preferNativePlatform: true, - providesModuleNodeModules: - opts.providesModuleNodeModules || defaults.providesModuleNodeModules, - reporter: opts.reporter, - resetCache: opts.resetCache, roots: opts.projectRoots, - transformCode: opts.transformCode, useWatchman: true, - watch: opts.watch || false, }); + const depGraph = await DependencyGraph.load(depGraphOpts); return new Resolver(opts, depGraph); } diff --git a/packager/src/node-haste/index.js b/packager/src/node-haste/index.js index 55bf309ac..25af7db61 100644 --- a/packager/src/node-haste/index.js +++ b/packager/src/node-haste/index.js @@ -53,7 +53,7 @@ type Options = { assetExts: Array, cache: Cache, extensions: Array, - extraNodeModules: ?Object, + extraNodeModules: ?{}, forceNodeFilesystemAPI: boolean, getTransformCacheKey: GetTransformCacheKey, globalTransformCache: ?GlobalTransformCache, @@ -87,7 +87,7 @@ class DependencyGraph extends EventEmitter { initialModuleMap: ModuleMap, }) { super(); - this._opts = {...config.opts}; + this._opts = config.opts; this._haste = config.haste; this._hasteFS = config.initialHasteFS; this._moduleMap = config.initialModuleMap;