packager: Server: remove last declareOpts usage and replace by Flow types

Summary:
`declareOpts` prevents strong Flow typing, and promotes default values, that may be different from a function to another (my goal is to remove defaults from the whole codebase except perhaps the public API). This changeset replaces it by Flow types and fixes callsites to be explicit on values.

This is the last callsite of `declareOpts` so I'll remove it, in a separate diff.

Reviewed By: cpojer

Differential Revision: D4970650

fbshipit-source-id: e5ea2e06febde892d28c9dc59dc2920d4033bb01
This commit is contained in:
Jean Lauliac 2017-05-03 03:26:52 -07:00 committed by Facebook Github Bot
parent 89b3cc1f2f
commit e135bacb48
4 changed files with 52 additions and 63 deletions

View File

@ -57,10 +57,12 @@ function attachHMRServer({httpServer, path, packagerServer}: HMROptions) {
resolutionResponse: ResolutionResponse<Module, *>,
}> {
return packagerServer.getDependencies({
platform: platform,
dev: true,
hot: true,
entryFile: bundleEntry,
hot: true,
minify: false,
platform: platform,
recursive: true,
}).then(response => {
/* $FlowFixMe: getModuleId might be null */
const {getModuleId}: {getModuleId: () => number} = response;
@ -73,10 +75,12 @@ function attachHMRServer({httpServer, path, packagerServer}: HMROptions) {
return Promise.resolve({path: dep.path, deps: []});
}
return packagerServer.getShallowDependencies({
platform: platform,
dev: true,
entryFile: dep.path,
hot: true,
entryFile: dep.path
minify: false,
platform: platform,
recursive: true,
}).then(deps => {
return {
path: dep.path,
@ -174,10 +178,12 @@ function attachHMRServer({httpServer, path, packagerServer}: HMROptions) {
const promise = type === 'delete'
? Promise.resolve()
: packagerServer.getShallowDependencies({
entryFile: filename,
platform: client.platform,
dev: true,
minify: false,
entryFile: filename,
hot: true,
platform: client.platform,
recursive: true,
}).then(deps => {
if (!client) {
return [];
@ -193,10 +199,11 @@ function attachHMRServer({httpServer, path, packagerServer}: HMROptions) {
// specific response we can compute a non recursive one which
// is the least we need and improve performance.
return packagerServer.getDependencies({
platform: client.platform,
dev: true,
hot: true,
entryFile: filename,
hot: true,
minify: false,
platform: client.platform,
recursive: true,
}).then(response => {
return packagerServer.getModuleForPath(filename).then(module => {

View File

@ -79,9 +79,13 @@ exports.buildBundle = function(options: Options, bundleOptions: PublicBundleOpti
});
};
exports.getOrderedDependencyPaths = function(options: Options, bundleOptions: {}) {
exports.getOrderedDependencyPaths = function(options: Options, depOptions: {
+entryFile: string,
+dev: boolean,
+platform: string,
}) {
var server = createNonPersistentServer(options);
return server.getOrderedDependencyPaths(bundleOptions)
return server.getOrderedDependencyPaths(depOptions)
.then(function(paths) {
server.end();
return paths;

View File

@ -584,9 +584,9 @@ class Bundler {
}
getOrderedDependencyPaths({entryFile, dev, platform}: {
entryFile: string,
dev: boolean,
platform: string,
+entryFile: string,
+dev: boolean,
+platform: string,
}) {
return this.getDependencies({entryFile, dev, platform}).then(
({dependencies}) => {

View File

@ -16,7 +16,6 @@ const getPlatformExtension = require('../node-haste').getPlatformExtension;
const Bundler = require('../Bundler');
const MultipartResponse = require('./MultipartResponse');
const declareOpts = require('../lib/declareOpts');
const defaults = require('../../defaults');
const mime = require('mime-types');
const path = require('path');
@ -98,32 +97,14 @@ export type BundleOptions = {
unbundle: boolean,
};
const dependencyOpts = declareOpts({
platform: {
type: 'string',
required: true,
},
dev: {
type: 'boolean',
default: true,
},
entryFile: {
type: 'string',
required: true,
},
recursive: {
type: 'boolean',
default: true,
},
hot: {
type: 'boolean',
default: false,
},
minify: {
type: 'boolean',
default: undefined,
},
});
type DependencyOptions = {|
+dev: boolean,
+entryFile: string,
+hot: boolean,
+minify: boolean,
+platform: ?string,
+recursive: boolean,
|};
const bundleDeps = new WeakMap();
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
@ -291,17 +272,14 @@ class Server {
return this._bundler.hmrBundle(options, host, port);
}
getShallowDependencies(options: {
entryFile: string,
platform?: string,
}): Promise<Array<Module>> {
getShallowDependencies(options: DependencyOptions): Promise<Array<Module>> {
return Promise.resolve().then(() => {
if (!options.platform) {
options.platform = getPlatformExtension(options.entryFile);
}
const opts = dependencyOpts(options);
return this._bundler.getShallowDependencies(opts);
const platform = options.platform != null
? options.platform : getPlatformExtension(options.entryFile);
const {entryFile, dev, minify, hot} = options;
return this._bundler.getShallowDependencies(
{entryFile, platform, dev, minify, hot, generateSourceMaps: false},
);
});
}
@ -309,24 +287,24 @@ class Server {
return this._bundler.getModuleForPath(entryFile);
}
getDependencies(options: {
entryFile: string,
platform: ?string,
}): Promise<ResolutionResponse<Module, *>> {
getDependencies(options: DependencyOptions): Promise<ResolutionResponse<Module, *>> {
return Promise.resolve().then(() => {
if (!options.platform) {
options.platform = getPlatformExtension(options.entryFile);
}
const opts = dependencyOpts(options);
return this._bundler.getDependencies(opts);
const platform = options.platform != null
? options.platform : getPlatformExtension(options.entryFile);
const {entryFile, dev, minify, hot} = options;
return this._bundler.getDependencies(
{entryFile, platform, dev, minify, hot, generateSourceMaps: false},
);
});
}
getOrderedDependencyPaths(options: {}): Promise<mixed> {
getOrderedDependencyPaths(options: {
+entryFile: string,
+dev: boolean,
+platform: string,
}): Promise<mixed> {
return Promise.resolve().then(() => {
const opts = dependencyOpts(options);
return this._bundler.getOrderedDependencyPaths(opts);
return this._bundler.getOrderedDependencyPaths(options);
});
}