@@ -155,6 +155,7 @@ module ts {
155
155
let commonSourceDirectory : string ;
156
156
let diagnosticsProducingTypeChecker : TypeChecker ;
157
157
let noDiagnosticsTypeChecker : TypeChecker ;
158
+ let resolvedExternalModuleCache : Map < string > = { } ;
158
159
159
160
let start = new Date ( ) . getTime ( ) ;
160
161
@@ -184,6 +185,7 @@ module ts {
184
185
getIdentifierCount : ( ) => getDiagnosticsProducingTypeChecker ( ) . getIdentifierCount ( ) ,
185
186
getSymbolCount : ( ) => getDiagnosticsProducingTypeChecker ( ) . getSymbolCount ( ) ,
186
187
getTypeCount : ( ) => getDiagnosticsProducingTypeChecker ( ) . getTypeCount ( ) ,
188
+ resolveExternalModule
187
189
} ;
188
190
return program ;
189
191
@@ -236,6 +238,60 @@ module ts {
236
238
emitTime += new Date ( ) . getTime ( ) - start ;
237
239
return emitResult ;
238
240
}
241
+
242
+ function resolveExternalModule ( moduleName : string , searchPath : string ) : string {
243
+ let cacheLookupName = moduleName + searchPath ;
244
+ if ( resolvedExternalModuleCache [ cacheLookupName ] ) {
245
+ return resolvedExternalModuleCache [ cacheLookupName ] ;
246
+ }
247
+ if ( resolvedExternalModuleCache [ cacheLookupName ] === '' ) {
248
+ return undefined ;
249
+ }
250
+ function getNameIfExists ( fileName : string ) : string {
251
+ if ( sys . fileExists ( fileName ) ) {
252
+ return fileName ;
253
+ }
254
+ }
255
+ while ( true ) {
256
+ // Look at files by all extensions
257
+ let found = forEach ( supportedExtensions ,
258
+ extension => getNameIfExists ( normalizePath ( combinePaths ( searchPath , moduleName ) ) + extension ) ) ;
259
+ // Also look at all files by node_modules
260
+ if ( ! found ) {
261
+ found = forEach ( supportedExtensions ,
262
+ extension => getNameIfExists ( normalizePath ( combinePaths ( combinePaths ( searchPath , "node_modules" ) , moduleName ) ) + extension ) ) ;
263
+ }
264
+ // Also look at package.json's main in node_modules
265
+ if ( ! found ) {
266
+ // If we found a package.json then look at its main field
267
+ let pkgJson = getNameIfExists ( normalizePath ( combinePaths ( combinePaths ( combinePaths ( searchPath , "node_modules" ) , moduleName ) , "package.json" ) ) ) ;
268
+ if ( pkgJson ) {
269
+ let pkgFile = JSON . parse ( sys . readFile ( pkgJson ) ) ;
270
+ if ( pkgFile . main ) {
271
+ var indexFileName = removeFileExtension ( combinePaths ( getDirectoryPath ( pkgJson ) , pkgFile . main ) ) ;
272
+ found = forEach ( supportedExtensions ,
273
+ extension => getNameIfExists ( indexFileName + extension ) )
274
+ }
275
+ }
276
+ }
277
+ // look at node_modules index
278
+ if ( ! found ) {
279
+ found = forEach ( supportedExtensions ,
280
+ extension => getNameIfExists ( normalizePath ( combinePaths ( combinePaths ( combinePaths ( searchPath , "node_modules" ) , moduleName ) , "index" ) ) + extension ) ) ;
281
+ }
282
+
283
+ // Finally cache and return or continue up the directory tree
284
+ if ( found ) {
285
+ return resolvedExternalModuleCache [ cacheLookupName ] = found ;
286
+ }
287
+ let parentPath = getDirectoryPath ( searchPath ) ;
288
+ if ( parentPath === searchPath ) {
289
+ resolvedExternalModuleCache [ cacheLookupName ] = '' ;
290
+ return undefined ;
291
+ }
292
+ searchPath = parentPath ;
293
+ }
294
+ }
239
295
240
296
function getSourceFile ( fileName : string ) {
241
297
fileName = host . getCanonicalFileName ( normalizeSlashes ( fileName ) ) ;
@@ -428,18 +484,9 @@ module ts {
428
484
if ( moduleNameExpr && moduleNameExpr . kind === SyntaxKind . StringLiteral ) {
429
485
let moduleNameText = ( < LiteralExpression > moduleNameExpr ) . text ;
430
486
if ( moduleNameText ) {
431
- let searchPath = basePath ;
432
- let searchName : string ;
433
- while ( true ) {
434
- searchName = normalizePath ( combinePaths ( searchPath , moduleNameText ) ) ;
435
- if ( forEach ( supportedExtensions , extension => findModuleSourceFile ( searchName + extension , moduleNameExpr ) ) ) {
436
- break ;
437
- }
438
- let parentPath = getDirectoryPath ( searchPath ) ;
439
- if ( parentPath === searchPath ) {
440
- break ;
441
- }
442
- searchPath = parentPath ;
487
+ let resolvedName : string = resolveExternalModule ( moduleNameText , getDirectoryPath ( file . fileName ) ) ;
488
+ if ( resolvedName ) {
489
+ findModuleSourceFile ( resolvedName , moduleNameExpr ) ;
443
490
}
444
491
}
445
492
}
0 commit comments