88
99'use strict' ;
1010
11- const HasteMap = require ( 'jest-haste-map' ) ;
12-
11+ const createHasteMap = require ( '../lib/createHasteMap' ) ;
1312const path = require ( 'path' ) ;
1413
1514/* eslint-disable max-len */
@@ -19,36 +18,23 @@ const REQUIRE_EXTENSIONS_PATTERN = /(\b(?:require\s*?\.\s*?(?:requireActual|requ
1918class HasteResolver {
2019
2120 constructor ( config , options ) {
22- const extensions = Array . from ( new Set (
23- config . moduleFileExtensions . concat ( config . testFileExtensions )
24- ) ) ;
25- const ignorePattern = new RegExp (
26- [ config . cacheDirectory ] . concat ( config . modulePathIgnorePatterns ) . join ( '|' )
27- ) ;
28-
21+ this . _map = createHasteMap ( config , options ) ;
2922 this . _defaultPlatform = config . haste . defaultPlatform ;
3023 this . _mocksPattern = new RegExp ( config . mocksPattern ) ;
31-
3224 this . _mappedModuleNames = Object . create ( null ) ;
3325 if ( config . moduleNameMapper . length ) {
3426 config . moduleNameMapper . forEach (
3527 map => this . _mappedModuleNames [ map [ 1 ] ] = new RegExp ( map [ 0 ] )
3628 ) ;
3729 }
3830
39- this . _map = new HasteMap ( {
40- cacheDirectory : options . cacheDirectory ,
41- extensions,
42- ignorePattern,
43- providesModuleNodeModules : config . haste . providesModuleNodeModules ,
44- platforms : config . haste . platforms || [ 'ios' , 'android' ] ,
45- resetCache : options . resetCache ,
46- roots : config . testPathDirs ,
47- useWatchman : config . watchman ,
48- } ) ;
49-
50- // warm-up
51- this . _map . build ( ) ;
31+ // warm-up and cache mocks
32+ this . _hasteMapPromise = this . _map . build ( ) . then ( data =>
33+ this . _getAllMocks ( ) . then ( mocks => {
34+ data . mocks = mocks ;
35+ return this . _map . persist ( data ) ;
36+ } )
37+ ) ;
5238
5339 /*extractRequires: code => {
5440 const data = HasteMap.extractRequires(code);
@@ -84,32 +70,7 @@ class HasteResolver {
8470 }
8571
8672 getHasteMap ( ) {
87- if ( this . _mapPromise ) {
88- return this . _mapPromise ;
89- }
90-
91- return this . _mapPromise = Promise . all ( [
92- this . _map . build ( ) ,
93- this . _getAllMocks ( ) ,
94- ] ) . then ( data => this . _getModuleMap ( data [ 0 ] . map , data [ 1 ] ) ) ;
95- }
96-
97- _getModuleMap ( map , mocks ) {
98- const modules = Object . create ( null ) ;
99- const packages = Object . create ( null ) ;
100- for ( const name in map ) {
101- const module =
102- map [ name ] [ this . _defaultPlatform ] ||
103- map [ name ] [ HasteMap . GENERIC_PLATFORM ] ;
104- if ( module ) {
105- if ( module . type == 'package' ) {
106- packages [ name ] = module . path ;
107- } else {
108- modules [ name ] = module . path ;
109- }
110- }
111- }
112- return { modules, mocks, packages} ;
73+ return this . _hasteMapPromise ;
11374 }
11475
11576 _getAllMocks ( ) {
0 commit comments