Skip to content

Commit 0410916

Browse files
committed
fix big files compilation
1 parent bc6c12c commit 0410916

File tree

8 files changed

+3690
-22
lines changed

8 files changed

+3690
-22
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ ts-node-dev --respawn server.ts
2929

3030
Also there is additional options specific to `ts-node-dev`:
3131

32-
- `--compile-timeout` (default: 10000 ms) - for how long to wait before report the error that something went wrong with compilation of a file.
3332
- `--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.
3433
- `--ignore-watch` (default: []) - files/folders to be [ignored by `node-dev`](https://github.com/fgnass/node-dev#ignore-paths).
3534

36-
NB! `--ignore-watch` will NOT affect files ignored by TS compilation. Use `--ignore` option (or `TS_NODE_IGNORE`) to pass **regExp strings** (string values for `new RegExp(..)`) for filtering files that should not be compiled, by default `/node_modules/` are ignored by compiler.
37-
3835
**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.
3936
So, to ignore everthing in `node_modules`, just pass `--ignore-watch node_modules`
4037

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

4240
By defalut to keep things clean it puts cached files to system temp directory, you may change this with `--cache-directory` option.
4341

lib/child-require-hook.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ var allowJs = false
77
var compiledDir
88
var preferTs = false
99
var ignore = [/node_modules/]
10+
var readyFile
1011

1112
var compile = (code, fileName) => {
1213
var compiledPath = getCompiledPath(code, fileName, compiledDir)
1314
process.send({
1415
compile: fileName,
15-
code: code,
1616
compiledPath: compiledPath
1717
})
1818
var compiled
1919
var start = new Date().getTime()
20-
var timeout = false
21-
while (compiled === undefined || timeout) {
20+
var passed
21+
while (compiled === undefined) {
2222
if (fs.existsSync(compiledPath + '.done')) {
2323
compiled = fs.readFileSync(compiledPath, 'utf-8')
2424
}
25-
var passed = (new Date().getTime() - start)
25+
passed = (new Date().getTime() - start)
2626
if (passed > timeThreshold) {
2727
throw new Error(
2828
'Could not require ' + fileName + ', compiled path:' + compiledPath
@@ -62,7 +62,9 @@ function registerJsExtension() {
6262
}
6363
}
6464
var _compile = m._compile
65-
var isIgnored = ignore && ignore.reduce((res, ignore) => res || ignore.test(fileName), false)
65+
var isIgnored = ignore && ignore.reduce(function (res, ignore) {
66+
return res || ignore.test(fileName)
67+
}, false)
6668
if (tsCode !== undefined || (allowJs && !isIgnored)) {
6769
m._compile = function (code, fileName) {
6870
if (tsCode !== undefined) {
@@ -80,4 +82,13 @@ function registerJsExtension() {
8082
registerExtensions(['.ts', '.tsx'])
8183
registerJsExtension()
8284

85+
if (readyFile) {
86+
var time = new Date().getTime()
87+
while (!fs.existsSync(readyFile)) {
88+
if (new Date().getTime() - time > 5000) {
89+
throw new Error('Waiting ts-node-dev ready file failed')
90+
}
91+
}
92+
}
93+
8394
module.exports.registerExtensions = registerExtensions

lib/compiler.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ var compiler = {
2323
getCompiledDir: function () {
2424
return path.join(tmpDir, 'compiled').replace(/\\/g, '/')
2525
},
26+
getCompilerReadyFilePath: function () {
27+
return path.join(os.tmpdir(), 'ts-node-dev-ready-' + comilationInstanceStampt)
28+
.replace(/\\/g, '/')
29+
},
2630
getChildHookPath: function () {
2731
return path.join(os.tmpdir(), 'ts-node-dev-hook-' + comilationInstanceStampt + '.js')
2832
.replace(/\\/g, '/')
2933
},
34+
writeReadyFile: function () {
35+
var fileData = fs.writeFileSync(compiler.getCompilerReadyFilePath(), '')
36+
},
3037
writeChildHookFile: function (options) {
3138
var fileData = fs.readFileSync(path.join(__dirname, 'child-require-hook.js'), 'utf-8')
3239
var compileTimeout = parseInt(options['compile-timeout'])
@@ -44,11 +51,13 @@ var compiler = {
4451
var ignoreVal = !ignore || ignore === 'false'
4552
? 'false'
4653
: '[' + (Array.isArray(ignore) ? ignore : ignore.split(/, /))
47-
.map(ignore => 'new RegExp("' + ignore + '")').join(', ') + ']'
54+
.map(ignore => 'new RegExp("' + ignore + '")').join(', ') + ']'
4855
fileData = fileData.replace('var ignore = [/node_modules/]', 'var ignore = ' + ignoreVal)
4956
}
5057
fileData = fileData.replace('var compiledDir', 'var compiledDir = "' + compiler.getCompiledDir() + '"')
5158
fileData = fileData.replace('./get-compiled-path', path.join(__dirname, 'get-compiled-path').replace(/\\/g, '/'))
59+
fileData = fileData.replace('var readyFile',
60+
'var readyFile = "' + compiler.getCompilerReadyFilePath() + '"')
5261
fs.writeFileSync(compiler.getChildHookPath(), fileData)
5362
},
5463
init: function (options) {
@@ -73,7 +82,7 @@ var compiler = {
7382
ignoreWarnings: options['ignoreWarnings'],
7483
disableWarnings: options['disableWarnings'],
7584
compilerOptions: options['compilerOptions']
76-
}
85+
}
7786
try {
7887
register(tsNodeOptions)
7988
} catch (e) {
@@ -110,8 +119,8 @@ var compiler = {
110119
},
111120
compile: function (params) {
112121
var fileName = params.compile
113-
var code = params.code
114-
var compiledPath = params.compiledPath
122+
var code = fs.readFileSync(fileName, 'utf-8')
123+
var compiledPath = params.compiledPath
115124
function writeCompiled(code, filename) {
116125
fs.writeFileSync(compiledPath, code)
117126
fs.writeFileSync(compiledPath + '.done', '')

lib/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,22 @@ module.exports = function (script, scriptArgs, nodeArgs, opts) {
7070
cwd: process.cwd(),
7171
env: process.env
7272
});
73-
if (!compiler.tsConfigPath) {
74-
throw new Error('Check existance of tsconfig.json file.')
75-
}
76-
watcher.add(compiler.tsConfigPath);
7773

78-
if (cfg.respawn) {
79-
child.respawn = true;
80-
}
8174
child.on('message', compiler.compile);
8275
child.on('exit', function (code) {
8376
if (!child.respawn) process.exit(code);
8477
child = undefined;
8578
});
86-
79+
80+
if (cfg.respawn) {
81+
child.respawn = true;
82+
}
83+
84+
if (!compiler.tsConfigPath) {
85+
throw new Error('Check existance of tsconfig.json file.')
86+
}
87+
watcher.add(compiler.tsConfigPath);
88+
8789
// Listen for `required` messages and watch the required file.
8890
ipc.on(child, 'required', function (m) {
8991
var isIgnored = cfg.ignore.some(isPrefixOf(m.required)) || cfg.ignore.some(isRegExpMatch(m.required));
@@ -98,6 +100,7 @@ module.exports = function (script, scriptArgs, nodeArgs, opts) {
98100
notify(m.error, m.message, 'error');
99101
stop(m.willTerminate);
100102
});
103+
compiler.writeReadyFile()
101104
}
102105

103106
function stop(willTerminate) {

package.json

Lines changed: 3 additions & 2 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.10",
3+
"version": "1.0.0-pre.15",
44
"description": "Compiles your TS app and restarts when files are modified.",
55
"keywords": [
66
"restart",
@@ -33,7 +33,8 @@
3333
},
3434
"scripts": {
3535
"test-node-dev": "tap test/*.js",
36-
"test": "node ./bin/ts-node-dev -r ./test/ts/add-require.js --cache --respawn --ignore-watch 'lib' --ignore-watch bin --ignore '/some/' --ignore '/other/' --prefer-ts --cache-directory .ts-node test/ts/test-script test-arg --fd"
36+
"test": "node ./bin/ts-node-dev -r ./test/ts/add-require.js --cache --respawn --ignore-watch 'lib' --ignore-watch bin --prefer-ts --cache-directory .ts-node test/ts/test-script test-arg --fd",
37+
"test-docker": "docker run --rm -v ${PWD}:/app mhart/alpine-node:8.7.0 sh -c 'cd app && node ./bin/ts-node-dev --cache-directory .ts-node test/ts/big'"
3738
},
3839
"dependencies": {
3940
"ts-node": "*",

0 commit comments

Comments
 (0)