Skip to content

Commit 477e705

Browse files
authored
Fix source map output for .tsx files (#395)
Closes #393
1 parent e9e9621 commit 477e705

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949
},
5050
"homepage": "https://github.com/TypeStrong/ts-node",
5151
"devDependencies": {
52+
"@types/react": "^15.0.38",
5253
"chai": "^4.0.1",
5354
"istanbul": "^0.4.0",
5455
"mocha": "^3.0.0",
5556
"ntypescript": "^1.201507091536.1",
5657
"proxyquire": "^1.7.2",
58+
"react": "^15.6.1",
5759
"rimraf": "^2.5.4",
5860
"semver": "^5.1.0",
5961
"tslint": "^5.0.0",

src/index.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const testDir = join(__dirname, '../tests')
1010
const EXEC_PATH = join(__dirname, '../dist/bin')
1111
const BIN_EXEC = `node "${EXEC_PATH}" --project "${testDir}"`
1212

13+
const SOURCE_MAP_REGEXP = /\/\/# sourceMappingURL=data:application\/json;charset=utf\-8;base64,[\w\+]+=*$/
14+
1315
describe('ts-node', function () {
1416
this.timeout(10000)
1517

@@ -177,7 +179,7 @@ describe('ts-node', function () {
177179
})
178180

179181
it('should pipe into an eval script', function (done) {
180-
const cp = exec(`${BIN_EXEC} -p 'declare var process: any\nprocess.stdin.isTTY'`, function (err, stdout) {
182+
const cp = exec(`${BIN_EXEC} --fast -p 'process.stdin.isTTY'`, function (err, stdout) {
181183
expect(err).to.equal(null)
182184
expect(stdout).to.equal('undefined\n')
183185

@@ -204,6 +206,15 @@ describe('ts-node', function () {
204206
return done()
205207
})
206208
})
209+
210+
it.skip('should use source maps with react tsx', function (done) {
211+
exec(`${BIN_EXEC} -r ./tests/emit-compiled.ts tests/jsx-react.tsx`, function (err, stdout) {
212+
expect(err).to.equal(null)
213+
expect(stdout).to.equal('todo')
214+
215+
return done()
216+
})
217+
})
207218
})
208219

209220
describe('register', function () {
@@ -259,6 +270,7 @@ describe('ts-node', function () {
259270
compiled = code
260271
return _compile.call(this, code, fileName)
261272
}
273+
262274
return old(m, fileName)
263275
}
264276
})
@@ -272,8 +284,11 @@ describe('ts-node', function () {
272284
require('../tests/with-jsx.tsx')
273285
} catch (error) {
274286
expect(error.stack).to.contain('SyntaxError: Unexpected token <\n')
275-
done()
276287
}
288+
289+
expect(compiled).to.match(SOURCE_MAP_REGEXP)
290+
291+
done()
277292
})
278293
})
279294
})

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ function updateOutput (outputText: string, fileName: string, sourceMap: string)
510510
const base64Map = new Buffer(updateSourceMap(sourceMap, fileName), 'utf8').toString('base64')
511511
const sourceMapContent = `data:application/json;charset=utf-8;base64,${base64Map}`
512512

513-
return outputText.slice(0, -1 * (basename(fileName).length + 4)) + sourceMapContent
513+
return outputText.replace(/[^=]+$/, sourceMapContent)
514514
}
515515

516516
/**

tests/emit-compiled.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const extensions = ['.tsx']
2+
3+
extensions.forEach(ext => {
4+
const old = require.extensions[ext]
5+
6+
require.extensions[ext] = (m, path) => {
7+
const _compile = m._compile
8+
9+
m._compile = (code, path) => {
10+
console.log(code)
11+
return _compile.call(this, code, path)
12+
}
13+
14+
return old(m, path)
15+
}
16+
})

tests/jsx-react.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react'
2+
3+
const Component = props => {
4+
return <div />
5+
}
6+
7+
export default Component

tests/tsconfig.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"files": []
2+
"compilerOptions": {
3+
"jsx": "react"
4+
},
5+
"files": [
6+
"../typings/globals/node/index.d.ts"
7+
]
38
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"noUnusedParameters": true,
1313
"moduleResolution": "node",
1414
"sourceMap": true,
15-
"inlineSources": true
15+
"inlineSources": true,
16+
"types": []
1617
},
1718
"include": [
1819
"src/**/*",

0 commit comments

Comments
 (0)