From d3f5eaea67e33929ef0d2b0edc8f90a265f6155d Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 21 Jun 2017 16:20:05 -0400 Subject: [PATCH] Combine source map with source file output --- package.json | 2 +- src/_bin.ts | 17 ++++++----- src/index.spec.ts | 2 -- src/index.ts | 73 +++++++++++++++++++---------------------------- 4 files changed, 40 insertions(+), 54 deletions(-) diff --git a/package.json b/package.json index 4560dbc48..35cb46582 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "rimraf": "^2.5.4", "semver": "^5.1.0", "tslint": "^5.0.0", - "tslint-config-standard": "^6.0.0", + "tslint-config-standard": "^6.0.1", "typescript": "^2.1.4", "typings": "^2.0.0" }, diff --git a/src/_bin.ts b/src/_bin.ts index efc39f55b..042e38284 100644 --- a/src/_bin.ts +++ b/src/_bin.ts @@ -105,12 +105,6 @@ const argv = minimist(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. diff --git a/src/index.spec.ts b/src/index.spec.ts index 94ead8667..9f3038635 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -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() } }) diff --git a/src/index.ts b/src/index.ts index 7e59f6bab..3bbcb1476 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -325,7 +318,6 @@ export function register (options: Options = {}): Register { cachedir, shouldCache, getFile, - fileExists, cache, function (code: string, fileName: string, lineOffset?: number) { setCache(code, fileName) @@ -346,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 } @@ -453,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 } @@ -504,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 } /**