Skip to content

Commit 7bc6bec

Browse files
committed
fix allowJs, new tests
1 parent e3209ef commit 7bc6bec

File tree

12 files changed

+157
-25
lines changed

12 files changed

+157
-25
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
node_modules
33
*.log
44
.ts-node
5-
.temp
5+
.tmp

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
version: '3.0'
22
volumes:
33
node10modules: null
4+
node10tmp: null
45
node14modules: null
6+
node14tmp: null
57
services:
68
node10:
79
image: node:10.21.0-jessie-slim
@@ -10,6 +12,7 @@ services:
1012
volumes:
1113
- ./:/package
1214
- node10modules:/package/node_modules
15+
- node10tmp:/package/.tmp
1316

1417
node14:
1518
image: node:14-stretch-slim
@@ -18,3 +21,4 @@ services:
1821
volumes:
1922
- ./:/package
2023
- node14modules:/package/node_modules
24+
- node14tmp:/package/.tmp

lib/child-require-hook.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var readyFile
1414
var execCheck = false
1515
var exitChild = false
1616
var sourceMapSupportPath
17+
var libPath
1718

1819
var checkFileScript = join(__dirname, 'check-file-exists.js')
1920

@@ -67,9 +68,12 @@ function isFileInNodeModules(fileName) {
6768
}
6869

