Skip to content

Commit e2278de

Browse files
committed
add notice about --transpileOnly
1 parent 4e00c34 commit e2278de

File tree

9 files changed

+216
-158
lines changed

9 files changed

+216
-158
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.DS_Store
22
.vagrant
33
node_modules
4-
npm-debug.log
4+
*.log
55
.ts-node

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# ts-node-dev
1+
# ts-node-dev
22

3-
> Tweaked version of [node-dev](https://github.com/fgnass/node-dev) that uses [ts-node](https://github.com/TypeStrong/ts-node) under the hood.
3+
> Tweaked version of [node-dev](https://github.com/fgnass/node-dev) that uses [ts-node](https://github.com/TypeStrong/ts-node) under the hood.
44
55
It restarts target node process when any of required files changes (as standard `node-dev`) but shares [Typescript](https://github.com/Microsoft/TypeScript/) compilation process between restarts. This significantly increases speed of restarting comparing to `node-dev -r ts-node/register ...`, `nodemon -x ts-node ...` variations because there is no need to instantiate `ts-node` compilation each time.
66

@@ -23,6 +23,7 @@ ts-node-dev [node-dev|ts-node flags] [ts-node-dev flags] [node cli flags] [--] [
2323
```
2424

2525
So you just combine [node-dev](https://github.com/fgnass/node-dev) and [ts-node](https://github.com/TypeStrong/ts-node) options (see docs of those packages):
26+
2627
```
2728
ts-node-dev --respawn --transpileOnly server.ts
2829
```
@@ -36,12 +37,14 @@ tsnd --respawn server.ts
3637
**Also there are additional options specific to `ts-node-dev`:**
3738

3839
- `--prefer-ts` (default: false) - for each `.js` file (that is not in `node_modules`) will try to check if corresponding `.ts` version exists and require it.
39-
- `--ignore-watch` (default: []) - files/folders to be [ignored by `node-dev`](https://github.com/fgnass/node-dev#ignore-paths). **But also this behaviour enhanced:** it will also make up `new RegExp` of passed ignore string and check absolute paths of required files for match.
40-
So, to ignore everthing in `node_modules`, just pass `--ignore-watch node_modules`.
40+
- `--ignore-watch` (default: []) - files/folders to be [ignored by `node-dev`](https://github.com/fgnass/node-dev#ignore-paths). **But also this behaviour enhanced:** it will also make up `new RegExp` of passed ignore string and check absolute paths of required files for match.
41+
So, to ignore everthing in `node_modules`, just pass `--ignore-watch node_modules`.
4142

4243
- `--debug` - some additional debug output.
4344

44-
**And some points to notice:**
45+
**Caveats and points of notice:**
46+
47+
- Especially for large code bases always consider running with `--transpileOnly` flag which is normal for dev workflow and will speed up things greatly. Note, that `ts-node-dev` will not put watch handlers on TS files that contain only types/interfaces (used only for type checking) - this is current limitation by design.
4548

4649
- `--ignore-watch` will NOT affect files ignored by TS compilation. Use `--ignore` option (or `TS_NODE_IGNORE` env variable) to pass **RegExp strings** for filtering files that should not be compiled, by default `/node_modules/` are ignored.
4750

lib/child-require-hook.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ var execCheck = false
1414

1515
var checkFileScript = join(__dirname, 'check-file-exists.js')
1616

17-
var waitForFile = function (fileName) {
17+
var waitForFile = function(fileName) {
1818
var start = new Date().getTime()
1919
while (true) {
2020
const exists = execCheck
21-
? execSync(['node', checkFileScript, '"' + fileName + '"'].join(' '), { stdio: 'inherit' })
21+
? execSync(['node', checkFileScript, '"' + fileName + '"'].join(' '), {
22+
stdio: 'inherit'
23+
}) || true
2224
: fs.existsSync(fileName)
25+
2326
if (exists) {
2427
return
2528
}
26-
var passed = (new Date().getTime() - start)
29+
var passed = new Date().getTime() - start
2730
if (timeThreshold && passed > timeThreshold) {
28-
throw new Error(
29-
'Could not require ' + fileName
30-
)
31+
throw new Error('Could not require ' + fileName)
3132
}
3233
}
3334
}
@@ -46,11 +47,11 @@ var compile = (code, fileName) => {
4647
}
4748

4849
function registerExtensions(extensions) {
49-
extensions.forEach(function (ext) {
50+
extensions.forEach(function(ext) {
5051
var old = require.extensions[ext] || require.extensions['.js']
51-
require.extensions[ext] = function (m, fileName) {
52+
require.extensions[ext] = function(m, fileName) {
5253
var _compile = m._compile
53-
m._compile = function (code, fileName) {
54+
m._compile = function(code, fileName) {
5455
return _compile.call(this, compile(code, fileName), fileName)
5556
}
5657
return old(m, fileName)
@@ -65,7 +66,7 @@ function isFileInNodeModules(fileName) {
6566
function registerJsExtension() {
6667
var old = require.extensions['.js']
6768
if (allowJs || preferTs) {
68-
require.extensions['.js'] = function (m, fileName) {
69+
require.extensions['.js'] = function(m, fileName) {
6970
var tsCode
7071
var tsFileName
7172
if (preferTs && !isFileInNodeModules(fileName)) {
@@ -75,11 +76,13 @@ function registerJsExtension() {
7576
}
7677
}
7778
var _compile = m._compile
78-
var isIgnored = ignore && ignore.reduce(function (res, ignore) {
79-
return res || ignore.test(fileName)
80-
}, false)
79+
var isIgnored =
80+
ignore &&
81+
ignore.reduce(function(res, ignore) {
82+
return res || ignore.test(fileName)
83+
}, false)
8184
if (tsCode !== undefined || (allowJs && !isIgnored)) {
82-
m._compile = function (code, fileName) {
85+
m._compile = function(code, fileName) {
8386
if (tsCode !== undefined) {
8487
code = tsCode
8588
fileName = tsFileName
@@ -104,4 +107,4 @@ if (readyFile) {
104107
}
105108
}
106109

107-
module.exports.registerExtensions = registerExtensions
110+
module.exports.registerExtensions = registerExtensions

lib/compiler.js

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,46 @@ var getCompiledPath = require('./get-compiled-path')
1313
var tmpDir = '.ts-node'
1414

1515
var extensions = ['.ts', '.tsx']
16-
var empty = function () { }
16+
var empty = function() {}
1717
var cwd = process.cwd()
18-
var comilationInstanceStampt = Math.random().toString().slice(2)
18+
var comilationInstanceStampt = Math.random()
19+
.toString()
20+
.slice(2)
1921

2022
var compiler = {
2123
allowJs: false,
2224
tsConfigPath: '',
23-
getCompilationId: function () {
25+
getCompilationId: function() {
2426
return comilationInstanceStampt
2527
},
26-
getCompiledDir: function () {
28+
getCompiledDir: function() {
2729
return path.join(tmpDir, 'compiled').replace(/\\/g, '/')
2830
},
29-
getCompileReqFilePath: function () {
30-
return path.join(compiler.getCompiledDir(), compiler.getCompilationId() + '.req')
31+
getCompileReqFilePath: function() {
32+
return path.join(
33+
compiler.getCompiledDir(),
34+
compiler.getCompilationId() + '.req'
35+
)
3136
},
32-
getCompilerReadyFilePath: function () {
33-
return path.join(os.tmpdir(), 'ts-node-dev-ready-' + comilationInstanceStampt)
37+
getCompilerReadyFilePath: function() {
38+
return path
39+
.join(os.tmpdir(), 'ts-node-dev-ready-' + comilationInstanceStampt)
3440
.replace(/\\/g, '/')
3541
},
36-
getChildHookPath: function () {
37-
return path.join(os.tmpdir(), 'ts-node-dev-hook-' + comilationInstanceStampt + '.js')
42+
getChildHookPath: function() {
43+
return path
44+
.join(os.tmpdir(), 'ts-node-dev-hook-' + comilationInstanceStampt + '.js')
3845
.replace(/\\/g, '/')
3946
},
40-
writeReadyFile: function () {
47+
writeReadyFile: function() {
4148
var fileData = fs.writeFileSync(compiler.getCompilerReadyFilePath(), '')
4249
},
43-
writeChildHookFile: function (options) {
44-
var fileData = fs.readFileSync(path.join(__dirname, 'child-require-hook.js'), 'utf-8')
45-
var compileTimeout = parseInt(options['compile-timeout'])
50+
writeChildHookFile: function(options) {
51+
var fileData = fs.readFileSync(
52+
path.join(__dirname, 'child-require-hook.js'),
53+
'utf-8'
54+
)
55+
var compileTimeout = parseInt(options['compile-timeout'])
4656
if (compileTimeout) {
4757
fileData = fileData.replace('10000', compileTimeout.toString())
4858
}
@@ -57,23 +67,46 @@ var compiler = {
5767
}
5868
if (options['ignore'] !== undefined) {
5969
var ignore = options['ignore']
60-
var ignoreVal = !ignore || ignore === 'false'
61-
? 'false'
62-
: '[' + (Array.isArray(ignore) ? ignore : ignore.split(/, /))
63-
.map(ignore => 'new RegExp("' + ignore + '")').join(', ') + ']'
64-
fileData = fileData.replace('var ignore = [/node_modules/]', 'var ignore = ' + ignoreVal)
70+
var ignoreVal =
71+
!ignore || ignore === 'false'
72+
? 'false'
73+
: '[' +
74+
(Array.isArray(ignore) ? ignore : ignore.split(/, /))
75+
.map(ignore => 'new RegExp("' + ignore + '")')
76+
.join(', ') +
77+
']'
78+
fileData = fileData.replace(
79+
'var ignore = [/node_modules/]',
80+
'var ignore = ' + ignoreVal
81+
)
6582
}
66-
fileData = fileData.replace('var compilationId', 'var compilationId = "' + compiler.getCompilationId() + '"')
67-
fileData = fileData.replace('var compiledDir', 'var compiledDir = "' + compiler.getCompiledDir() + '"')
68-
fileData = fileData.replace('./get-compiled-path', path.join(__dirname, 'get-compiled-path').replace(/\\/g, '/'))
69-
fileData = fileData.replace('var readyFile',
70-
'var readyFile = "' + compiler.getCompilerReadyFilePath() + '"')
71-
fileData = fileData.replace(/__dirname/, '"' + __dirname.replace(/\\/g, '/') + '"')
83+
fileData = fileData.replace(
84+
'var compilationId',
85+
'var compilationId = "' + compiler.getCompilationId() + '"'
86+
)
87+
fileData = fileData.replace(
88+
'var compiledDir',
89+
'var compiledDir = "' + compiler.getCompiledDir() + '"'
90+
)
91+
fileData = fileData.replace(
92+
'./get-compiled-path',
93+
path.join(__dirname, 'get-compiled-path').replace(/\\/g, '/')
94+
)
95+
fileData = fileData.replace(
96+
'var readyFile',
97+
'var readyFile = "' + compiler.getCompilerReadyFilePath() + '"'
98+
)
99+
fileData = fileData.replace(
100+
/__dirname/,
101+
'"' + __dirname.replace(/\\/g, '/') + '"'
102+
)
72103
fs.writeFileSync(compiler.getChildHookPath(), fileData)
73104
},
74-
init: function (options) {
105+
init: function(options) {
75106
var project = options['project']
76-
compiler.tsConfigPath = resolveSync(cwd, typeof project === 'string' ? project : undefined) || ''
107+
compiler.log = options.log
108+
compiler.tsConfigPath =
109+
resolveSync(cwd, typeof project === 'string' ? project : undefined) || ''
77110

78111
var originalJsHandler = require.extensions['.js']
79112
require.extensions['.ts'] = empty
@@ -86,7 +119,10 @@ var compiler = {
86119
try {
87120
compilerOptions = JSON.parse(options['compilerOptions'])
88121
} catch (e) {
89-
console.log('Could not parse compilerOptions', options['compilerOptions'])
122+
console.log(
123+
'Could not parse compilerOptions',
124+
options['compilerOptions']
125+
)
90126
console.log(e)
91127
}
92128
}
@@ -129,7 +165,7 @@ var compiler = {
129165
tsHandler = require.extensions['.ts']
130166
compiler.writeChildHookFile(options)
131167
},
132-
compileChanged: function (fileName) {
168+
compileChanged: function(fileName) {
133169
var ext = path.extname(fileName)
134170
if (extensions.indexOf(ext) < 0) return
135171
try {
@@ -143,7 +179,7 @@ var compiler = {
143179
console.error(e)
144180
}
145181
},
146-
compile: function (params) {
182+
compile: function(params) {
147183
var fileName = params.compile
148184
var code = fs.readFileSync(fileName, 'utf-8')
149185
var compiledPath = params.compiledPath
@@ -154,23 +190,22 @@ var compiler = {
154190
if (fs.existsSync(compiledPath)) {
155191
return
156192
}
193+
var starTime = new Date().getTime()
157194
var m = {
158195
_compile: writeCompiled
159196
}
160-
tsHandler(m, fileName)
197+
tsHandler(m, fileName)
161198
try {
162199
m._compile(code, fileName)
200+
compiler.log.debug(
201+
fileName,
202+
'compiled in',
203+
new Date().getTime() - starTime,
204+
'ms'
205+
)
163206
} catch (e) {
164-
var splits = e.message.split('\n')
165-
var title = splits.shift()
166-
var message = splits.join('\n')
167-
// compiler.notify(title, '\n' + message, 'error')
168-
// compiler.stop()
169-
// setTimeout(function () {
170-
// compiler.stop()
171-
// })
172207
code = 'throw ' + 'new Error(' + JSON.stringify(e.message) + ')' + ';'
173-
writeCompiled(code)
208+
writeCompiled(code)
174209
}
175210
}
176211
}

lib/get-compiled-path.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ var path = require('path')
33
var cwd = process.cwd()
44

55
module.exports = (code, fileName, compiledDir) => {
6-
var hash = crypto.createHash('sha256')
7-
.update(fileName + code, 'utf8').digest('hex')
8-
fileName = path.relative(cwd, fileName)
9-
var hashed =
10-
fileName.replace(/[^\w]/g, '_') +
11-
'_' + hash +
12-
'.js'
6+
var hash = crypto
7+
.createHash('sha256')
8+
.update(fileName + code, 'utf8')
9+
.digest('hex')
10+
fileName = path.relative(cwd, fileName)
11+
var hashed = fileName.replace(/[^\w]/g, '_') + '_' + hash + '.js'
1312
return path.join(compiledDir, hashed)
14-
}
13+
}

0 commit comments

Comments
 (0)