Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.4.1
Choose a base ref
...
head repository: TypeStrong/ts-node
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.4.2
Choose a head ref
  • 2 commits
  • 4 files changed
  • 2 contributors

Commits on Oct 10, 2016

  1. Fix JSX source maps using { "jsx": "preserve" }

    Closes #214
    srolel authored and blakeembrey committed Oct 10, 2016
    Copy the full SHA
    cfaf3fc View commit details
  2. v1.4.2

    blakeembrey committed Oct 10, 2016
    Copy the full SHA
    196bebf View commit details
Showing with 76 additions and 14 deletions.
  1. +1 −1 package.json
  2. +38 −1 src/index.spec.ts
  3. +34 −12 src/index.ts
  4. +3 −0 tests/with-jsx.tsx
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-node",
"version": "1.4.1",
"version": "1.4.2",
"preferGlobal": true,
"description": "TypeScript execution environment and REPL for node",
"main": "dist/index.js",
39 changes: 38 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -171,7 +171,12 @@ describe('ts-node', function () {
})

describe('register', function () {
register({ project: testDir })
register({
project: testDir,
compilerOptions: {
jsx: 'preserve'
}
})

it('should be able to require typescript', function () {
const m = require('../tests/module')
@@ -205,5 +210,37 @@ describe('ts-node', function () {
done()
}
})

describe('JSX preserve', () => {
let old = require.extensions['.tsx']
let compiled: string

before(function () {
require.extensions['.tsx'] = (m: any, fileName: string) => {
const _compile = m._compile

m._compile = (code: string, fileName: string) => {
compiled = code
return _compile.call(this, code, fileName)
}
return old(m, fileName)
}
})

after(function () {
require.extensions['.tsx'] = old
})

it('should use source maps', function (done) {
try {
require('../tests/with-jsx.tsx')
} catch (error) {
expect(error.stack).to.contain('SyntaxError: Unexpected token <\n')
expect(compiled).to.not.contain('//# sourceMappingURL=w') // First letter of filename.
expect(compiled).to.match(/\/\/# sourceMappingURL=.*\.jsx.map$/)
done()
}
})
})
})
})
46 changes: 34 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -204,6 +204,17 @@ export function register (options: Options = {}): () => Register {
}
}

/**
* Get the extension for a transpiled file.
*/
function getExtension (fileName: string) {
if (compilerOptions.jsx === 'preserve' && extname(fileName) === '.tsx') {
return '.jsx'
}

return '.js'
}

/**
* Create the basic required function using transpile mode.
*/
@@ -225,7 +236,15 @@ export function register (options: Options = {}): () => Register {
return [result.outputText, result.sourceMapText as string]
}

let compile = readThrough(cachedir, shouldCache, getFile, fileExists, cache, getOutput)
let compile = readThrough(
cachedir,
shouldCache,
getFile,
fileExists,
cache,
getOutput,
getExtension
)

let getTypeInfo = function (fileName: string, position: number): TypeInfo {
throw new TypeError(`No type information available under "--fast" mode`)
@@ -313,7 +332,8 @@ export function register (options: Options = {}): () => Register {
addCache(code, fileName)

return getOutput(code, fileName)
}
},
getExtension
)

getTypeInfo = function (fileName: string, position: number) {
@@ -431,17 +451,19 @@ function readThrough (
getFile: (fileName: string) => string,
fileExists: (fileName: string) => boolean,
cache: Cache,
compile: (code: string, fileName: string) => SourceOutput
compile: (code: string, fileName: string) => SourceOutput,
getExtension: (fileName: string) => string
) {
if (shouldCache === false) {
return function (code: string, fileName: string) {
const cachePath = join(cachedir, getCacheName(code, fileName))
const sourceMapPath = `${cachePath}.js.map`
const extension = getExtension(fileName)
const sourceMapPath = `${cachePath}${extension}.map`
const out = compile(code, fileName)

cache.sourceMaps[fileName] = sourceMapPath

const output = updateOutput(out[0], fileName, sourceMapPath)
const output = updateOutput(out[0], fileName, extension, sourceMapPath)
const sourceMap = updateSourceMap(out[1], fileName)

writeFileSync(sourceMapPath, sourceMap)
@@ -452,8 +474,9 @@ function readThrough (

return function (code: string, fileName: string) {
const cachePath = join(cachedir, getCacheName(code, fileName))
const outputPath = `${cachePath}.js`
const sourceMapPath = `${cachePath}.js.map`
const extension = getExtension(fileName)
const outputPath = `${cachePath}${extension}`
const sourceMapPath = `${outputPath}.map`

cache.sourceMaps[fileName] = sourceMapPath

@@ -464,7 +487,7 @@ function readThrough (

const out = compile(code, fileName)

const output = updateOutput(out[0], fileName, sourceMapPath)
const output = updateOutput(out[0], fileName, extension, sourceMapPath)
const sourceMap = updateSourceMap(out[1], fileName)

writeFileSync(outputPath, output)
@@ -477,11 +500,10 @@ function readThrough (
/**
* Update the output remapping the source map.
*/
function updateOutput (outputText: string, fileName: string, sourceMapPath: string) {
// Replace the original extension (E.g. `.ts`).
function updateOutput (outputText: string, fileName: string, extension: string, sourceMapPath: string) {
// Replace the original extension (E.g. `.ts`).
const ext = extname(fileName)
const originalPath = basename(fileName).slice(0, -ext.length) + '.js.map'

const originalPath = basename(fileName).slice(0, -ext.length) + `${extension}.map`
return outputText.slice(0, -originalPath.length) + sourceMapPath.replace(/\\/g, '/')
}

3 changes: 3 additions & 0 deletions tests/with-jsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo2 {
render() { return <div></div> }
}