Skip to content

Commit 49cddc0

Browse files
committed
compiler update
1 parent c0df64c commit 49cddc0

File tree

7 files changed

+119
-74
lines changed

7 files changed

+119
-74
lines changed

lib/compiler.js

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,54 @@ var tsHandler
1212
var getCompiledPath = require('./get-compiled-path')
1313
var tmpDir = '.ts-node'
1414

15-
var sourceMapSupportPath = require.resolve('source-map-support').replace(/\\/g, '/')
15+
var sourceMapSupportPath = require
16+
.resolve('source-map-support')
17+
.replace(/\\/g, '/')
1618

1719
var extensions = ['.ts', '.tsx']
18-
var empty = function() {}
20+
var empty = function () {}
1921
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 = {}
2327

2428
var compiler = {
2529
allowJs: false,
2630
tsConfigPath: '',
27-
getCompilationId: function() {
31+
getCompilationId: function () {
2832
return compilationInstanceStamp
29-
},
30-
createCompiledDir: function() {
33+
},
34+
createCompiledDir: function () {
3135
var compiledDir = compiler.getCompiledDir()
3236
if (!fs.existsSync(compiledDir)) {
3337
mkdirp.sync(compiler.getCompiledDir())
34-
}
38+
}
3539
},
36-
getCompiledDir: function() {
40+
getCompiledDir: function () {
3741
return path.join(tmpDir, 'compiled').replace(/\\/g, '/')
3842
},
39-
getCompileReqFilePath: function() {
43+
getCompileReqFilePath: function () {
4044
return path.join(
4145
compiler.getCompiledDir(),
4246
compiler.getCompilationId() + '.req'
4347
)
4448
},
45-
getCompilerReadyFilePath: function() {
49+
getCompilerReadyFilePath: function () {
4650
return path
4751
.join(os.tmpdir(), 'ts-node-dev-ready-' + compilationInstanceStamp)
4852
.replace(/\\/g, '/')
4953
},
50-
getChildHookPath: function() {
54+
getChildHookPath: function () {
5155
return path
5256
.join(os.tmpdir(), 'ts-node-dev-hook-' + compilationInstanceStamp + '.js')
5357
.replace(/\\/g, '/')
5458
},
55-
writeReadyFile: function() {
59+
writeReadyFile: function () {
5660
var fileData = fs.writeFileSync(compiler.getCompilerReadyFilePath(), '')
5761
},
58-
writeChildHookFile: function(options) {
59-
62+
writeChildHookFile: function (options) {
6063
var fileData = fs.readFileSync(
6164
path.join(__dirname, 'child-require-hook.js'),
6265
'utf-8'
@@ -73,8 +76,8 @@ var compiler = {
7376
}
7477
if (options['exec-check']) {
7578
fileData = fileData.replace('execCheck = false', 'execCheck = true')
76-
}
77-
79+
}
80+
7881
if (options['exit-child']) {
7982
fileData = fileData.replace('exitChild = false', 'exitChild = true')
8083
}
@@ -85,7 +88,7 @@ var compiler = {
8588
? 'false'
8689
: '[' +
8790
(Array.isArray(ignore) ? ignore : ignore.split(/, /))
88-
.map(ignore => 'new RegExp("' + ignore + '")')
91+
.map((ignore) => 'new RegExp("' + ignore + '")')
8992
.join(', ') +
9093
']'
9194
fileData = fileData.replace(
@@ -112,25 +115,48 @@ var compiler = {
112115
fileData = fileData.replace(
113116
'var sourceMapSupportPath',
114117
'var sourceMapSupportPath = "' + sourceMapSupportPath + '"'
115-
)
118+
)
116119
fileData = fileData.replace(
117120
/__dirname/,
118121
'"' + __dirname.replace(/\\/g, '/') + '"'
119122
)
120123
fs.writeFileSync(compiler.getChildHookPath(), fileData)
121124
},
122-
init: function(options) {
125+
init: function (options) {
126+
compiler.options = options
123127
var project = options['project']
124128
compiler.log = options.log
125129
compiler.tsConfigPath =
126130
resolveSync(cwd, typeof project === 'string' ? project : undefined) || ''
127131

128-
var originalJsHandler = require.extensions['.js']
132+
//var originalJsHandler = require.extensions['.js']
129133
require.extensions['.ts'] = empty
130134
require.extensions['.tsx'] = empty
131135
tmpDir = options['cache-directory']
132136
? path.resolve(options['cache-directory'])
133137
: 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+
134160
var compilerOptionsArg =
135161
options['compilerOptions'] || options['compiler-options']
136162
var compilerOptions
@@ -145,7 +171,6 @@ var compiler = {
145171
console.log(e)
146172
}
147173
}
148-
149174
let ignore = options['ignore'] || process.env['TS_NODE_IGNORE']
150175
if (ignore && typeof ignore === 'string') {
151176
ignore = [ignore]
@@ -171,45 +196,43 @@ var compiler = {
171196
options['ignoreDiagnostics'] || options['ignore-diagnostics'],
172197
logError: options['log-error'],
173198
disableWarnings: options['disableWarnings'],
174-
preferTsExts: options['prefer-ts-exts'],
199+
preferTsExts: options['prefer-ts-exts'],
175200
compilerOptions: compilerOptions,
176-
files: options['files'] || true
201+
files: options['files'] || true,
177202
}
178203
try {
179-
register(tsNodeOptions)
204+
// if (compiler.service && compiler.service.enable) {
205+
// compiler.service.enable(false)
206+
// }
207+
compiler.service = register(tsNodeOptions)
180208
} catch (e) {
181209
console.log(e)
182210
return
183211
}
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+
})
197216
},
198-
compileChanged: function(fileName) {
217+
compileChanged: function (fileName) {
199218
var ext = path.extname(fileName)
200219
if (extensions.indexOf(ext) < 0) return
201220
try {
202221
var code = fs.readFileSync(fileName, 'utf-8')
203222
compiler.compile({
204223
code: code,
205224
compile: fileName,
206-
compiledPath: getCompiledPath(code, fileName, compiler.getCompiledDir())
225+
compiledPath: getCompiledPath(
226+
code,
227+
fileName,
228+
compiler.getCompiledDir()
229+
),
207230
})
208231
} catch (e) {
209232
console.error(e)
210233
}
211234
},
212-
compile: function(params) {
235+
compile: function (params) {
213236
var fileName = params.compile
214237
var code = fs.readFileSync(fileName, 'utf-8')
215238
var compiledPath = params.compiledPath
@@ -222,10 +245,12 @@ var compiler = {
222245
}
223246
var starTime = new Date().getTime()
224247
var m = {
225-
_compile: writeCompiled
248+
_compile: writeCompiled,
226249
}
227-
tsHandler(m, fileName)
228250
try {
251+
var ext = path.extname(fileName)
252+
var extHandler = extHandlers[ext]
253+
extHandler(m, fileName)
229254
m._compile(code, fileName)
230255
compiler.log.debug(
231256
fileName,
@@ -234,10 +259,12 @@ var compiler = {
234259
'ms'
235260
)
236261
} catch (e) {
262+
console.log('Compilation error in', fileName)
237263
code = 'throw ' + 'new Error(' + JSON.stringify(e.message) + ')' + ';'
238264
writeCompiled(code)
265+
compiler.registerTsNode()
239266
}
240-
}
267+
},
241268
}
242269

243270
module.exports = compiler

lib/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
5454
if (cfg.deps) log.info('... or add `--no-deps` to use less file handles.')
5555
})
5656

57-
// Read for "rs" from command line
57+
// Read for "rs" from command line
5858
if (opts.rs !== false) {
5959
const rl = readline.createInterface({
6060
input: process.stdin,
@@ -67,7 +67,7 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
6767
restart('', true)
6868
}
6969
})
70-
}
70+
}
7171

7272
/**
7373
* Run the wrapped script.
@@ -187,7 +187,7 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
187187
compiler.init(opts)
188188
}
189189
/* eslint-disable no-octal-escape */
190-
if (cfg.clear) process.stdout.write('\033[2J\033[H')
190+
if (cfg.clear) process.stdout.write('\033[2J\033[H')
191191
if (isManualRestart === true) {
192192
notify('Restarting', 'manual restart from user')
193193
} else {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-node-dev",
3-
"version": "1.0.0-pre.43",
3+
"version": "1.0.0-pre.44",
44
"description": "Compiles your TS app and restarts when files are modified.",
55
"keywords": [
66
"restart",
@@ -52,7 +52,7 @@
5252
"resolve": "^1.0.0",
5353
"rimraf": "^2.6.1",
5454
"source-map-support": "^0.5.12",
55-
"tree-kill": "^1.2.1",
55+
"tree-kill": "^1.2.2",
5656
"ts-node": "*",
5757
"tsconfig": "^7.0.0"
5858
},
@@ -66,6 +66,6 @@
6666
"tap": "^5.2.0",
6767
"touch": "^1.0.0",
6868
"tsconfig-paths": "^3.3.1",
69-
"typescript": "^2.3.4"
69+
"typescript": "^3.7.4"
7070
}
7171
}

test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {fn} from './util'
2+
3+
console.log(fn(1))

test/ts/test-script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const obj: A = {
66
b: 2
77
}
88

9-
fn(1)
9+
fn(1))
1010
console.log('test', str)
1111

1212

util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
export const fn = (x: number) => {
3+
return x
4+
}

0 commit comments

Comments
 (0)