@@ -12,51 +12,54 @@ var tsHandler
12
12
var getCompiledPath = require ( './get-compiled-path' )
13
13
var tmpDir = '.ts-node'
14
14
15
- var sourceMapSupportPath = require . resolve ( 'source-map-support' ) . replace ( / \\ / g, '/' )
15
+ var sourceMapSupportPath = require
16
+ . resolve ( 'source-map-support' )
17
+ . replace ( / \\ / g, '/' )
16
18
17
19
var extensions = [ '.ts' , '.tsx' ]
18
- var empty = function ( ) { }
20
+ var empty = function ( ) { }
19
21
var cwd = process . cwd ( )
20
- var compilationInstanceStamp = Math . random ( )
21
- . toString ( )
22
- . slice ( 2 )
22
+ var compilationInstanceStamp = Math . random ( ) . toString ( ) . slice ( 2 )
23
+
24
+ var originalJsHandler = require . extensions [ '.js' ]
25
+
26
+ var extHandlers = { }
23
27
24
28
var compiler = {
25
29
allowJs : false ,
26
30
tsConfigPath : '' ,
27
- getCompilationId : function ( ) {
31
+ getCompilationId : function ( ) {
28
32
return compilationInstanceStamp
29
- } ,
30
- createCompiledDir : function ( ) {
33
+ } ,
34
+ createCompiledDir : function ( ) {
31
35
var compiledDir = compiler . getCompiledDir ( )
32
36
if ( ! fs . existsSync ( compiledDir ) ) {
33
37
mkdirp . sync ( compiler . getCompiledDir ( ) )
34
- }
38
+ }
35
39
} ,
36
- getCompiledDir : function ( ) {
40
+ getCompiledDir : function ( ) {
37
41
return path . join ( tmpDir , 'compiled' ) . replace ( / \\ / g, '/' )
38
42
} ,
39
- getCompileReqFilePath : function ( ) {
43
+ getCompileReqFilePath : function ( ) {
40
44
return path . join (
41
45
compiler . getCompiledDir ( ) ,
42
46
compiler . getCompilationId ( ) + '.req'
43
47
)
44
48
} ,
45
- getCompilerReadyFilePath : function ( ) {
49
+ getCompilerReadyFilePath : function ( ) {
46
50
return path
47
51
. join ( os . tmpdir ( ) , 'ts-node-dev-ready-' + compilationInstanceStamp )
48
52
. replace ( / \\ / g, '/' )
49
53
} ,
50
- getChildHookPath : function ( ) {
54
+ getChildHookPath : function ( ) {
51
55
return path
52
56
. join ( os . tmpdir ( ) , 'ts-node-dev-hook-' + compilationInstanceStamp + '.js' )
53
57
. replace ( / \\ / g, '/' )
54
58
} ,
55
- writeReadyFile : function ( ) {
59
+ writeReadyFile : function ( ) {
56
60
var fileData = fs . writeFileSync ( compiler . getCompilerReadyFilePath ( ) , '' )
57
61
} ,
58
- writeChildHookFile : function ( options ) {
59
-
62
+ writeChildHookFile : function ( options ) {
60
63
var fileData = fs . readFileSync (
61
64
path . join ( __dirname , 'child-require-hook.js' ) ,
62
65
'utf-8'
@@ -73,8 +76,8 @@ var compiler = {
73
76
}
74
77
if ( options [ 'exec-check' ] ) {
75
78
fileData = fileData . replace ( 'execCheck = false' , 'execCheck = true' )
76
- }
77
-
79
+ }
80
+
78
81
if ( options [ 'exit-child' ] ) {
79
82
fileData = fileData . replace ( 'exitChild = false' , 'exitChild = true' )
80
83
}
@@ -85,7 +88,7 @@ var compiler = {
85
88
? 'false'
86
89
: '[' +
87
90
( Array . isArray ( ignore ) ? ignore : ignore . split ( / , / ) )
88
- . map ( ignore => 'new RegExp("' + ignore + '")' )
91
+ . map ( ( ignore ) => 'new RegExp("' + ignore + '")' )
89
92
. join ( ', ' ) +
90
93
']'
91
94
fileData = fileData . replace (
@@ -112,25 +115,48 @@ var compiler = {
112
115
fileData = fileData . replace (
113
116
'var sourceMapSupportPath' ,
114
117
'var sourceMapSupportPath = "' + sourceMapSupportPath + '"'
115
- )
118
+ )
116
119
fileData = fileData . replace (
117
120
/ _ _ d i r n a m e / ,
118
121
'"' + __dirname . replace ( / \\ / g, '/' ) + '"'
119
122
)
120
123
fs . writeFileSync ( compiler . getChildHookPath ( ) , fileData )
121
124
} ,
122
- init : function ( options ) {
125
+ init : function ( options ) {
126
+ compiler . options = options
123
127
var project = options [ 'project' ]
124
128
compiler . log = options . log
125
129
compiler . tsConfigPath =
126
130
resolveSync ( cwd , typeof project === 'string' ? project : undefined ) || ''
127
131
128
- var originalJsHandler = require . extensions [ '.js' ]
132
+ // var originalJsHandler = require.extensions['.js']
129
133
require . extensions [ '.ts' ] = empty
130
134
require . extensions [ '.tsx' ] = empty
131
135
tmpDir = options [ 'cache-directory' ]
132
136
? path . resolve ( options [ 'cache-directory' ] )
133
137
: fs . mkdtempSync ( path . join ( os . tmpdir ( ) , '.ts-node' ) )
138
+
139
+ compiler . registerTsNode ( )
140
+
141
+ /* clean up compiled on each new init*/
142
+ rimraf . sync ( compiler . getCompiledDir ( ) )
143
+ compiler . createCompiledDir ( )
144
+ /* check if `allowJs` compiler option enable */
145
+ var allowJsEnabled = require . extensions [ '.js' ] !== originalJsHandler
146
+ if ( allowJsEnabled ) {
147
+ compiler . allowJs = true
148
+ require . extensions [ '.js' ] = originalJsHandler
149
+ extensions . push ( '.js' )
150
+ }
151
+
152
+ compiler . writeChildHookFile ( options )
153
+ } ,
154
+ registerTsNode : function ( ) {
155
+ var options = compiler . options
156
+ extensions . forEach ( function ( ext ) {
157
+ require . extensions [ ext ] = originalJsHandler
158
+ } )
159
+
134
160
var compilerOptionsArg =
135
161
options [ 'compilerOptions' ] || options [ 'compiler-options' ]
136
162
var compilerOptions
@@ -145,7 +171,6 @@ var compiler = {
145
171
console . log ( e )
146
172
}
147
173
}
148
-
149
174
let ignore = options [ 'ignore' ] || process . env [ 'TS_NODE_IGNORE' ]
150
175
if ( ignore && typeof ignore === 'string' ) {
151
176
ignore = [ ignore ]
@@ -171,45 +196,43 @@ var compiler = {
171
196
options [ 'ignoreDiagnostics' ] || options [ 'ignore-diagnostics' ] ,
172
197
logError : options [ 'log-error' ] ,
173
198
disableWarnings : options [ 'disableWarnings' ] ,
174
- preferTsExts : options [ 'prefer-ts-exts' ] ,
199
+ preferTsExts : options [ 'prefer-ts-exts' ] ,
175
200
compilerOptions : compilerOptions ,
176
- files : options [ 'files' ] || true
201
+ files : options [ 'files' ] || true ,
177
202
}
178
203
try {
179
- register ( tsNodeOptions )
204
+ // if (compiler.service && compiler.service.enable) {
205
+ // compiler.service.enable(false)
206
+ // }
207
+ compiler . service = register ( tsNodeOptions )
180
208
} catch ( e ) {
181
209
console . log ( e )
182
210
return
183
211
}
184
-
185
- /* clean up compiled on each new init*/
186
- rimraf . sync ( compiler . getCompiledDir ( ) )
187
- compiler . createCompiledDir ( )
188
- /* check if `allowJs` compiler option enable */
189
- var allowJsEnabled = require . extensions [ '.js' ] !== originalJsHandler
190
- if ( allowJsEnabled ) {
191
- compiler . allowJs = true
192
- require . extensions [ '.js' ] = originalJsHandler
193
- extensions . push ( '.js' )
194
- }
195
- tsHandler = require . extensions [ '.ts' ]
196
- compiler . writeChildHookFile ( options )
212
+ extensions . forEach ( function ( ext ) {
213
+ extHandlers [ ext ] = require . extensions [ ext ]
214
+ require . extensions [ ext ] = originalJsHandler
215
+ } )
197
216
} ,
198
- compileChanged : function ( fileName ) {
217
+ compileChanged : function ( fileName ) {
199
218
var ext = path . extname ( fileName )
200
219
if ( extensions . indexOf ( ext ) < 0 ) return
201
220
try {
202
221
var code = fs . readFileSync ( fileName , 'utf-8' )
203
222
compiler . compile ( {
204
223
code : code ,
205
224
compile : fileName ,
206
- compiledPath : getCompiledPath ( code , fileName , compiler . getCompiledDir ( ) )
225
+ compiledPath : getCompiledPath (
226
+ code ,
227
+ fileName ,
228
+ compiler . getCompiledDir ( )
229
+ ) ,
207
230
} )
208
231
} catch ( e ) {
209
232
console . error ( e )
210
233
}
211
234
} ,
212
- compile : function ( params ) {
235
+ compile : function ( params ) {
213
236
var fileName = params . compile
214
237
var code = fs . readFileSync ( fileName , 'utf-8' )
215
238
var compiledPath = params . compiledPath
@@ -222,10 +245,12 @@ var compiler = {
222
245
}
223
246
var starTime = new Date ( ) . getTime ( )
224
247
var m = {
225
- _compile : writeCompiled
248
+ _compile : writeCompiled ,
226
249
}
227
- tsHandler ( m , fileName )
228
250
try {
251
+ var ext = path . extname ( fileName )
252
+ var extHandler = extHandlers [ ext ]
253
+ extHandler ( m , fileName )
229
254
m . _compile ( code , fileName )
230
255
compiler . log . debug (
231
256
fileName ,
@@ -234,10 +259,12 @@ var compiler = {
234
259
'ms'
235
260
)
236
261
} catch ( e ) {
262
+ console . log ( 'Compilation error in' , fileName )
237
263
code = 'throw ' + 'new Error(' + JSON . stringify ( e . message ) + ')' + ';'
238
264
writeCompiled ( code )
265
+ compiler . registerTsNode ( )
239
266
}
240
- }
267
+ } ,
241
268
}
242
269
243
270
module . exports = compiler
0 commit comments