fix(bazel): Cache fileNameToModuleName lookups (#25731)
This saves expensive re-parsing of the file when not run as a Bazel worker PR Close #25731
This commit is contained in:
		
							parent
							
								
									268cf7989c
								
							
						
					
					
						commit
						f394ba0e27
					
				| @ -224,14 +224,21 @@ export function compile({allowNonHermeticReads, allDepsCompiledWithBazel = true, | ||||
|   }; | ||||
| 
 | ||||
|   const ngHost = ng.createCompilerHost({options: compilerOpts, tsHost: bazelHost}); | ||||
| 
 | ||||
|   const fileNameToModuleNameCache = new Map<string, string>(); | ||||
|   ngHost.fileNameToModuleName = (importedFilePath: string, containingFilePath: string) => { | ||||
|     // Lookup the module name specified in the TypeScript source file, if present
 | ||||
|     // it will look like ///<amd module-name="something"/>
 | ||||
|     // For performance, we only do this for .d.ts files, to avoid parsing lots of
 | ||||
|     // additional files that were not in the program.
 | ||||
|     // (We don't have the ts.Program available in this codepath)
 | ||||
|     if (importedFilePath.endsWith('.d.ts')) { | ||||
|     // Memoize this lookup to avoid expensive re-parses of the same file
 | ||||
|     // When run as a worker, the actual ts.SourceFile is cached
 | ||||
|     // but when we don't run as a worker, there is no cache.
 | ||||
|     // For one example target in g3, we saw a cache hit rate of 7590/7695
 | ||||
|     if (fileNameToModuleNameCache.has(importedFilePath)) { | ||||
|       return fileNameToModuleNameCache.get(importedFilePath); | ||||
|     } | ||||
|     const result = doFileNameToModuleName(importedFilePath); | ||||
|     fileNameToModuleNameCache.set(importedFilePath, result); | ||||
|     return result; | ||||
|   }; | ||||
| 
 | ||||
|   function doFileNameToModuleName(importedFilePath: string): string { | ||||
|     try { | ||||
|       const sourceFile = ngHost.getSourceFile(importedFilePath, ts.ScriptTarget.Latest); | ||||
|       if (sourceFile && sourceFile.moduleName) { | ||||
| @ -241,7 +248,6 @@ export function compile({allowNonHermeticReads, allDepsCompiledWithBazel = true, | ||||
|       // File does not exist or parse error. Ignore this case and continue onto the
 | ||||
|       // other methods of resolving the module below.
 | ||||
|     } | ||||
|     } | ||||
|     if ((compilerOpts.module === ts.ModuleKind.UMD || compilerOpts.module === ts.ModuleKind.AMD) && | ||||
|         ngHost.amdModuleName) { | ||||
|       return ngHost.amdModuleName({ fileName: importedFilePath } as ts.SourceFile); | ||||
| @ -251,7 +257,8 @@ export function compile({allowNonHermeticReads, allDepsCompiledWithBazel = true, | ||||
|       return result.substr(NODE_MODULES.length); | ||||
|     } | ||||
|     return bazelOpts.workspaceName + '/' + result; | ||||
|   }; | ||||
|   } | ||||
| 
 | ||||
|   ngHost.toSummaryFileName = (fileName: string, referringSrcFileName: string) => path.posix.join( | ||||
|       bazelOpts.workspaceName, | ||||
|       relativeToRootDirs(fileName, compilerOpts.rootDirs).replace(EXT, '')); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user