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: v3.0.6
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: v3.1.0
Choose a head ref
  • 4 commits
  • 4 files changed
  • 3 contributors

Commits on Jun 14, 2017

  1. Copy the full SHA
    28697b1 View commit details

Commits on Jun 21, 2017

  1. Copy the full SHA
    61f1c16 View commit details
  2. Copy the full SHA
    1cac86b View commit details
  3. 3.1.0

    blakeembrey committed Jun 21, 2017
    Copy the full SHA
    3ea7458 View commit details
Showing with 45 additions and 56 deletions.
  1. +2 −2 package.json
  2. +10 −7 src/_bin.ts
  3. +0 −2 src/index.spec.ts
  4. +33 −45 src/index.ts
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-node",
"version": "3.0.6",
"version": "3.1.0",
"preferGlobal": true,
"description": "TypeScript execution environment and REPL for node",
"main": "dist/index.js",
@@ -58,7 +58,7 @@
"rimraf": "^2.5.4",
"semver": "^5.1.0",
"tslint": "^5.0.0",
"tslint-config-standard": "^5.0.1",
"tslint-config-standard": "^6.0.1",
"typescript": "^2.1.4",
"typings": "^2.0.0"
},
17 changes: 10 additions & 7 deletions src/_bin.ts
Original file line number Diff line number Diff line change
@@ -105,12 +105,6 @@ const argv = minimist<Argv>(process.argv.slice(2, stop), {
}
})

if (argv.version) {
console.log(`ts-node v${VERSION}`)
console.log(`node ${process.version}`)
process.exit(0)
}

if (argv.help) {
console.log(`
Usage: ts-node [options] [ -e script | script.ts ] [arguments]
@@ -155,8 +149,17 @@ const service = register({
fileExists: isEval ? fileExistsEval : fileExists
})

// Output project information.
if (argv.version) {
console.log(`ts-node v${VERSION}`)
console.log(`node ${process.version}`)
console.log(`typescript v${service.ts.version}`)
console.log(`cache ${JSON.stringify(service.cachedir)}`)
process.exit(0)
}

// Require specified modules before start-up.
;(Module as any)._preloadModules(arrify(argv.require))
(Module as any)._preloadModules(arrify(argv.require))

/**
* Eval helpers.
2 changes: 0 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -252,8 +252,6 @@ describe('ts-node', function () {
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()
}
})
78 changes: 33 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ export interface Options {
interface Cache {
contents: { [fileName: string]: string }
versions: { [fileName: string]: number }
sourceMaps: { [fileName: string]: string }
outputs: { [fileName: string]: string }
}

/**
@@ -115,6 +115,8 @@ export function normalizeSlashes (value: string): string {
export interface Register {
cwd: string
extensions: string[]
cachedir: string
ts: TSCommon
compile (code: string, fileName: string, lineOffset?: number): string
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
}
@@ -150,7 +152,7 @@ export function register (options: Options = {}): Register {
const cache: Cache = {
contents: Object.create(null),
versions: Object.create(null),
sourceMaps: Object.create(null)
outputs: Object.create(null)
}

const ignore = arrify(
@@ -165,13 +167,8 @@ export function register (options: Options = {}): Register {
// Install source map support and read from cache.
sourceMapSupport.install({
environment: 'node',
retrieveSourceMap (fileName: string) {
if (cache.sourceMaps[fileName]) {
return {
url: cache.sourceMaps[fileName],
map: getFile(cache.sourceMaps[fileName])
}
}
retrieveFile (path: string) {
return cache.outputs[path]
}
})

@@ -187,9 +184,6 @@ export function register (options: Options = {}): Register {
getCompilerDigest({ version: ts.version, fast, ignoreWarnings, disableWarnings, config, compiler })
)

// Make sure the cache directory _always_ exists (source maps write there).
mkdirp.sync(cachedir)

// Render the configuration errors and exit the script.
if (configDiagnostics.length) {
throw new TSError(formatDiagnostics(configDiagnostics, cwd, ts, 0))
@@ -243,7 +237,6 @@ export function register (options: Options = {}): Register {
cachedir,
shouldCache,
getFile,
fileExists,
cache,
getOutput,
getExtension
@@ -278,6 +271,9 @@ export function register (options: Options = {}): Register {
},
getDirectories: getDirectories,
directoryExists: directoryExists,
fileExists: fileExists,
readFile: getFile,
readDirectory: ts.sys.readDirectory,
getNewLine: () => EOL,
getCurrentDirectory: () => cwd,
getCompilationSettings: () => config.options,
@@ -322,7 +318,6 @@ export function register (options: Options = {}): Register {
cachedir,
shouldCache,
getFile,
fileExists,
cache,
function (code: string, fileName: string, lineOffset?: number) {
setCache(code, fileName)
@@ -343,10 +338,12 @@ export function register (options: Options = {}): Register {
}
}

const register: Register = { cwd, compile, getTypeInfo, extensions }
const register: Register = { cwd, compile, getTypeInfo, extensions, cachedir, ts }

// Register the extensions.
extensions.forEach(extension => registerExtension(extension, ignore, register, originalJsHandler))
extensions.forEach(extension => {
registerExtension(extension, ignore, register, originalJsHandler)
})

return register
}
@@ -450,49 +447,40 @@ function readThrough (
cachedir: string,
shouldCache: boolean,
getFile: (fileName: string) => string,
fileExists: (fileName: string) => boolean,
cache: Cache,
compile: (code: string, fileName: string, lineOffset?: number) => SourceOutput,
getExtension: (fileName: string) => string
) {
if (shouldCache === false) {
return function (code: string, fileName: string, lineOffset?: number) {
const cachePath = join(cachedir, getCacheName(code, fileName))
const extension = getExtension(fileName)
const sourceMapPath = `${cachePath}${extension}.map`
const out = compile(code, fileName, lineOffset)

cache.sourceMaps[fileName] = sourceMapPath

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

writeFileSync(sourceMapPath, sourceMap)
cache.outputs[fileName] = output

return output
}
}

// Make sure the cache directory exists before continuing.
mkdirp.sync(cachedir)

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

cache.sourceMaps[fileName] = sourceMapPath

// Use the cache when available.
if (fileExists(outputPath)) {
return getFile(outputPath)
}

const out = compile(code, fileName, lineOffset)
try {
const output = getFile(outputPath)
cache.outputs[fileName] = output
return output
} catch (err) {/* Ignore. */}

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

cache.outputs[fileName] = output
writeFileSync(outputPath, output)
writeFileSync(sourceMapPath, sourceMap)

return output
}
@@ -501,11 +489,11 @@ function readThrough (
/**
* Update the output remapping the source map.
*/
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) + `${extension}.map`
return outputText.slice(0, -originalPath.length) + sourceMapPath.replace(/\\/g, '/')
function updateOutput (outputText: string, fileName: string, sourceMap: string) {
const base64Map = new Buffer(updateSourceMap(sourceMap, fileName), 'utf8').toString('base64')
const sourceMapContent = `data:application/json;charset=utf-8;base64,${base64Map}`

return outputText.slice(0, -1 * (basename(fileName).length + 4)) + sourceMapContent
}

/**
@@ -525,7 +513,7 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
function getCacheName (sourceCode: string, fileName: string) {
return crypto.createHash('sha256')
.update(extname(fileName), 'utf8')
.update('\0', 'utf8')
.update('\x001\x00', 'utf8') // Store "cache version" in hash.
.update(sourceCode, 'utf8')
.digest('hex')
}