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
This commit is contained in:
Jean Lauliac 2017-01-06 06:02:39 -08:00 committed by Facebook Github Bot
parent fbfc52c832
commit 6eb417e69d
3 changed files with 59 additions and 74 deletions

View File

@ -21,9 +21,10 @@ exports.createServer = createServer;
exports.Logger = Logger; exports.Logger = Logger;
type Options = { type Options = {
nonPersistent: boolean,
projectRoots: Array<string>,
reporter?: Reporter, reporter?: Reporter,
watch?: boolean, watch?: boolean,
nonPersistent: boolean,
}; };
exports.buildBundle = function(options: Options, bundleOptions: {}) { exports.buildBundle = function(options: Options, bundleOptions: {}) {

View File

@ -129,7 +129,7 @@ type Options = {
allowBundleUpdates: boolean, allowBundleUpdates: boolean,
assetExts: Array<string>, assetExts: Array<string>,
assetServer: AssetServer, assetServer: AssetServer,
blacklistRE: RegExp, blacklistRE?: RegExp,
cacheVersion: string, cacheVersion: string,
extraNodeModules: {}, extraNodeModules: {},
getTransformOptions?: GetTransformOptions<*>, getTransformOptions?: GetTransformOptions<*>,
@ -139,7 +139,7 @@ type Options = {
projectRoots: Array<string>, projectRoots: Array<string>,
reporter: Reporter, reporter: Reporter,
resetCache: boolean, resetCache: boolean,
transformModulePath: string, transformModulePath?: string,
transformTimeoutInterval: ?number, transformTimeoutInterval: ?number,
watch: boolean, watch: boolean,
}; };

View File

@ -32,6 +32,7 @@ import type {IncomingMessage, ServerResponse} from 'http';
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
import type Bundle from '../Bundler/Bundle'; import type Bundle from '../Bundler/Bundle';
import type {Reporter} from '../lib/reporting'; import type {Reporter} from '../lib/reporting';
import type {GetTransformOptions} from '../Bundler';
const { const {
createActionStartEntry, createActionStartEntry,
@ -52,66 +53,23 @@ function debounceAndBatch(fn, delay) {
}; };
} }
const validateOpts = declareOpts({ type Options = {
projectRoots: { assetExts?: Array<string>,
type: 'array', blacklistRE?: RegExp,
required: true, cacheVersion?: string,
}, extraNodeModules?: {},
blacklistRE: { getTransformOptions?: GetTransformOptions<*>,
type: 'object', // typeof regex is object moduleFormat?: string,
}, platforms?: Array<string>,
moduleFormat: { polyfillModuleNames?: Array<string>,
type: 'string', projectRoots: Array<string>,
default: 'haste', reporter: Reporter,
}, resetCache?: boolean,
polyfillModuleNames: { silent?: boolean,
type: 'array', transformModulePath?: string,
default: [], transformTimeoutInterval?: number,
}, watch?: boolean,
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',
},
});
const bundleOpts = declareOpts({ const bundleOpts = declareOpts({
sourceMapUrl: { sourceMapUrl: {
@ -211,7 +169,20 @@ const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
class Server { class Server {
_opts: { _opts: {
assetExts: Array<string>,
blacklistRE: ?RegExp,
cacheVersion: string,
extraNodeModules: {},
getTransformOptions?: GetTransformOptions<*>,
moduleFormat: string,
platforms: Array<string>,
polyfillModuleNames: Array<string>,
projectRoots: Array<string>, projectRoots: Array<string>,
reporter: Reporter,
resetCache: boolean,
silent: boolean,
transformModulePath: ?string,
transformTimeoutInterval: ?number,
watch: boolean, watch: boolean,
}; };
_projectRoots: Array<string>; _projectRoots: Array<string>;
@ -227,29 +198,42 @@ class Server {
_hmrFileChangeListener: (type: string, filePath: string) => mixed; _hmrFileChangeListener: (type: string, filePath: string) => mixed;
_reporter: Reporter; _reporter: Reporter;
constructor(options: { constructor(options: Options) {
reporter: Reporter, this._opts = {
watch?: boolean, assetExts: options.assetExts || defaults.assetExts,
}) { blacklistRE: options.blacklistRE,
const opts = this._opts = validateOpts(options); 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 = const processFileChange =
({type, filePath, stat}) => this.onFileChange(type, filePath, stat); ({type, filePath, stat}) => this.onFileChange(type, filePath, stat);
this._reporter = options.reporter; this._reporter = options.reporter;
this._projectRoots = opts.projectRoots; this._projectRoots = this._opts.projectRoots;
this._bundles = Object.create(null); this._bundles = Object.create(null);
this._changeWatchers = []; this._changeWatchers = [];
this._fileChangeListeners = []; this._fileChangeListeners = [];
this._assetServer = new AssetServer({ this._assetServer = new AssetServer({
assetExts: opts.assetExts, assetExts: this._opts.assetExts,
projectRoots: opts.projectRoots, projectRoots: this._opts.projectRoots,
}); });
const bundlerOpts = Object.create(opts); const bundlerOpts = Object.create(this._opts);
bundlerOpts.assetServer = this._assetServer; bundlerOpts.assetServer = this._assetServer;
bundlerOpts.allowBundleUpdates = options.watch; bundlerOpts.allowBundleUpdates = this._opts.watch;
bundlerOpts.watch = options.watch; bundlerOpts.watch = this._opts.watch;
bundlerOpts.reporter = options.reporter; bundlerOpts.reporter = options.reporter;
this._bundler = new Bundler(bundlerOpts); this._bundler = new Bundler(bundlerOpts);