From 79ff67320f152f943bbd7d127e07f64f896f0c6a Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 17 May 2017 09:28:05 -0700 Subject: [PATCH] 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 --- .../metro-bundler/src/Bundler/BundleBase.js | 16 +++---- .../metro-bundler/src/Bundler/HMRBundle.js | 43 ++++++++++++++++--- packages/metro-bundler/src/Bundler/index.js | 2 +- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/packages/metro-bundler/src/Bundler/BundleBase.js b/packages/metro-bundler/src/Bundler/BundleBase.js index 84a53630..5ddd966d 100644 --- a/packages/metro-bundler/src/Bundler/BundleBase.js +++ b/packages/metro-bundler/src/Bundler/BundleBase.js @@ -28,18 +28,18 @@ class BundleBase { _assets: Array; _finalized: boolean; _mainModuleId: number | void; - _modules: Array; _source: ?string; + __modules: Array; constructor() { this._finalized = false; - this._modules = []; + this.__modules = []; this._assets = []; this._mainModuleId = undefined; } isEmpty() { - return this._modules.length === 0 && this._assets.length === 0; + return this.__modules.length === 0 && this._assets.length === 0; } getMainModuleId() { @@ -55,7 +55,7 @@ class BundleBase { throw new Error('Expected a ModuleTransport object'); } - return this._modules.push(module) - 1; + return this.__modules.push(module) - 1; } replaceModuleAt(index: number, module: ModuleTransport) { @@ -63,11 +63,11 @@ class BundleBase { throw new Error('Expeceted a ModuleTransport object'); } - this._modules[index] = module; + this.__modules[index] = module; } getModules() { - return this._modules; + return this.__modules; } getAssets() { @@ -80,7 +80,7 @@ class BundleBase { finalize(options: FinalizeOptions) { if (!options.allowUpdates) { - Object.freeze(this._modules); + Object.freeze(this.__modules); Object.freeze(this._assets); } @@ -94,7 +94,7 @@ class BundleBase { 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; } diff --git a/packages/metro-bundler/src/Bundler/HMRBundle.js b/packages/metro-bundler/src/Bundler/HMRBundle.js index 065bbd1e..5953f7f7 100644 --- a/packages/metro-bundler/src/Bundler/HMRBundle.js +++ b/packages/metro-bundler/src/Bundler/HMRBundle.js @@ -5,14 +5,34 @@ * 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 + * @format */ + 'use strict'; const BundleBase = require('./BundleBase'); 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 { - constructor({sourceURLFn, sourceMappingURLFn}) { + _sourceMappingURLFn: (hmrpath: string) => mixed; + _sourceMappingURLs: Array; + _sourceURLFn: (hmrpath: string) => mixed; + _sourceURLs: Array; + + constructor({ + sourceURLFn, + sourceMappingURLFn, + }: { + sourceURLFn: (hmrpath: string) => mixed, + sourceMappingURLFn: (hmrpath: string) => mixed, + }) { super(); this._sourceURLFn = sourceURLFn; this._sourceMappingURLFn = sourceMappingURLFn; @@ -20,22 +40,35 @@ class HMRBundle extends BundleBase { 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, + /* $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( response, module, moduleTransport.code, + /* $FlowFixMe: may not exist */ moduleTransport.meta.dependencyOffsets, ); 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)); - return Promise.resolve(); + // inconsistent with parent class return type + return (Promise.resolve(): any); } getModulesIdsAndCode() { - return this._modules.map(module => { + return this.__modules.map(module => { return { id: JSON.stringify(module.id), code: module.code, diff --git a/packages/metro-bundler/src/Bundler/index.js b/packages/metro-bundler/src/Bundler/index.js index 60edab60..38cf5ac0 100644 --- a/packages/metro-bundler/src/Bundler/index.js +++ b/packages/metro-bundler/src/Bundler/index.js @@ -338,7 +338,7 @@ class Bundler { unbundle, }: { assetPlugins?: Array, - bundle: Bundle, + bundle: Bundle | HMRBundle, dev: boolean, entryFile?: string, entryModuleOnly?: boolean,