From b61c74544270c7567dfc159abb9dc062711c26a9 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Sun, 7 Oct 2018 11:14:05 -0700 Subject: [PATCH] Remove support for `ts-node` cache output (#701) --- src/bin.ts | 14 ++------ src/index.ts | 99 +++++----------------------------------------------- 2 files changed, 11 insertions(+), 102 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index c3b89c38c..9ca3dd656 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -24,8 +24,6 @@ interface Argv { typeCheck?: boolean transpileOnly?: boolean files?: boolean - cache?: boolean - cacheDirectory?: string compiler?: string ignore?: string | string[] project?: string @@ -38,8 +36,8 @@ interface Argv { const argv = minimist(process.argv.slice(2), { stopEarly: true, - string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'cacheDirectory', 'ignore'], - boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'files', 'cache', 'pretty', 'skipProject', 'skipIgnore'], + string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'ignore'], + boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'files', 'pretty', 'skipProject', 'skipIgnore'], alias: { eval: ['e'], print: ['p'], @@ -48,7 +46,6 @@ const argv = minimist(process.argv.slice(2), { version: ['v'], typeCheck: ['type-check'], transpileOnly: ['T', 'transpile-only'], - cacheDirectory: ['cache-directory'], ignore: ['I'], project: ['P'], skipIgnore: ['skip-ignore'], @@ -58,12 +55,10 @@ const argv = minimist(process.argv.slice(2), { compilerOptions: ['O', 'compiler-options'] }, default: { - cache: DEFAULTS.cache, files: DEFAULTS.files, pretty: DEFAULTS.pretty, typeCheck: DEFAULTS.typeCheck, transpileOnly: DEFAULTS.transpileOnly, - cacheDirectory: DEFAULTS.cacheDirectory, ignore: DEFAULTS.ignore, project: DEFAULTS.project, skipIgnore: DEFAULTS.skipIgnore, @@ -87,7 +82,6 @@ Options: -v, --version Print module version information -T, --transpile-only Use TypeScript's faster \`transpileModule\` - --cache-directory Configure the output file cache directory -I, --ignore [pattern] Override the path patterns to skip compilation -P, --project [path] Path to TypeScript JSON project file -C, --compiler [name] Specify a custom TypeScript compiler @@ -96,7 +90,6 @@ Options: --files Load files from \`tsconfig.json\` on startup --pretty Use pretty diagnostic formatter - --no-cache Disable the local TypeScript Node cache --skip-project Skip reading \`tsconfig.json\` --skip-ignore Skip \`--ignore\` checks `) @@ -115,8 +108,6 @@ const service = register({ pretty: argv.pretty, typeCheck: argv.typeCheck, transpileOnly: argv.transpileOnly, - cache: argv.cache, - cacheDirectory: argv.cacheDirectory, ignore: argv.ignore, project: argv.project, skipIgnore: argv.skipIgnore, @@ -133,7 +124,6 @@ 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) } diff --git a/src/index.ts b/src/index.ts index 9764a933a..42fcd9a42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,8 +43,6 @@ export interface Options { typeCheck?: boolean | null transpileOnly?: boolean | null files?: boolean | null - cache?: boolean | null - cacheDirectory?: string compiler?: string ignore?: string | string[] project?: string @@ -79,9 +77,7 @@ export interface TypeInfo { */ export const DEFAULTS: Options = { files: yn(process.env['TS_NODE_FILES']), - cache: yn(process.env['TS_NODE_CACHE'], { default: true }), pretty: yn(process.env['TS_NODE_PRETTY']), - cacheDirectory: process.env['TS_NODE_CACHE_DIRECTORY'], compiler: process.env['TS_NODE_COMPILER'], compilerOptions: parse(process.env['TS_NODE_COMPILER_OPTIONS']), ignore: split(process.env['TS_NODE_IGNORE']), @@ -150,7 +146,6 @@ export class TSError extends BaseError { export interface Register { cwd: string extensions: string[] - cachedir: string ts: typeof ts compile (code: string, fileName: string, lineOffset?: number): string getTypeInfo (code: string, fileName: string, position: number): TypeInfo @@ -170,7 +165,6 @@ function getTmpDir (): string { */ export function register (opts: Options = {}): Register { const options = Object.assign({}, DEFAULTS, opts) - const cacheDirectory = options.cacheDirectory || getTmpDir() const originalJsHandler = require.extensions['.js'] const ignoreDiagnostics = arrify(options.ignoreDiagnostics).concat([ @@ -210,18 +204,6 @@ export function register (opts: Options = {}): Register { const extensions = ['.ts', '.tsx'] const fileNames = options.files ? config.fileNames : [] - const cachedir = join( - resolve(cwd, cacheDirectory), - getCompilerDigest({ - version: ts.version, - options: config.options, - fileNames, - typeCheck, - ignoreDiagnostics, - compiler - }) - ) - const diagnosticHost: ts.FormatDiagnosticsHost = { getNewLine: () => EOL, getCurrentDirectory: () => cwd, @@ -372,8 +354,15 @@ export function register (opts: Options = {}): Register { } } - const compile = readThrough(cachedir, options.cache === true, memoryCache, getOutput, getExtension) - const register: Register = { cwd, compile, getTypeInfo, extensions, cachedir, ts } + // Create a simple TypeScript compiler proxy. + function compile (code: string, fileName: string, lineOffset?: number) { + const [value, sourceMap] = getOutput(code, fileName, lineOffset) + const output = updateOutput(value, fileName, sourceMap, getExtension) + memoryCache.outputs[fileName] = output + return output + } + + const register: Register = { cwd, compile, getTypeInfo, extensions, ts } // Register the extensions. extensions.forEach(extension => { @@ -494,57 +483,6 @@ function readConfig ( */ type SourceOutput = [string, string] -/** - * Wrap the function with caching. - */ -function readThrough ( - cachedir: string, - shouldCache: boolean, - memoryCache: MemoryCache, - compile: (code: string, fileName: string, lineOffset?: number) => SourceOutput, - getExtension: (fileName: string) => string -) { - if (shouldCache === false) { - return function (code: string, fileName: string, lineOffset?: number) { - debug('readThrough', fileName) - - const [value, sourceMap] = compile(code, fileName, lineOffset) - const output = updateOutput(value, fileName, sourceMap, getExtension) - - memoryCache.outputs[fileName] = output - - return output - } - } - - // Make sure the cache directory exists before continuing. - mkdirp.sync(cachedir) - - return function (code: string, fileName: string, lineOffset?: number) { - debug('readThrough', fileName) - - const cachePath = join(cachedir, getCacheName(code, fileName)) - const extension = getExtension(fileName) - const outputPath = `${cachePath}${extension}` - - try { - const output = readFileSync(outputPath, 'utf8') - if (isValidCacheContent(output)) { - memoryCache.outputs[fileName] = output - return output - } - } catch (err) {/* Ignore. */} - - const [value, sourceMap] = compile(code, fileName, lineOffset) - const output = updateOutput(value, fileName, sourceMap, getExtension) - - memoryCache.outputs[fileName] = output - writeFileSync(outputPath, output) - - return output - } -} - /** * Update the output remapping the source map. */ @@ -567,25 +505,6 @@ function updateSourceMap (sourceMapText: string, fileName: string) { return JSON.stringify(sourceMap) } -/** - * Get the file name for the cache entry. - */ -function getCacheName (sourceCode: string, fileName: string) { - return crypto.createHash('sha256') - .update(extname(fileName), 'utf8') - .update('\x00', 'utf8') - .update(sourceCode, 'utf8') - .digest('hex') -} - -/** - * Ensure the given cached content is valid by sniffing for a base64 encoded '}' - * at the end of the content, which should exist if there is a valid sourceMap present. - */ -function isValidCacheContent (contents: string) { - return /(?:9|0=|Q==)$/.test(contents.slice(-3)) -} - /** * Create a hash of the current configuration. */