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
This commit is contained in:
Jean Lauliac 2017-03-10 08:28:42 -08:00 committed by Facebook Github Bot
parent e1a2633f69
commit da94f5e5af
3 changed files with 12 additions and 21 deletions

View File

@ -24,6 +24,7 @@ const ModuleTransport = require('../lib/ModuleTransport');
const imageSize = require('image-size'); const imageSize = require('image-size');
const path = require('path'); const path = require('path');
const denodeify = require('denodeify'); const denodeify = require('denodeify');
const defaults = require('../../defaults');
const { const {
sep: pathSeparator, sep: pathSeparator,
@ -179,10 +180,11 @@ class Bundler {
hasteImpl: opts.hasteImpl, hasteImpl: opts.hasteImpl,
minifyCode: this._transformer.minify, minifyCode: this._transformer.minify,
moduleFormat: opts.moduleFormat, moduleFormat: opts.moduleFormat,
platforms: opts.platforms, platforms: new Set(opts.platforms),
polyfillModuleNames: opts.polyfillModuleNames, polyfillModuleNames: opts.polyfillModuleNames,
projectRoots: opts.projectRoots, projectRoots: opts.projectRoots,
providesModuleNodeModules: opts.providesModuleNodeModules, providesModuleNodeModules:
opts.providesModuleNodeModules || defaults.providesModuleNodeModules,
reporter: opts.reporter, reporter: opts.reporter,
resetCache: opts.resetCache, resetCache: opts.resetCache,
transformCode: transformCode:

View File

@ -33,19 +33,19 @@ type Options = {
assetExts: Array<string>, assetExts: Array<string>,
blacklistRE?: RegExp, blacklistRE?: RegExp,
cache: Cache, cache: Cache,
extraNodeModules?: {}, extraNodeModules: ?{},
getTransformCacheKey: GetTransformCacheKey, getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
hasteImpl?: HasteImpl, hasteImpl?: HasteImpl,
minifyCode: MinifyCode, minifyCode: MinifyCode,
platforms: Array<string>, platforms: Set<string>,
polyfillModuleNames?: Array<string>, polyfillModuleNames?: Array<string>,
projectRoots: Array<string>, projectRoots: Array<string>,
providesModuleNodeModules?: Array<string>, providesModuleNodeModules: Array<string>,
reporter: Reporter, reporter: Reporter,
resetCache: boolean, resetCache: boolean,
transformCode: TransformCode, transformCode: TransformCode,
watch?: boolean, watch: boolean,
}; };
class Resolver { class Resolver {
@ -61,15 +61,10 @@ class Resolver {
} }
static async load(opts: Options): Promise<Resolver> { static async load(opts: Options): Promise<Resolver> {
const depGraph = await DependencyGraph.load({ const depGraphOpts = Object.assign(Object.create(opts), {
assetDependencies: ['react-native/Libraries/Image/AssetRegistry'], assetDependencies: ['react-native/Libraries/Image/AssetRegistry'],
assetExts: opts.assetExts,
cache: opts.cache,
extraNodeModules: opts.extraNodeModules,
extensions: ['js', 'json'], extensions: ['js', 'json'],
forceNodeFilesystemAPI: false, forceNodeFilesystemAPI: false,
getTransformCacheKey: opts.getTransformCacheKey,
globalTransformCache: opts.globalTransformCache,
ignoreFilePath(filepath) { ignoreFilePath(filepath) {
return filepath.indexOf('__tests__') !== -1 || return filepath.indexOf('__tests__') !== -1 ||
(opts.blacklistRE != null && opts.blacklistRE.test(filepath)); (opts.blacklistRE != null && opts.blacklistRE.test(filepath));
@ -80,17 +75,11 @@ class Resolver {
hasteImpl: opts.hasteImpl, hasteImpl: opts.hasteImpl,
resetCache: opts.resetCache, resetCache: opts.resetCache,
}, },
platforms: new Set(opts.platforms),
preferNativePlatform: true, preferNativePlatform: true,
providesModuleNodeModules:
opts.providesModuleNodeModules || defaults.providesModuleNodeModules,
reporter: opts.reporter,
resetCache: opts.resetCache,
roots: opts.projectRoots, roots: opts.projectRoots,
transformCode: opts.transformCode,
useWatchman: true, useWatchman: true,
watch: opts.watch || false,
}); });
const depGraph = await DependencyGraph.load(depGraphOpts);
return new Resolver(opts, depGraph); return new Resolver(opts, depGraph);
} }

View File

@ -53,7 +53,7 @@ type Options = {
assetExts: Array<string>, assetExts: Array<string>,
cache: Cache, cache: Cache,
extensions: Array<string>, extensions: Array<string>,
extraNodeModules: ?Object, extraNodeModules: ?{},
forceNodeFilesystemAPI: boolean, forceNodeFilesystemAPI: boolean,
getTransformCacheKey: GetTransformCacheKey, getTransformCacheKey: GetTransformCacheKey,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
@ -87,7 +87,7 @@ class DependencyGraph extends EventEmitter {
initialModuleMap: ModuleMap, initialModuleMap: ModuleMap,
}) { }) {
super(); super();
this._opts = {...config.opts}; this._opts = config.opts;
this._haste = config.haste; this._haste = config.haste;
this._hasteFS = config.initialHasteFS; this._hasteFS = config.initialHasteFS;
this._moduleMap = config.initialModuleMap; this._moduleMap = config.initialModuleMap;