Skip to content

Commit 929c9b1

Browse files
committed
fixes comilation with sync fs based request
1 parent 46662df commit 929c9b1

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ts-node-dev
22

3-
> Tweacked 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

lib/child-require-hook.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var fs = require('fs')
22
var getCompiledPath = require('./get-compiled-path')
33
var sep = require('path').sep
44

5+
var compilationId
56
var timeThreshold = 10000
67
var allowJs = false
78
var compiledDir
@@ -15,6 +16,8 @@ var compile = (code, fileName) => {
1516
compile: fileName,
1617
compiledPath: compiledPath
1718
})
19+
var compileRequestFile = [compiledDir, compilationId + '.req'].join(sep)
20+
fs.writeFileSync(compileRequestFile, [fileName, compiledPath].join('\n'))
1821
var compiled
1922
var start = new Date().getTime()
2023
var passed

lib/compiler.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ var comilationInstanceStampt = new Date().getTime()
2020
var compiler = {
2121
allowJs: false,
2222
tsConfigPath: '',
23+
getCompilationId: function () {
24+
return comilationInstanceStampt
25+
},
2326
getCompiledDir: function () {
2427
return path.join(tmpDir, 'compiled').replace(/\\/g, '/')
2528
},
29+
getCompileReqFilePath: function () {
30+
return path.join(compiler.getCompiledDir(), compiler.getCompilationId() + '.req')
31+
},
2632
getCompilerReadyFilePath: function () {
2733
return path.join(os.tmpdir(), 'ts-node-dev-ready-' + comilationInstanceStampt)
2834
.replace(/\\/g, '/')
@@ -54,6 +60,7 @@ var compiler = {
5460
.map(ignore => 'new RegExp("' + ignore + '")').join(', ') + ']'
5561
fileData = fileData.replace('var ignore = [/node_modules/]', 'var ignore = ' + ignoreVal)
5662
}
63+
fileData = fileData.replace('var compilationId', 'var compilationId = "' + compiler.getCompilationId() + '"')
5764
fileData = fileData.replace('var compiledDir', 'var compiledDir = "' + compiler.getCompiledDir() + '"')
5865
fileData = fileData.replace('./get-compiled-path', path.join(__dirname, 'get-compiled-path').replace(/\\/g, '/'))
5966
fileData = fileData.replace('var readyFile',

lib/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var fork = require('child_process').fork;
22
var filewatcher = require('filewatcher');
33
var ipc = require('./ipc');
44
var resolveMain = require('./resolveMain');
5-
var compiler = require('./compiler')
5+
var compiler = require('./compiler');
6+
var fs = require('fs');
67

78
module.exports = function (script, scriptArgs, nodeArgs, opts) {
89
compiler.init(opts)
@@ -71,7 +72,33 @@ module.exports = function (script, scriptArgs, nodeArgs, opts) {
7172
env: process.env
7273
});
7374

74-
child.on('message', compiler.compile);
75+
var compileReqWatcher = filewatcher({ forcePolling: opts.poll });
76+
var currentCompilePath
77+
fs.writeFileSync(compiler.getCompileReqFilePath(), '');
78+
compileReqWatcher.add(compiler.getCompileReqFilePath());
79+
compileReqWatcher.on('change', function (file) {
80+
fs.readFile(file, 'utf-8', function (err, data) {
81+
var split = data.split('\n')
82+
var compile = split[0]
83+
var compiledPath = split[1]
84+
if (currentCompilePath == compiledPath) return
85+
currentCompilePath = compiledPath
86+
// console.log('compileReqWatcher file change', compile);
87+
if (compiledPath) {
88+
compiler.compile({
89+
compile: compile,
90+
compiledPath: compiledPath
91+
})
92+
}
93+
})
94+
})
95+
child.on('message', function (message) {
96+
if (currentCompilePath == message.compiledPath) return
97+
currentCompilePath = message.compiledPath
98+
// console.log('message comile', message.compile);
99+
compiler.compile(message)
100+
});
101+
75102
child.on('exit', function (code) {
76103
if (!child.respawn) process.exit(code);
77104
child = undefined;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-node-dev",
3-
"version": "1.0.0-pre.15",
3+
"version": "1.0.0-pre.16",
44
"description": "Compiles your TS app and restarts when files are modified.",
55
"keywords": [
66
"restart",

0 commit comments

Comments
 (0)