packager: HMRBundle: @flow

Summary: Better to have *some* Flow coverage (I'd like 100% of packager covered from when we split it apart) than none.

Reviewed By: davidaurelio

Differential Revision: D5077757

fbshipit-source-id: b23169b3edf2bd3eb0e8a399d099151aa705b198
This commit is contained in:
Jean Lauliac 2017-05-17 09:28:05 -07:00 committed by Facebook Github Bot
parent c3430956df
commit 79ff67320f
3 changed files with 47 additions and 14 deletions

View File

@ -28,18 +28,18 @@ class BundleBase {
_assets: Array<mixed>; _assets: Array<mixed>;
_finalized: boolean; _finalized: boolean;
_mainModuleId: number | void; _mainModuleId: number | void;
_modules: Array<ModuleTransport>;
_source: ?string; _source: ?string;
__modules: Array<ModuleTransport>;
constructor() { constructor() {
this._finalized = false; this._finalized = false;
this._modules = []; this.__modules = [];
this._assets = []; this._assets = [];
this._mainModuleId = undefined; this._mainModuleId = undefined;
} }
isEmpty() { isEmpty() {
return this._modules.length === 0 && this._assets.length === 0; return this.__modules.length === 0 && this._assets.length === 0;
} }
getMainModuleId() { getMainModuleId() {
@ -55,7 +55,7 @@ class BundleBase {
throw new Error('Expected a ModuleTransport object'); throw new Error('Expected a ModuleTransport object');
} }
return this._modules.push(module) - 1; return this.__modules.push(module) - 1;
} }
replaceModuleAt(index: number, module: ModuleTransport) { replaceModuleAt(index: number, module: ModuleTransport) {
@ -63,11 +63,11 @@ class BundleBase {
throw new Error('Expeceted a ModuleTransport object'); throw new Error('Expeceted a ModuleTransport object');
} }
this._modules[index] = module; this.__modules[index] = module;
} }
getModules() { getModules() {
return this._modules; return this.__modules;
} }
getAssets() { getAssets() {
@ -80,7 +80,7 @@ class BundleBase {
finalize(options: FinalizeOptions) { finalize(options: FinalizeOptions) {
if (!options.allowUpdates) { if (!options.allowUpdates) {
Object.freeze(this._modules); Object.freeze(this.__modules);
Object.freeze(this._assets); Object.freeze(this._assets);
} }
@ -94,7 +94,7 @@ class BundleBase {
return this._source; return this._source;
} }
this._source = this._modules.map(module => module.code).join('\n'); this._source = this.__modules.map(module => module.code).join('\n');
return this._source; return this._source;
} }

View File

@ -5,14 +5,34 @@
* 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
* @format
*/ */
'use strict'; 'use strict';
const BundleBase = require('./BundleBase'); const BundleBase = require('./BundleBase');
const ModuleTransport = require('../lib/ModuleTransport'); const ModuleTransport = require('../lib/ModuleTransport');
import type Resolver from '../Resolver';
import type ResolutionResponse
from '../node-haste/DependencyGraph/ResolutionResponse';
import type Module from '../node-haste/Module';
class HMRBundle extends BundleBase { class HMRBundle extends BundleBase {
constructor({sourceURLFn, sourceMappingURLFn}) { _sourceMappingURLFn: (hmrpath: string) => mixed;
_sourceMappingURLs: Array<mixed>;
_sourceURLFn: (hmrpath: string) => mixed;
_sourceURLs: Array<mixed>;
constructor({
sourceURLFn,
sourceMappingURLFn,
}: {
sourceURLFn: (hmrpath: string) => mixed,
sourceMappingURLFn: (hmrpath: string) => mixed,
}) {
super(); super();
this._sourceURLFn = sourceURLFn; this._sourceURLFn = sourceURLFn;
this._sourceMappingURLFn = sourceMappingURLFn; this._sourceMappingURLFn = sourceMappingURLFn;
@ -20,22 +40,35 @@ class HMRBundle extends BundleBase {
this._sourceMappingURLs = []; this._sourceMappingURLs = [];
} }
addModule(resolver, response, module, moduleTransport) { addModule(
/* $FlowFixMe: broken OOP design: function signature should be the same */
resolver: Resolver,
/* $FlowFixMe: broken OOP design: function signature should be the same */
response: ResolutionResponse<Module, {}>,
/* $FlowFixMe: broken OOP design: function signature should be the same */
module: Module,
/* $FlowFixMe: broken OOP design: function signature should be the same */
moduleTransport: ModuleTransport,
) {
const code = resolver.resolveRequires( const code = resolver.resolveRequires(
response, response,
module, module,
moduleTransport.code, moduleTransport.code,
/* $FlowFixMe: may not exist */
moduleTransport.meta.dependencyOffsets, moduleTransport.meta.dependencyOffsets,
); );
super.addModule(new ModuleTransport({...moduleTransport, code})); super.addModule(new ModuleTransport({...moduleTransport, code}));
this._sourceMappingURLs.push(this._sourceMappingURLFn(moduleTransport.sourcePath)); this._sourceMappingURLs.push(
this._sourceMappingURLFn(moduleTransport.sourcePath),
);
this._sourceURLs.push(this._sourceURLFn(moduleTransport.sourcePath)); this._sourceURLs.push(this._sourceURLFn(moduleTransport.sourcePath));
return Promise.resolve(); // inconsistent with parent class return type
return (Promise.resolve(): any);
} }
getModulesIdsAndCode() { getModulesIdsAndCode() {
return this._modules.map(module => { return this.__modules.map(module => {
return { return {
id: JSON.stringify(module.id), id: JSON.stringify(module.id),
code: module.code, code: module.code,

View File

@ -338,7 +338,7 @@ class Bundler {
unbundle, unbundle,
}: { }: {
assetPlugins?: Array<string>, assetPlugins?: Array<string>,
bundle: Bundle, bundle: Bundle | HMRBundle,
dev: boolean, dev: boolean,
entryFile?: string, entryFile?: string,
entryModuleOnly?: boolean, entryModuleOnly?: boolean,