diff --git a/packager/src/Bundler/index.js b/packager/src/Bundler/index.js index d5da5d7cd14..60edab60380 100644 --- a/packager/src/Bundler/index.js +++ b/packager/src/Bundler/index.js @@ -642,7 +642,7 @@ class Bundler { entryFilePath: string, options: BundlingOptions, getModuleId: () => number, - dependencyPairs: Array<[mixed, {path: string}]>, + dependencyPairs: Array<[string, Module]>, assetPlugins: Array, }): Promise { let moduleTransport; diff --git a/packager/src/Server/index.js b/packager/src/Server/index.js index f9dcf6e626a..fae8323b7c2 100644 --- a/packager/src/Server/index.js +++ b/packager/src/Server/index.js @@ -552,6 +552,7 @@ class Server { changedModules.forEach(m => { response.setResolvedDependencyPairs( m, + /* $FlowFixMe: should be enforced not to be null. */ dependencyPairs.get(m.path), {ignoreFinalized: true}, ); diff --git a/packager/src/lib/ModuleTransport.js b/packager/src/lib/ModuleTransport.js index 1c4e48bdd86..4ba202673ea 100644 --- a/packager/src/lib/ModuleTransport.js +++ b/packager/src/lib/ModuleTransport.js @@ -12,13 +12,14 @@ 'use strict'; import type {RawMapping} from '../Bundler/source-map'; +import type Module from '../node-haste/Module'; import type {SourceMap} from './SourceMap'; type SourceMapOrMappings = SourceMap | Array; type Metadata = { dependencies?: ?Array, - dependencyPairs?: Array<[mixed, {path: string}]>, + dependencyPairs?: Array<[string, Module]>, preloaded: ?boolean, }; diff --git a/packager/src/node-haste/DependencyGraph/ResolutionRequest.js b/packager/src/node-haste/DependencyGraph/ResolutionRequest.js index 4a973778db1..da674da3ba8 100644 --- a/packager/src/node-haste/DependencyGraph/ResolutionRequest.js +++ b/packager/src/node-haste/DependencyGraph/ResolutionRequest.js @@ -214,9 +214,10 @@ class ResolutionRequest { ); }); - const collectedDependencies = new MapWithDefaults(module => - collect(module), - ); + const collectedDependencies: MapWithDefaults< + TModule, + Promise>, + > = new MapWithDefaults(module => collect(module)); const crawlDependencies = (mod, [depNames, dependencies]) => { const filteredPairs = []; diff --git a/packager/src/node-haste/DependencyGraph/ResolutionResponse.js b/packager/src/node-haste/DependencyGraph/ResolutionResponse.js index 2b1bbb134a9..1b3eca62579 100644 --- a/packager/src/node-haste/DependencyGraph/ResolutionResponse.js +++ b/packager/src/node-haste/DependencyGraph/ResolutionResponse.js @@ -11,8 +11,6 @@ 'use strict'; -import type Module from '../Module'; - const NO_OPTIONS = {}; class ResolutionResponse { @@ -26,7 +24,7 @@ class ResolutionResponse { // This is monkey-patched from Resolver. getModuleId: ?() => number; - _mappings: {}; + _mappings: {[hash: string]: Array<[string, TModule]>}; _finalized: boolean; _mainModule: ?TModule; @@ -104,8 +102,8 @@ class ResolutionResponse { } setResolvedDependencyPairs( - module: Module, - pairs: mixed, + module: TModule, + pairs: Array<[string, TModule]>, options: {ignoreFinalized?: boolean} = NO_OPTIONS, ) { if (!options.ignoreFinalized) { @@ -121,7 +119,7 @@ class ResolutionResponse { this.mocks = mocks; } - getResolvedDependencyPairs(module: TModule) { + getResolvedDependencyPairs(module: TModule): $ReadOnlyArray<[string, TModule]> { this._assertFinalized(); return this._mappings[module.hash()]; } diff --git a/packager/src/node-haste/lib/MapWithDefaults.js b/packager/src/node-haste/lib/MapWithDefaults.js index a264f0c2518..ce79e045051 100644 --- a/packager/src/node-haste/lib/MapWithDefaults.js +++ b/packager/src/node-haste/lib/MapWithDefaults.js @@ -1,30 +1,35 @@ - /** +/** * Copyright (c) 2016-present, Facebook, Inc. * All rights reserved. * * 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'; -module.exports = function MapWithDefaults(factory, iterable) { - // This can't be `MapWithDefaults extends Map`, b/c the way babel transforms - // super calls in constructors: Map.call(this, iterable) throws for native - // Map objects in node 4+. - // TODO(davidaurelio) switch to a transform that does not transform classes - // and super calls, and change this into a class +class MapWithDefaults extends Map { + _factory: TK => TV; - const map = iterable ? new Map(iterable) : new Map(); - const {get} = map; - map.get = key => { - if (map.has(key)) { - return get.call(map, key); + constructor(factory: TK => TV, iterable?: Iterable<[TK, TV]>) { + super(iterable); + this._factory = factory; + } + + get(key: TK): TV { + if (this.has(key)) { + /* $FlowFixMe: can never be `undefined` since we tested with `has` + * (except if `TV` includes `void` as subtype, ex. is nullable) */ + return Map.prototype.get.call(this, key); } - - const value = factory(key); - map.set(key, value); + const value = this._factory(key); + this.set(key, value); return value; - }; - return map; -}; + } +} + +module.exports = MapWithDefaults; diff --git a/packager/src/node-haste/lib/__tests__/MapWithDefaults-test.js b/packager/src/node-haste/lib/__tests__/MapWithDefaults-test.js new file mode 100644 index 00000000000..efb122b3d7b --- /dev/null +++ b/packager/src/node-haste/lib/__tests__/MapWithDefaults-test.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * 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. + * + * @format + */ + +'use strict'; + +jest.disableAutomock(); + +const MapWithDefaults = require('../MapWithDefaults'); + +describe('MapWithDefaults', function() { + it('works', () => { + const map = new MapWithDefaults(() => ['bar']); + map.get('foo').push('baz'); + expect(map.get('foo')).toEqual(['bar', 'baz']); + }); +}); diff --git a/packager/src/node-haste/lib/getInverseDependencies.js b/packager/src/node-haste/lib/getInverseDependencies.js index 6ff90334f87..663208ee760 100644 --- a/packager/src/node-haste/lib/getInverseDependencies.js +++ b/packager/src/node-haste/lib/getInverseDependencies.js @@ -17,7 +17,7 @@ import type ResolutionResponse from '../DependencyGraph/ResolutionResponse'; function resolveModuleRequires( resolutionResponse: ResolutionResponse, module: TModule, -) { +): Array { const pairs = resolutionResponse.getResolvedDependencyPairs(module); return pairs ? pairs.map(([, dependencyModule]) => dependencyModule) : []; }