6970
function registerJsExtension() {
70-
var old = require.extensions['.js']
71+
var old = require.extensions['.js']
7172
if (allowJs || preferTs) {
7273
require.extensions['.js'] = function(m, fileName) {
74+
if (fileName.indexOf(libPath) === 0) {
75+
return old(m, fileName)
76+
}
7377
var tsCode
7478
var tsFileName
7579
if (preferTs && !isFileInNodeModules(fileName)) {
@@ -98,7 +102,9 @@ function registerJsExtension() {
98102
}
99103
}
100104

101-
var sourceMapRequire = Module.createRequire ? Module.createRequire(sourceMapSupportPath) : require
105+
var sourceMapRequire = Module.createRequire
106+
? Module.createRequire(sourceMapSupportPath)
107+
: require
102108

103109
sourceMapRequire(sourceMapSupportPath).install({
104110
hookRequire: true

lib/compiler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ var compiler = {
117117
'var sourceMapSupportPath',
118118
'var sourceMapSupportPath = "' + fixPath(sourceMapSupportPath) + '"'
119119
)
120+
fileData = fileData.replace(
121+
'var libPath',
122+
'var libPath = "' + __dirname.replace(/\\/g, '\\\\') + '"'
123+
)
120124
fileData = fileData.replace(/__dirname/, '"' + fixPath(__dirname) + '"')
121125
fs.writeFileSync(compiler.getChildHookPath(), fileData)
122126
},
@@ -146,7 +150,7 @@ var compiler = {
146150
var allowJsEnabled = require.extensions['.js'] !== originalJsHandler
147151
if (allowJsEnabled) {
148152
compiler.allowJs = true
149-
require.extensions['.js'] = originalJsHandler
153+
//require.extensions['.js'] = originalJsHandler
150154
extensions.push('.js')
151155
}
152156

@@ -247,7 +251,7 @@ var compiler = {
247251
}
248252
const _compile = () => {
249253
var ext = path.extname(fileName)
250-
var extHandler = extHandlers[ext]
254+
var extHandler = extHandlers[ext] || require.extensions[ext]
251255
extHandler(m, fileName)
252256
m._compile(code, fileName)
253257
compiler.log.debug(

package.json

Lines changed: 5 additions & 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.47",
3+
"version": "1.0.0-pre.48",
44
"description": "Compiles your TS app and restarts when files are modified.",
55
"keywords": [
66
"restart",
@@ -40,6 +40,7 @@
4040
"ts-node-dev": "node ./bin/ts-node-dev",
4141
"tsnd": "yarn ts-node-dev",
4242
"test": "ts-node --transpile-only test/index.ts",
43+
"test-docker": "docker-compose up",
4344
"ci": "yarn test",
4445
"ci-local": "docker run --name travis-debug -dit quay.io/travisci/ci-nodejs",
4546
"manual": "node ./bin/ts-node-dev --rt 5 --exit-child --tree-kill --clear -r tsconfig-paths/register -r ./test/manual/add-require -r ./test/manual/add-require-2 -r esm -O \"{\\\"module\\\": \\\"es6\\\"}\" --preserve-symlinks --respawn --ignore-watch 'lib' --ignore-watch bin --prefer-ts --debug --poll --interval 1000 --cache-directory .ts-node --inspect -- test/manual/test-script test-arg --fd"
@@ -63,6 +64,7 @@
6364
"@types/node": "^8.0.4",
6465
"@types/tape": "^4.13.0",
6566
"@types/touch": "^3.1.1",
67+
"@types/ts-nameof": "^4.2.1",
6668
"coffee-script": "^1.8.0",
6769
"eslint": "^2.0.0",
6870
"eslint-config-airbnb-base": "^3.0.1",
@@ -72,7 +74,9 @@
7274
"tap": "^5.2.0",
7375
"tape": "^5.0.1",
7476
"touch": "^1.0.0",
77+
"ts-nameof": "^5.0.0",
7578
"tsconfig-paths": "^3.3.1",
79+
"ttypescript": "^1.5.10",
7680
"typescript": "^3.9.5"
7781
},
7882
"peerDependencies": {

test/fixture/js-module.js

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

test/fixture/nameof.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(nameof(console))

test/fixture/to-transform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("something")

test/index.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import * as test from 'tape'
2-
import { spawnTsNodeDev } from './spawn'
2+
import { spawnTsNodeDev, scriptsDir, tmpDir } from './spawn'
33
import * as fs from 'fs-extra'
44
import { join } from 'path'
55
import touch = require('touch')
66

7-
const tempDir = join(__dirname, '../.temp')
8-
export const scriptsDir = join(tempDir, 'fixture')
9-
107
const replaceText = async (
118
script: string,
129
pattern: string | RegExp,
@@ -31,8 +28,8 @@ const waitFor = (timeout: number) => {
3128
return new Promise((resolve) => setTimeout(resolve, timeout))
3229
}
3330

34-
fs.ensureDirSync(tempDir)
35-
fs.removeSync(join(tempDir, 'fixture'))
31+
fs.ensureDirSync(tmpDir)
32+
fs.removeSync(join(tmpDir, 'fixture'))
3633
fs.copySync(join(__dirname, 'fixture'), scriptsDir)
3734

3835
/*
@@ -116,10 +113,9 @@ const notFoundSource = `export const fn = (x: number) => {
116113
return 'v1'
117114
}
118115
`
119-
120116
test('It restarts when not found module added', async (t) => {
121117
const ps = spawnTsNodeDev('--respawn with-not-found.ts', {
122-
// stdout: true,
118+
//stdout: true,
123119
env: {
124120
TS_NODE_DEV_ERROR_RECOMPILE_TIMEOUT: 250,
125121
},
@@ -129,7 +125,50 @@ test('It restarts when not found module added', async (t) => {
129125
setTimeout(() => writeFile('not-found.ts', notFoundSource), 1000)
130126

131127
await ps.waitForLine(/v1/)
132-
t.pass('Restarted successfully after error fixed.')
128+
t.pass('Restarted successfully after module was created.')
133129
await ps.exit()
134130
await removeFile('not-found.ts')
135131
})
132+
133+
test('It handles allowJs option and loads JS modules', async (t) => {
134+
const cOptions = { allowJs: true }
135+
const ps = spawnTsNodeDev(
136+
[
137+
`--respawn`,
138+
`--compiler ttypescript`,
139+
`--compilerOptions=${JSON.stringify(cOptions)}`,
140+
`js-module.js`,
141+
].join(' ')
142+
).turnOnOutput()
143+
await ps.waitForLine(/JS MODULE/)
144+
await ps.exit()
145+
})
146+
147+
test('It allows use TS Transformers', async (t) => {
148+
const cOptions = { plugins: [{ transform: 'ts-nameof', type: 'raw' }] }
149+
const ps = spawnTsNodeDev(
150+
[
151+
`--respawn`,
152+
`--compiler ttypescript`,
153+
`--compilerOptions=${JSON.stringify(cOptions)}`,
154+
`nameof.ts`,
155+
].join(' ')
156+
) //.turnOnOutput()
157+
await ps.waitForLine(/console/)
158+
159+
await ps.exit()
160+
})
161+
162+
test('It allows use custom TS Transformers', async (t) => {
163+
const cOptions = { plugins: [{ transform: __dirname + '/transformer.ts' }] }
164+
const ps = spawnTsNodeDev(
165+
[
166+
`--respawn`,
167+
`--compiler ttypescript`,
168+
`--compilerOptions=${JSON.stringify(cOptions)}`,
169+
`to-transform.ts`,
170+
].join(' ')
171+
) //.turnOnOutput()
172+
await ps.waitForLine(/transformed/)
173+
await ps.exit()
174+
})

test/spawn.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import child = require('child_process')
22
import path = require('path')
33
const bin = path.join(__dirname, '/../bin/ts-node-dev')
4-
const scriptsDir = path.join(__dirname, '/../.temp/fixture')
4+
5+
export const tmpDir = path.join(__dirname, '../.tmp')
6+
export const scriptsDir = path.join(tmpDir, 'fixture')
57

68
export const spawnTsNodeDev = (
79
cmd: string,
8-
opts: { stdout?: boolean; stderr?: boolean; env?: any } = {}
10+
options: { stdout?: boolean; stderr?: boolean; env?: any } = {}
911
) => {
12+
const opts = { ...options }
1013
const nodeArg = [bin].concat(cmd.split(' '))
11-
const ps = child.spawn('node', nodeArg, { cwd: scriptsDir, env: {
12-
...process.env,
13-
...opts.env
14-
} })
14+
const ps = child.spawn('node', nodeArg, {
15+
cwd: scriptsDir,
16+
env: {
17+
...process.env,
18+
...opts.env,
19+
},
20+
})
1521
var out = ''
1622
var err = ''
1723

@@ -33,9 +39,13 @@ export const spawnTsNodeDev = (
3339
? str.indexOf(pattern) >= 0
3440
: pattern.test(str)
3541
}
36-
37-
return {
42+
const self = {
3843
ps,
44+
turnOnOutput: () => {
45+
opts.stderr = true
46+
opts.stdout = true
47+
return self
48+
},
3949
getStdout: () => out,
4050
getStderr: () => err,
4151
waitForLine: (pattern: string | RegExp) => {
@@ -62,4 +72,5 @@ export const spawnTsNodeDev = (
6272
})
6373
},
6474
}
75+
return self
6576
}

0 commit comments

Comments
 (0)