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

View File

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

View File

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

View File

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