Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine source map with source file output #368

Merged
merged 1 commit into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 10 additions & 7 deletions src/_bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
Expand Down
75 changes: 30 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

/**
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
Expand All @@ -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]
}
})

Expand All @@ -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))
Expand Down Expand Up @@ -243,7 +237,6 @@ export function register (options: Options = {}): Register {
cachedir,
shouldCache,
getFile,
fileExists,
cache,
getOutput,
getExtension
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

/**
Expand All @@ -528,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')
}
Expand Down