From 6eb417e69df7e02ad2820e1785cb9243d95b1db5 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Fri, 6 Jan 2017 06:02:39 -0800 Subject: [PATCH] packager: remove validateOpts for Server class Summary: Remove the validateOpts() that prevents full Flow typing. In turn, having full Flow typing will allow us to remove 'defaults' across the codebase, that are source of inconsistencies. Reviewed By: davidaurelio, cpojer Differential Revision: D4377823 fbshipit-source-id: 8bb0a6b5069de64a9bb9d63892cd08854da91777 --- react-packager/react-packager.js | 3 +- react-packager/src/Bundler/index.js | 4 +- react-packager/src/Server/index.js | 126 ++++++++++++---------------- 3 files changed, 59 insertions(+), 74 deletions(-) diff --git a/react-packager/react-packager.js b/react-packager/react-packager.js index 680c69fb..339adfce 100644 --- a/react-packager/react-packager.js +++ b/react-packager/react-packager.js @@ -21,9 +21,10 @@ exports.createServer = createServer; exports.Logger = Logger; type Options = { + nonPersistent: boolean, + projectRoots: Array, reporter?: Reporter, watch?: boolean, - nonPersistent: boolean, }; exports.buildBundle = function(options: Options, bundleOptions: {}) { diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index 8b1aa733..59fcfcbf 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -129,7 +129,7 @@ type Options = { allowBundleUpdates: boolean, assetExts: Array, assetServer: AssetServer, - blacklistRE: RegExp, + blacklistRE?: RegExp, cacheVersion: string, extraNodeModules: {}, getTransformOptions?: GetTransformOptions<*>, @@ -139,7 +139,7 @@ type Options = { projectRoots: Array, reporter: Reporter, resetCache: boolean, - transformModulePath: string, + transformModulePath?: string, transformTimeoutInterval: ?number, watch: boolean, }; diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index c30d9b19..478fe825 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -32,6 +32,7 @@ import type {IncomingMessage, ServerResponse} from 'http'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type Bundle from '../Bundler/Bundle'; import type {Reporter} from '../lib/reporting'; +import type {GetTransformOptions} from '../Bundler'; const { createActionStartEntry, @@ -52,66 +53,23 @@ function debounceAndBatch(fn, delay) { }; } -const validateOpts = declareOpts({ - projectRoots: { - type: 'array', - required: true, - }, - blacklistRE: { - type: 'object', // typeof regex is object - }, - moduleFormat: { - type: 'string', - default: 'haste', - }, - polyfillModuleNames: { - type: 'array', - default: [], - }, - cacheVersion: { - type: 'string', - default: '1.0', - }, - resetCache: { - type: 'boolean', - default: false, - }, - transformModulePath: { - type: 'string', - required: false, - }, - extraNodeModules: { - type: 'object', - required: false, - }, - watch: { - type: 'boolean', - default: false, - }, - assetExts: { - type: 'array', - default: defaults.assetExts, - }, - platforms: { - type: 'array', - default: defaults.platforms, - }, - transformTimeoutInterval: { - type: 'number', - required: false, - }, - getTransformOptions: { - type: 'function', - required: false, - }, - silent: { - type: 'boolean', - default: false, - }, - reporter: { - type: 'object', - }, -}); +type Options = { + assetExts?: Array, + blacklistRE?: RegExp, + cacheVersion?: string, + extraNodeModules?: {}, + getTransformOptions?: GetTransformOptions<*>, + moduleFormat?: string, + platforms?: Array, + polyfillModuleNames?: Array, + projectRoots: Array, + reporter: Reporter, + resetCache?: boolean, + silent?: boolean, + transformModulePath?: string, + transformTimeoutInterval?: number, + watch?: boolean, +}; const bundleOpts = declareOpts({ sourceMapUrl: { @@ -211,7 +169,20 @@ const NODE_MODULES = `${path.sep}node_modules${path.sep}`; class Server { _opts: { + assetExts: Array, + blacklistRE: ?RegExp, + cacheVersion: string, + extraNodeModules: {}, + getTransformOptions?: GetTransformOptions<*>, + moduleFormat: string, + platforms: Array, + polyfillModuleNames: Array, projectRoots: Array, + reporter: Reporter, + resetCache: boolean, + silent: boolean, + transformModulePath: ?string, + transformTimeoutInterval: ?number, watch: boolean, }; _projectRoots: Array; @@ -227,29 +198,42 @@ class Server { _hmrFileChangeListener: (type: string, filePath: string) => mixed; _reporter: Reporter; - constructor(options: { - reporter: Reporter, - watch?: boolean, - }) { - const opts = this._opts = validateOpts(options); + constructor(options: Options) { + this._opts = { + assetExts: options.assetExts || defaults.assetExts, + blacklistRE: options.blacklistRE, + cacheVersion: options.cacheVersion || '1.0', + extraNodeModules: options.extraNodeModules || {}, + getTransformOptions: options.getTransformOptions, + moduleFormat: options.moduleFormat != null ? options.moduleFormat : 'haste', + platforms: options.platforms || defaults.platforms, + polyfillModuleNames: options.polyfillModuleNames || [], + projectRoots: options.projectRoots, + reporter: options.reporter, + resetCache: options.resetCache || false, + silent: options.silent || false, + transformModulePath: options.transformModulePath, + transformTimeoutInterval: options.transformTimeoutInterval, + watch: options.watch || false, + }; const processFileChange = ({type, filePath, stat}) => this.onFileChange(type, filePath, stat); this._reporter = options.reporter; - this._projectRoots = opts.projectRoots; + this._projectRoots = this._opts.projectRoots; this._bundles = Object.create(null); this._changeWatchers = []; this._fileChangeListeners = []; this._assetServer = new AssetServer({ - assetExts: opts.assetExts, - projectRoots: opts.projectRoots, + assetExts: this._opts.assetExts, + projectRoots: this._opts.projectRoots, }); - const bundlerOpts = Object.create(opts); + const bundlerOpts = Object.create(this._opts); bundlerOpts.assetServer = this._assetServer; - bundlerOpts.allowBundleUpdates = options.watch; - bundlerOpts.watch = options.watch; + bundlerOpts.allowBundleUpdates = this._opts.watch; + bundlerOpts.watch = this._opts.watch; bundlerOpts.reporter = options.reporter; this._bundler = new Bundler(bundlerOpts);