Skip to content

Commit b61c745

Browse files
authored
Remove support for ts-node cache output (#701)
1 parent df1ac1d commit b61c745

File tree

2 files changed

+11
-102
lines changed

2 files changed

+11
-102
lines changed

src/bin.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ interface Argv {
2424
typeCheck?: boolean
2525
transpileOnly?: boolean
2626
files?: boolean
27-
cache?: boolean
28-
cacheDirectory?: string
2927
compiler?: string
3028
ignore?: string | string[]
3129
project?: string
@@ -38,8 +36,8 @@ interface Argv {
3836

3937
const argv = minimist<Argv>(process.argv.slice(2), {
4038
stopEarly: true,
41-
string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'cacheDirectory', 'ignore'],
42-
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'files', 'cache', 'pretty', 'skipProject', 'skipIgnore'],
39+
string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'ignore'],
40+
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'files', 'pretty', 'skipProject', 'skipIgnore'],
4341
alias: {
4442
eval: ['e'],
4543
print: ['p'],
@@ -48,7 +46,6 @@ const argv = minimist<Argv>(process.argv.slice(2), {
4846
version: ['v'],
4947
typeCheck: ['type-check'],
5048
transpileOnly: ['T', 'transpile-only'],
51-
cacheDirectory: ['cache-directory'],
5249
ignore: ['I'],
5350
project: ['P'],
5451
skipIgnore: ['skip-ignore'],
@@ -58,12 +55,10 @@ const argv = minimist<Argv>(process.argv.slice(2), {
5855
compilerOptions: ['O', 'compiler-options']
5956
},
6057
default: {
61-
cache: DEFAULTS.cache,
6258
files: DEFAULTS.files,
6359
pretty: DEFAULTS.pretty,
6460
typeCheck: DEFAULTS.typeCheck,
6561
transpileOnly: DEFAULTS.transpileOnly,
66-
cacheDirectory: DEFAULTS.cacheDirectory,
6762
ignore: DEFAULTS.ignore,
6863
project: DEFAULTS.project,
6964
skipIgnore: DEFAULTS.skipIgnore,
@@ -87,7 +82,6 @@ Options:
8782
-v, --version Print module version information
8883
8984
-T, --transpile-only Use TypeScript's faster \`transpileModule\`
90-
--cache-directory Configure the output file cache directory
9185
-I, --ignore [pattern] Override the path patterns to skip compilation
9286
-P, --project [path] Path to TypeScript JSON project file
9387
-C, --compiler [name] Specify a custom TypeScript compiler
@@ -96,7 +90,6 @@ Options:
9690
9791
--files Load files from \`tsconfig.json\` on startup
9892
--pretty Use pretty diagnostic formatter
99-
--no-cache Disable the local TypeScript Node cache
10093
--skip-project Skip reading \`tsconfig.json\`
10194
--skip-ignore Skip \`--ignore\` checks
10295
`)
@@ -115,8 +108,6 @@ const service = register({
115108
pretty: argv.pretty,
116109
typeCheck: argv.typeCheck,
117110
transpileOnly: argv.transpileOnly,
118-
cache: argv.cache,
119-
cacheDirectory: argv.cacheDirectory,
120111
ignore: argv.ignore,
121112
project: argv.project,
122113
skipIgnore: argv.skipIgnore,
@@ -133,7 +124,6 @@ if (argv.version) {
133124
console.log(`ts-node v${VERSION}`)
134125
console.log(`node ${process.version}`)
135126
console.log(`typescript v${service.ts.version}`)
136-
console.log(`cache ${JSON.stringify(service.cachedir)}`)
137127
process.exit(0)
138128
}
139129

src/index.ts

+9-90
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ export interface Options {
4343
typeCheck?: boolean | null
4444
transpileOnly?: boolean | null
4545
files?: boolean | null
46-
cache?: boolean | null
47-
cacheDirectory?: string
4846
compiler?: string
4947
ignore?: string | string[]
5048
project?: string
@@ -79,9 +77,7 @@ export interface TypeInfo {
7977
*/
8078
export const DEFAULTS: Options = {
8179
files: yn(process.env['TS_NODE_FILES']),
82-
cache: yn(process.env['TS_NODE_CACHE'], { default: true }),
8380
pretty: yn(process.env['TS_NODE_PRETTY']),
84-
cacheDirectory: process.env['TS_NODE_CACHE_DIRECTORY'],
8581
compiler: process.env['TS_NODE_COMPILER'],
8682
compilerOptions: parse(process.env['TS_NODE_COMPILER_OPTIONS']),
8783
ignore: split(process.env['TS_NODE_IGNORE']),
@@ -150,7 +146,6 @@ export class TSError extends BaseError {
150146
export interface Register {
151147
cwd: string
152148
extensions: string[]
153-
cachedir: string
154149
ts: typeof ts
155150
compile (code: string, fileName: string, lineOffset?: number): string
156151
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
@@ -170,7 +165,6 @@ function getTmpDir (): string {
170165
*/
171166
export function register (opts: Options = {}): Register {
172167
const options = Object.assign({}, DEFAULTS, opts)
173-
const cacheDirectory = options.cacheDirectory || getTmpDir()
174168
const originalJsHandler = require.extensions['.js']
175169

176170
const ignoreDiagnostics = arrify(options.ignoreDiagnostics).concat([
@@ -210,18 +204,6 @@ export function register (opts: Options = {}): Register {
210204
const extensions = ['.ts', '.tsx']
211205
const fileNames = options.files ? config.fileNames : []
212206

213-
const cachedir = join(
214-
resolve(cwd, cacheDirectory),
215-
getCompilerDigest({
216-
version: ts.version,
217-
options: config.options,
218-
fileNames,
219-
typeCheck,
220-
ignoreDiagnostics,
221-
compiler
222-
})
223-
)
224-
225207
const diagnosticHost: ts.FormatDiagnosticsHost = {
226208
getNewLine: () => EOL,
227209
getCurrentDirectory: () => cwd,
@@ -372,8 +354,15 @@ export function register (opts: Options = {}): Register {
372354
}
373355
}
374356

375-
const compile = readThrough(cachedir, options.cache === true, memoryCache, getOutput, getExtension)
376-
const register: Register = { cwd, compile, getTypeInfo, extensions, cachedir, ts }
357+
// Create a simple TypeScript compiler proxy.
358+
function compile (code: string, fileName: string, lineOffset?: number) {
359+
const [value, sourceMap] = getOutput(code, fileName, lineOffset)
360+
const output = updateOutput(value, fileName, sourceMap, getExtension)
361+
memoryCache.outputs[fileName] = output
362+
return output
363+
}
364+
365+
const register: Register = { cwd, compile, getTypeInfo, extensions, ts }
377366

378367
// Register the extensions.
379368
extensions.forEach(extension => {
@@ -494,57 +483,6 @@ function readConfig (
494483
*/
495484
type SourceOutput = [string, string]
496485

497-
/**
498-
* Wrap the function with caching.
499-
*/
500-
function readThrough (
501-
cachedir: string,
502-
shouldCache: boolean,
503-
memoryCache: MemoryCache,
504-
compile: (code: string, fileName: string, lineOffset?: number) => SourceOutput,
505-
getExtension: (fileName: string) => string
506-
) {
507-
if (shouldCache === false) {
508-
return function (code: string, fileName: string, lineOffset?: number) {
509-
debug('readThrough', fileName)
510-
511-
const [value, sourceMap] = compile(code, fileName, lineOffset)
512-
const output = updateOutput(value, fileName, sourceMap, getExtension)
513-
514-
memoryCache.outputs[fileName] = output
515-
516-
return output
517-
}
518-
}
519-
520-
// Make sure the cache directory exists before continuing.
521-
mkdirp.sync(cachedir)
522-
523-
return function (code: string, fileName: string, lineOffset?: number) {
524-
debug('readThrough', fileName)
525-
526-
const cachePath = join(cachedir, getCacheName(code, fileName))
527-
const extension = getExtension(fileName)
528-
const outputPath = `${cachePath}${extension}`
529-
530-
try {
531-
const output = readFileSync(outputPath, 'utf8')
532-
if (isValidCacheContent(output)) {
533-
memoryCache.outputs[fileName] = output
534-
return output
535-
}
536-
} catch (err) {/* Ignore. */}
537-
538-
const [value, sourceMap] = compile(code, fileName, lineOffset)
539-
const output = updateOutput(value, fileName, sourceMap, getExtension)
540-
541-
memoryCache.outputs[fileName] = output
542-
writeFileSync(outputPath, output)
543-
544-
return output
545-
}
546-
}
547-
548486
/**
549487
* Update the output remapping the source map.
550488
*/
@@ -567,25 +505,6 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
567505
return JSON.stringify(sourceMap)
568506
}
569507

570-
/**
571-
* Get the file name for the cache entry.
572-
*/
573-
function getCacheName (sourceCode: string, fileName: string) {
574-
return crypto.createHash('sha256')
575-
.update(extname(fileName), 'utf8')
576-
.update('\x00', 'utf8')
577-
.update(sourceCode, 'utf8')
578-
.digest('hex')
579-
}
580-
581-
/**
582-
* Ensure the given cached content is valid by sniffing for a base64 encoded '}'
583-
* at the end of the content, which should exist if there is a valid sourceMap present.
584-
*/
585-
function isValidCacheContent (contents: string) {
586-
return /(?:9|0=|Q==)$/.test(contents.slice(-3))
587-
}
588-
589508
/**
590509
* Create a hash of the current configuration.
591510
*/

0 commit comments

Comments
 (0)