@@ -58,7 +58,6 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
5858 private genInternalTypings : boolean = false ;
5959
6060 static includeExtensions = [ '.ts' ] ;
61- static excludeExtensions = [ '.d.ts' ] ;
6261
6362 constructor ( public inputPath : string , public cachePath : string , public options ) {
6463 if ( options . rootFilePaths ) {
@@ -83,11 +82,7 @@ class DiffingTSCompiler implements DiffingBroccoliPlugin {
8382 this . genInternalTypings = false ;
8483 }
8584
86- // TODO: the above turns rootDir set to './' into an empty string - looks like a tsc bug
87- // check back when we upgrade to 1.7.x
88- if ( this . tsOpts . rootDir === '' ) {
89- this . tsOpts . rootDir = './' ;
90- }
85+ this . tsOpts . rootDir = inputPath ;
9186 this . tsOpts . outDir = this . cachePath ;
9287
9388 this . tsServiceHost = new CustomLanguageServiceHost ( this . tsOpts , this . rootFilePaths ,
@@ -331,54 +326,34 @@ class CustomLanguageServiceHost implements ts.LanguageServiceHost {
331326 }
332327
333328
334- getScriptFileNames ( ) : string [ ] { return this . fileNames ; }
329+ getScriptFileNames ( ) : string [ ] {
330+ return this . fileNames . map ( f => path . join ( this . treeInputPath , f ) ) ;
331+ }
335332
336333
337334 getScriptVersion ( fileName : string ) : string {
338335 return this . fileRegistry [ fileName ] && this . fileRegistry [ fileName ] . version . toString ( ) ;
339336 }
340337
341338
342- /**
343- * This method is called quite a bit to lookup 3 kinds of paths:
344- * 1/ files in the fileRegistry
345- * - these are the files in our project that we are watching for changes
346- * - in the future we could add caching for these files and invalidate the cache when
347- * the file is changed lazily during lookup
348- * 2/ .d.ts and library files not in the fileRegistry
349- * - these are not our files, they come from tsd or typescript itself
350- * - these files change only rarely but since we need them very rarely, it's not worth the
351- * cache invalidation hassle to cache them
352- * 3/ bogus paths that typescript compiler tries to lookup during import resolution
353- * - these paths are tricky to cache since files come and go and paths that was bogus in the
354- * past might not be bogus later
355- *
356- * In the initial experiments the impact of this caching was insignificant (single digit %) and
357- * not worth the potential issues with stale cache records.
358- */
359339 getScriptSnapshot ( tsFilePath : string ) : ts . IScriptSnapshot {
360- let absoluteTsFilePath ;
361-
362- if ( tsFilePath == this . defaultLibFilePath || path . isAbsolute ( tsFilePath ) ) {
363- absoluteTsFilePath = tsFilePath ;
364- } else if ( this . compilerOptions . moduleResolution === ts . ModuleResolutionKind . NodeJs &&
365- tsFilePath . match ( / ^ n o d e _ m o d u l e s / ) ) {
366- absoluteTsFilePath = path . resolve ( tsFilePath ) ;
367- } else if ( tsFilePath . match ( / ^ r x j s / ) ) {
368- absoluteTsFilePath = path . resolve ( 'node_modules' , tsFilePath ) ;
369- } else if ( tsFilePath . match ( / ^ n o d e _ m o d u l e s / ) ) {
370- absoluteTsFilePath = path . resolve ( 'node_modules/../' , tsFilePath ) ;
371- } else {
372- absoluteTsFilePath = path . join ( this . treeInputPath , tsFilePath ) ;
373- }
340+ // TypeScript seems to request lots of bogus paths during import path lookup and resolution,
341+ // so we we just return undefined when the path is not correct.
374342
343+ // Ensure it is in the input tree or a lib.d.ts file.
344+ if ( ! startsWith ( tsFilePath , this . treeInputPath ) && ! tsFilePath . match ( / \/ l i b ( \. .* ) * .d \. t s $ / ) ) {
345+ if ( fs . existsSync ( tsFilePath ) ) {
346+ console . log ( 'Rejecting' , tsFilePath , '. File is not in the input tree.' ) ;
347+ }
348+ return undefined ;
349+ }
375350
376- if ( ! fs . existsSync ( absoluteTsFilePath ) ) {
377- // TypeScript seems to request lots of bogus paths during import path lookup and resolution,
378- // so we we just return undefined when the path is not correct.
351+ // Ensure it exists
352+ if ( ! fs . existsSync ( tsFilePath ) ) {
379353 return undefined ;
380354 }
381- return ts . ScriptSnapshot . fromString ( fs . readFileSync ( absoluteTsFilePath , FS_OPTS ) ) ;
355+
356+ return ts . ScriptSnapshot . fromString ( fs . readFileSync ( tsFilePath , FS_OPTS ) ) ;
382357 }
383358
384359
@@ -402,6 +377,10 @@ function clone<T>(object: T): T {
402377 return < T > result ;
403378}
404379
380+ function startsWith ( str : string , substring : string ) : boolean {
381+ return str . substring ( 0 , substring . length ) === substring ;
382+ }
383+
405384function endsWith ( str : string , substring : string ) : boolean {
406385 return str . indexOf ( substring , str . length - substring . length ) !== - 1 ;
407386}
0 commit comments