@@ -11,7 +11,6 @@ import * as TS from 'typescript'
11
11
import { loadSync } from 'tsconfig'
12
12
13
13
const pkg = require ( '../package.json' )
14
- const oldHandlers : { [ key : string ] : any } = { }
15
14
16
15
/**
17
16
* Common TypeScript interfaces between versions.
@@ -67,7 +66,6 @@ export interface Options {
67
66
ignoreWarnings ?: Array < number | string >
68
67
disableWarnings ?: boolean
69
68
getFile ?: ( fileName : string ) => string
70
- getVersion ?: ( fileName : string ) => string
71
69
fileExists ?: ( fileName : string ) => boolean
72
70
compilerOptions ?: any
73
71
}
@@ -76,10 +74,9 @@ export interface Options {
76
74
* Track the project information.
77
75
*/
78
76
interface Project {
79
- files : { [ fileName : string ] : boolean }
80
- versions : { [ fileName : string ] : string }
81
- maps : { [ fileName : string ] : string }
82
- version : number
77
+ cache : { [ fileName : string ] : string }
78
+ versions : { [ fileName : string ] : number }
79
+ sourceMaps : { [ fileName : string ] : string }
83
80
}
84
81
85
82
/**
@@ -95,7 +92,6 @@ export interface TypeInfo {
95
92
*/
96
93
const DEFAULT_OPTIONS : Options = {
97
94
getFile,
98
- getVersion,
99
95
fileExists,
100
96
cache : process . env . TS_NODE_CACHE ,
101
97
cacheDirectory : process . env . TS_NODE_CACHE_DIRECTORY || join ( tmpdir ( ) , 'ts-node' ) ,
@@ -109,7 +105,7 @@ const DEFAULT_OPTIONS: Options = {
109
105
110
106
export interface Register {
111
107
cwd : string
112
- compile ( fileName : string ) : string
108
+ compile ( code : string , fileName : string ) : string
113
109
getTypeInfo ( fileName : string , position : number ) : TypeInfo
114
110
}
115
111
@@ -130,16 +126,16 @@ export function register (opts?: Options): () => Register {
130
126
options . compilerOptions
131
127
132
128
function load ( ) {
133
- const project : Project = { version : 0 , files : { } , versions : { } , maps : { } }
129
+ const project : Project = { cache : { } , versions : { } , sourceMaps : { } }
134
130
135
131
// Install source map support and read from cache.
136
132
sourceMapSupport . install ( {
137
133
environment : 'node' ,
138
134
retrieveSourceMap ( fileName : string ) {
139
- if ( project . maps [ fileName ] ) {
135
+ if ( project . sourceMaps [ fileName ] ) {
140
136
return {
141
- url : project . maps [ fileName ] ,
142
- map : options . getFile ( project . maps [ fileName ] )
137
+ url : project . sourceMaps [ fileName ] ,
138
+ map : options . getFile ( project . sourceMaps [ fileName ] )
143
139
}
144
140
}
145
141
}
@@ -167,14 +163,16 @@ export function register (opts?: Options): () => Register {
167
163
168
164
// Add all files into the file hash.
169
165
for ( const fileName of config . fileNames ) {
170
- project . files [ fileName ] = true
166
+ if ( / \. d \. t s $ / . test ( fileName ) ) {
167
+ project . versions [ fileName ] = 1
168
+ }
171
169
}
172
170
173
171
/**
174
172
* Create the basic required function using transpile mode.
175
173
*/
176
- let getOutput = function ( fileName : string , contents : string ) : SourceOutput {
177
- const result = ts . transpileModule ( contents , {
174
+ let getOutput = function ( code : string , fileName : string ) : SourceOutput {
175
+ const result = ts . transpileModule ( code , {
178
176
fileName,
179
177
compilerOptions : config . options ,
180
178
reportDiagnostics : true
@@ -199,16 +197,33 @@ export function register (opts?: Options): () => Register {
199
197
200
198
// Use full language services when the fast option is disabled.
201
199
if ( ! options . fast ) {
200
+ // Add the file to the project.
201
+ const addVersion = function ( fileName : string ) {
202
+ if ( ! project . versions . hasOwnProperty ( fileName ) ) {
203
+ project . versions [ fileName ] = 1
204
+ }
205
+ }
206
+
207
+ // Set the file contents into cache.
208
+ const addCache = function ( code : string , fileName : string ) {
209
+ project . cache [ fileName ] = code
210
+ project . versions [ fileName ] += 1
211
+ }
212
+
213
+ // Create the compiler host for type checking.
202
214
const serviceHost = {
203
- getScriptFileNames : ( ) => Object . keys ( project . files ) ,
204
- getProjectVersion : ( ) => String ( project . version ) ,
205
- getScriptVersion : ( fileName : string ) => versionFile ( fileName ) ,
215
+ getScriptFileNames : ( ) => Object . keys ( project . versions ) ,
216
+ getScriptVersion : ( fileName : string ) => String ( project . versions [ fileName ] ) ,
206
217
getScriptSnapshot ( fileName : string ) {
207
- if ( ! options . fileExists ( fileName ) ) {
208
- return undefined
218
+ if ( ! project . cache . hasOwnProperty ( fileName ) ) {
219
+ if ( ! options . fileExists ( fileName ) ) {
220
+ return undefined
221
+ }
222
+
223
+ project . cache [ fileName ] = options . getFile ( fileName )
209
224
}
210
225
211
- return ts . ScriptSnapshot . fromString ( options . getFile ( fileName ) )
226
+ return ts . ScriptSnapshot . fromString ( project . cache [ fileName ] )
212
227
} ,
213
228
getDirectories : getDirectories ,
214
229
directoryExists : directoryExists ,
@@ -220,28 +235,7 @@ export function register (opts?: Options): () => Register {
220
235
221
236
const service = ts . createLanguageService ( serviceHost )
222
237
223
- const addAndVersionFile = function ( fileName : string ) {
224
- // Add files to the hash before compilation.
225
- project . files [ fileName ] = true
226
-
227
- const currentVersion = project . versions [ fileName ]
228
- const newVersion = versionFile ( fileName )
229
-
230
- // Increment the project version for file changes.
231
- if ( currentVersion !== newVersion ) {
232
- project . version ++
233
- }
234
-
235
- return newVersion
236
- }
237
-
238
- const versionFile = function ( fileName : string ) {
239
- const version = options . getVersion ( fileName )
240
- project . versions [ fileName ] = version
241
- return version
242
- }
243
-
244
- getOutput = function ( fileName : string ) {
238
+ getOutput = function ( code : string , fileName : string ) {
245
239
const output = service . getEmitOutput ( fileName )
246
240
247
241
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
@@ -262,14 +256,15 @@ export function register (opts?: Options): () => Register {
262
256
return [ output . outputFiles [ 1 ] . text , output . outputFiles [ 0 ] . text ]
263
257
}
264
258
265
- compile = readThrough ( cachedir , options , project , function ( fileName : string , contents : string ) {
266
- addAndVersionFile ( fileName )
259
+ compile = readThrough ( cachedir , options , project , function ( code : string , fileName : string ) {
260
+ addVersion ( fileName )
261
+ addCache ( code , fileName )
267
262
268
- return getOutput ( fileName , contents )
263
+ return getOutput ( code , fileName )
269
264
} )
270
265
271
266
getTypeInfo = function ( fileName : string , position : number ) {
272
- addAndVersionFile ( fileName )
267
+ addVersion ( fileName )
273
268
274
269
const info = service . getQuickInfoAtPosition ( fileName , position )
275
270
const name = ts . displayPartsToString ( info ? info . displayParts : [ ] )
@@ -286,25 +281,25 @@ export function register (opts?: Options): () => Register {
286
281
return result || ( result = load ( ) )
287
282
}
288
283
289
- function loader ( m : any , fileName : string ) {
290
- return m . _compile ( service ( ) . compile ( fileName ) , fileName )
291
- }
292
-
293
284
function shouldIgnore ( filename : string ) {
294
285
return relative ( service ( ) . cwd , filename ) . split ( sep ) . indexOf ( 'node_modules' ) > - 1
295
286
}
296
287
297
288
function registerExtension ( ext : string ) {
298
- const old = oldHandlers [ ext ] || oldHandlers [ '.js' ] || require . extensions [ '.js' ]
299
-
300
- oldHandlers [ ext ] = require . extensions [ ext ]
289
+ const old = require . extensions [ ext ] || require . extensions [ '.js' ]
301
290
302
291
require . extensions [ ext ] = function ( m : any , filename : string ) {
303
292
if ( shouldIgnore ( filename ) ) {
304
293
return old ( m , filename )
305
294
}
306
295
307
- return loader ( m , filename )
296
+ const _compile = m . _compile
297
+
298
+ m . _compile = function ( code : string , fileName : string ) {
299
+ return _compile . call ( this , service ( ) . compile ( code , fileName ) , fileName )
300
+ }
301
+
302
+ return old ( m , filename )
308
303
}
309
304
}
310
305
@@ -369,16 +364,15 @@ function readThrough (
369
364
cachedir : string ,
370
365
options : Options ,
371
366
project : Project ,
372
- compile : ( fileName : string , contents : string ) => SourceOutput
367
+ compile : ( code : string , fileName : string ) => SourceOutput
373
368
) {
374
369
if ( options . cache === false ) {
375
- return function ( fileName : string ) {
376
- const contents = options . getFile ( fileName )
377
- const cachePath = join ( cachedir , getCacheName ( contents , fileName ) )
370
+ return function ( code : string , fileName : string ) {
371
+ const cachePath = join ( cachedir , getCacheName ( code , fileName ) )
378
372
const sourceMapPath = `${ cachePath } .js.map`
379
- const out = compile ( fileName , contents )
373
+ const out = compile ( code , fileName )
380
374
381
- project . maps [ fileName ] = sourceMapPath
375
+ project . sourceMaps [ fileName ] = sourceMapPath
382
376
383
377
const output = updateOutput ( out [ 0 ] , fileName , sourceMapPath )
384
378
const sourceMap = updateSourceMap ( out [ 1 ] , fileName )
@@ -389,20 +383,19 @@ function readThrough (
389
383
}
390
384
}
391
385
392
- return function ( fileName : string ) {
393
- const contents = options . getFile ( fileName )
394
- const cachePath = join ( cachedir , getCacheName ( contents , fileName ) )
386
+ return function ( code : string , fileName : string ) {
387
+ const cachePath = join ( cachedir , getCacheName ( code , fileName ) )
395
388
const outputPath = `${ cachePath } .js`
396
389
const sourceMapPath = `${ cachePath } .js.map`
397
390
398
- project . maps [ fileName ] = sourceMapPath
391
+ project . sourceMaps [ fileName ] = sourceMapPath
399
392
400
393
// Use the cache when available.
401
394
if ( options . fileExists ( outputPath ) ) {
402
395
return options . getFile ( outputPath )
403
396
}
404
397
405
- const out = compile ( fileName , contents )
398
+ const out = compile ( code , fileName )
406
399
407
400
const output = updateOutput ( out [ 0 ] , fileName , sourceMapPath )
408
401
const sourceMap = updateSourceMap ( out [ 1 ] , fileName )
@@ -465,13 +458,6 @@ function getCompilerDigest (ts: TSCommon, options: Options, config: any) {
465
458
)
466
459
}
467
460
468
- /**
469
- * Get the file version using the mod time.
470
- */
471
- export function getVersion ( fileName : string ) : string {
472
- return String ( statSync ( fileName ) . mtime . getTime ( ) )
473
- }
474
-
475
461
/**
476
462
* Check if the file exists.
477
463
*/
0 commit comments