@@ -1073,9 +1073,16 @@ namespace ts {
10731073 // - This calls resolveModuleNames, and then calls findSourceFile for each resolved module.
10741074 // As all these operations happen - and are nested - within the createProgram call, they close over the below variables.
10751075 // The current resolution depth is tracked by incrementing/decrementing as the depth first search progresses.
1076- const maxNodeModulesJsDepth = options . maxNodeModuleJsDepth || 2 ;
1076+ const maxNodeModulesJsDepth = typeof options . maxNodeModuleJsDepth === "number" ? options . maxNodeModuleJsDepth : 2 ;
10771077 let currentNodeModulesJsDepth = 0 ;
10781078
1079+ // If a module has some of its imports skipped due to being at the depth limit under node_modules, then track
1080+ // this, as it may be imported at a shallower depth later, and then it will need its skipped imports processed.
1081+ const modulesWithElidedImports : Map < boolean > = { } ;
1082+
1083+ // Track source files that are JavaScript files found by searching under node_modules, as these shouldn't be compiled.
1084+ const jsFilesFoundSearchingNodeModules : Map < boolean > = { } ;
1085+
10791086 const start = new Date ( ) . getTime ( ) ;
10801087
10811088 host = host || createCompilerHost ( options ) ;
@@ -1230,6 +1237,7 @@ namespace ts {
12301237 ( oldOptions . rootDir !== options . rootDir ) ||
12311238 ( oldOptions . configFilePath !== options . configFilePath ) ||
12321239 ( oldOptions . baseUrl !== options . baseUrl ) ||
1240+ ( oldOptions . maxNodeModuleJsDepth !== options . maxNodeModuleJsDepth ) ||
12331241 ! arrayIsEqualTo ( oldOptions . typeRoots , oldOptions . typeRoots ) ||
12341242 ! arrayIsEqualTo ( oldOptions . rootDirs , options . rootDirs ) ||
12351243 ! mapIsEqualTo ( oldOptions . paths , options . paths ) ) {
@@ -1353,7 +1361,10 @@ namespace ts {
13531361 getNewLine : ( ) => host . getNewLine ( ) ,
13541362 getSourceFile : program . getSourceFile ,
13551363 getSourceFileByPath : program . getSourceFileByPath ,
1356- getSourceFiles : program . getSourceFiles ,
1364+ getSourceFiles : ( ) => filter ( program . getSourceFiles ( ) ,
1365+ // Remove JavaScript files found by searching node_modules from the source files to emit
1366+ sourceFile => ! lookUp ( jsFilesFoundSearchingNodeModules , sourceFile . path )
1367+ ) ,
13571368 writeFile : writeFileCallback || (
13581369 ( fileName , data , writeByteOrderMark , onError , sourceFiles ) => host . writeFile ( fileName , data , writeByteOrderMark , onError , sourceFiles ) ) ,
13591370 isEmitBlocked,
@@ -1888,6 +1899,14 @@ namespace ts {
18881899 reportFileNamesDifferOnlyInCasingError ( fileName , file . fileName , refFile , refPos , refEnd ) ;
18891900 }
18901901
1902+ // See if we need to reprocess the imports due to prior skipped imports
1903+ if ( file && lookUp ( modulesWithElidedImports , file . path ) ) {
1904+ if ( currentNodeModulesJsDepth < maxNodeModulesJsDepth ) { // TODO: Check for off-by-ones
1905+ modulesWithElidedImports [ file . path ] = false ;
1906+ processImportedModules ( file , getDirectoryPath ( fileName ) ) ;
1907+ }
1908+ }
1909+
18911910 return file ;
18921911 }
18931912
@@ -2026,27 +2045,36 @@ namespace ts {
20262045 for ( let i = 0 ; i < moduleNames . length ; i ++ ) {
20272046 const resolution = resolutions [ i ] ;
20282047 setResolvedModule ( file , moduleNames [ i ] , resolution ) ;
2048+ const resolvedPath = resolution ? toPath ( resolution . resolvedFileName , currentDirectory , getCanonicalFileName ) : undefined ;
2049+
20292050 // add file to program only if:
20302051 // - resolution was successful
20312052 // - noResolve is falsy
20322053 // - module name comes from the list of imports
20332054 // - it's not a top level JavaScript module that exceeded the search max
20342055 const isJsFileUnderNodeModules = resolution && resolution . isExternalLibraryImport &&
20352056 hasJavaScriptFileExtension ( resolution . resolvedFileName ) ;
2057+
20362058 if ( isJsFileUnderNodeModules ) {
2059+ jsFilesFoundSearchingNodeModules [ resolvedPath ] = true ;
20372060 currentNodeModulesJsDepth ++ ;
20382061 }
2039- const shouldAddFile = resolution && ! options . noResolve && i < file . imports . length &&
2040- ! ( isJsFileUnderNodeModules && currentNodeModulesJsDepth > maxNodeModulesJsDepth ) ;
20412062
2042- if ( shouldAddFile ) {
2063+ const elideImport = isJsFileUnderNodeModules && currentNodeModulesJsDepth > maxNodeModulesJsDepth ;
2064+ const shouldAddFile = resolution && ! options . noResolve && i < file . imports . length && ! elideImport ;
2065+
2066+ if ( elideImport ) {
2067+ modulesWithElidedImports [ file . path ] = true ;
2068+ }
2069+ else if ( shouldAddFile ) {
20432070 findSourceFile ( resolution . resolvedFileName ,
2044- toPath ( resolution . resolvedFileName , currentDirectory , getCanonicalFileName ) ,
2071+ resolvedPath ,
20452072 /*isDefaultLib*/ false , /*isReference*/ false ,
20462073 file ,
20472074 skipTrivia ( file . text , file . imports [ i ] . pos ) ,
20482075 file . imports [ i ] . end ) ;
20492076 }
2077+
20502078 if ( isJsFileUnderNodeModules ) {
20512079 currentNodeModulesJsDepth -- ;
20522080 }
0 commit comments