packager ResolutionResponse: @flow

Reviewed By: davidaurelio

Differential Revision: D4146739

fbshipit-source-id: 0b8e65dfaa2f28c4498fb17b8d980da9271fe8d6
This commit is contained in:
Jean Lauliac 2016-11-08 04:46:15 -08:00 committed by Facebook Github Bot
parent d25047ebf1
commit 3ae8e44f34
4 changed files with 39 additions and 9 deletions

View File

@ -307,6 +307,8 @@ class Bundler {
onProgress,
}) {
const onResolutionResponse = (response: ResolutionResponse) => {
/* $FlowFixMe: looks like ResolutionResponse is monkey-patched
* with `getModuleId`. */
bundle.setMainModuleId(response.getModuleId(getMainModule(response)));
if (entryModuleOnly) {
response.dependencies = response.dependencies.filter(module =>
@ -331,6 +333,8 @@ class Bundler {
? runBeforeMainModule
.map(name => modulesByName[name])
.filter(Boolean)
/* $FlowFixMe: looks like ResolutionResponse is monkey-patched
* with `getModuleId`. */
.map(response.getModuleId)
: undefined;

View File

@ -5,13 +5,29 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
import Module from '../Module';
const NO_OPTIONS = {};
class ResolutionResponse {
constructor({transformOptions}) {
transformOptions: {};
dependencies: Array<Module>;
mainModuleId: ?(number | string);
mocks: mixed;
numPrependedDependencies: number;
_mappings: {};
_finalized: boolean;
_mainModule: ?Module;
constructor({transformOptions}: {transformOptions: {}}) {
this.transformOptions = transformOptions;
this.dependencies = [];
this.mainModuleId = null;
@ -21,7 +37,11 @@ class ResolutionResponse {
this._finalized = false;
}
copy(properties) {
copy(properties: {
dependencies: Array<Module>,
mainModuleId: number,
mocks: mixed,
}): ResolutionResponse {
const {
dependencies = this.dependencies,
mainModuleId = this.mainModuleId,
@ -31,6 +51,7 @@ class ResolutionResponse {
const numPrependedDependencies = dependencies === this.dependencies
? this.numPrependedDependencies : 0;
/* $FlowFixMe: Flow doesn't like Object.assign on class-made objects. */
return Object.assign(
new this.constructor({transformOptions: this.transformOptions}),
this,
@ -56,6 +77,7 @@ class ResolutionResponse {
}
finalize() {
/* $FlowFixMe: _mainModule is not initialized in the constructor. */
return this._mainModule.getName().then(id => {
this.mainModuleId = id;
this._finalized = true;
@ -63,7 +85,7 @@ class ResolutionResponse {
});
}
pushDependency(module) {
pushDependency(module: Module) {
this._assertNotFinalized();
if (this.dependencies.length === 0) {
this._mainModule = module;
@ -72,13 +94,17 @@ class ResolutionResponse {
this.dependencies.push(module);
}
prependDependency(module) {
prependDependency(module: Module) {
this._assertNotFinalized();
this.dependencies.unshift(module);
this.numPrependedDependencies += 1;
}
setResolvedDependencyPairs(module, pairs, options = NO_OPTIONS) {
setResolvedDependencyPairs(
module: Module,
pairs: mixed,
options: {ignoreFinalized?: boolean} = NO_OPTIONS,
) {
if (!options.ignoreFinalized) {
this._assertNotFinalized();
}
@ -88,11 +114,11 @@ class ResolutionResponse {
}
}
setMocks(mocks) {
setMocks(mocks: mixed) {
this.mocks = mocks;
}
getResolvedDependencyPairs(module) {
getResolvedDependencyPairs(module: Module) {
this._assertFinalized();
return this._mappings[module.hash()];
}

View File

@ -134,7 +134,7 @@ class Module {
return this.read(transformOptions).then(({map}) => map);
}
getName(): Promise<string> {
getName(): Promise<string | number> {
return this._cache.get(
this.path,
'name',

View File

@ -301,7 +301,7 @@ class DependencyGraph {
}: {
entryPath: string,
platform: string,
transformOptions: mixed,
transformOptions: {},
onProgress: () => void,
recursive: boolean,
}) {