Saner entry point

Summary: makes flow typing for the entry point more sound and fixes two issues

Reviewed By: BYK

Differential Revision: D5507650

fbshipit-source-id: 6b03f7de792ffcece4d0d61950e136a61ea7db2e
This commit is contained in:
David Aurelio 2017-07-27 17:58:55 -07:00 committed by Facebook Github Bot
parent 43457e6674
commit 2dda50893f
2 changed files with 52 additions and 44 deletions

View File

@ -66,7 +66,7 @@ function debounceAndBatch(fn, delay) {
};
}
type Options = {
export type Options = {|
assetExts?: Array<string>,
+assetRegistryPath: string,
blacklistRE?: RegExp,
@ -86,7 +86,7 @@ type Options = {
postProcessBundleSourcemap: PostProcessBundleSourcemap,
projectRoots: $ReadOnlyArray<string>,
providesModuleNodeModules?: Array<string>,
reporter: Reporter,
reporter?: Reporter,
resetCache?: boolean,
silent?: boolean,
+sourceExts: ?Array<string>,
@ -95,7 +95,7 @@ type Options = {
transformTimeoutInterval?: number,
watch?: boolean,
workerPath: ?string,
};
|};
export type BundleOptions = {
+assetPlugins: Array<string>,
@ -174,6 +174,8 @@ class Server {
_nextBundleBuildID: number;
constructor(options: Options) {
const reporter =
options.reporter || require('../lib/reporting').nullReporter;
const maxWorkers = getMaxWorkers(options.maxWorkers);
this._opts = {
assetExts: options.assetExts || defaults.assetExts,
@ -199,7 +201,7 @@ class Server {
postProcessBundleSourcemap: options.postProcessBundleSourcemap,
projectRoots: options.projectRoots,
providesModuleNodeModules: options.providesModuleNodeModules,
reporter: options.reporter,
reporter,
resetCache: options.resetCache || false,
silent: options.silent || false,
sourceExts: options.sourceExts || defaults.sourceExts,
@ -213,7 +215,7 @@ class Server {
const processFileChange = ({type, filePath}) =>
this.onFileChange(type, filePath);
this._reporter = options.reporter;
this._reporter = reporter;
this._projectRoots = this._opts.projectRoots;
this._bundles = Object.create(null);
this._changeWatchers = [];
@ -230,7 +232,7 @@ class Server {
bundlerOpts.allowBundleUpdates = this._opts.watch;
bundlerOpts.globalTransformCache = options.globalTransformCache;
bundlerOpts.watch = this._opts.watch;
bundlerOpts.reporter = options.reporter;
bundlerOpts.reporter = reporter;
this._bundler = new Bundler(bundlerOpts);
// changes to the haste map can affect resolution of files in the bundle

View File

@ -21,7 +21,7 @@ const invariant = require('fbjs/lib/invariant');
const {fromRawMappings, compactMapping} = require('./Bundler/source-map');
import type {PostProcessModules, PostMinifyProcess, PostProcessBundleSourcemap} from './Bundler';
import type Server from './Server';
import type Server, {Options as ServerOptions} from './Server';
import type {GlobalTransformCache} from './lib/GlobalTransformCache';
import type {TransformCache} from './lib/TransformCaching';
import type {Reporter} from './lib/reporting';
@ -33,27 +33,13 @@ exports.createServer = createServer;
exports.Logger = Logger;
type Options = {|
+assetRegistryPath: string,
+sourceExts: ?Array<string>,
+transformCache: TransformCache,
+transformModulePath: string,
enableBabelRCLookup?: boolean,
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
globalTransformCache: ?GlobalTransformCache,
hasteImpl?: HasteImpl,
+maxWorkers?: number,
nonPersistent?: boolean,
postMinifyProcess?: PostMinifyProcess,
postProcessBundleSourcemap?: PostProcessBundleSourcemap,
postProcessModules?: PostProcessModules,
projectRoots: $ReadOnlyArray<string>,
reporter?: Reporter,
watch?: boolean,
workerPath: ?string,
...ServerOptions,
// optional types to force flow errors in `toServerOptions`
nonPersistent?: ?boolean,
transformCache?: ?TransformCache,
verbose?: ?boolean,
|};
type StrictOptions = {...Options, reporter: Reporter};
type PublicBundleOptions = {
+dev?: boolean,
+entryFile: string,
@ -127,7 +113,7 @@ function enableDebug() {
debug.enable(debugPattern);
}
function createServer(options: StrictOptions): Server {
function createServer(options: Options): Server {
// the debug module is configured globally, we need to enable debugging
// *before* requiring any packages that use `debug` for logging
if (options.verbose) {
@ -135,25 +121,45 @@ function createServer(options: StrictOptions): Server {
}
// Some callsites may not be Flowified yet.
invariant(options.reporter != null, 'createServer() requires reporter');
if (options.transformCache == null) {
options.transformCache = TransformCaching.useTempDir();
}
const serverOptions = Object.assign({}, options);
delete serverOptions.verbose;
invariant(options.assetRegistryPath != null, 'createServer() requires assetRegistryPath');
const ServerClass = require('./Server');
return new ServerClass(serverOptions);
return new ServerClass(toServerOptions(options));
}
function createNonPersistentServer(options: Options): Server {
const serverOptions = {
// It's unsound to set-up the reporter here,
// but this allows backward compatibility.
reporter: options.reporter == null
? require('./lib/reporting').nullReporter
: options.reporter,
...options,
watch: !options.nonPersistent,
};
return createServer(serverOptions);
return createServer(options);
}
function toServerOptions(options: Options): ServerOptions {
return {
assetExts: options.assetExts,
assetRegistryPath: options.assetRegistryPath,
blacklistRE: options.blacklistRE,
cacheVersion: options.cacheVersion,
enableBabelRCLookup: options.enableBabelRCLookup,
extraNodeModules: options.extraNodeModules,
getPolyfills: options.getPolyfills,
getTransformOptions: options.getTransformOptions,
globalTransformCache: options.globalTransformCache,
hasteImpl: options.hasteImpl,
maxWorkers: options.maxWorkers,
moduleFormat: options.moduleFormat,
platforms: options.platforms,
polyfillModuleNames: options.polyfillModuleNames,
postProcessModules: options.postProcessModules,
postMinifyProcess: options.postMinifyProcess,
postProcessBundleSourcemap: options.postProcessBundleSourcemap,
projectRoots: options.projectRoots,
providesModuleNodeModules: options.providesModuleNodeModules,
reporter: options.reporter,
resetCache: options.resetCache,
silent: options.silent,
sourceExts: options.sourceExts,
transformCache: options.transformCache || TransformCaching.useTempDir(),
transformModulePath: options.transformModulePath,
transformTimeoutInterval: options.transformTimeoutInterval,
watch: typeof options.watch === 'boolean' ? options.watch : !!options.nonPersistent,
workerPath: options.workerPath,
};
}