-
-
Notifications
You must be signed in to change notification settings - Fork 534
/
index.ts
603 lines (509 loc) · 18.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import { relative, basename, extname, resolve, dirname, join } from 'path'
import sourceMapSupport = require('source-map-support')
import yn from 'yn'
import { BaseError } from 'make-error'
import * as util from 'util'
import * as _ts from 'typescript'
/**
* @internal
*/
export const INSPECT_CUSTOM = util.inspect.custom || 'inspect'
/**
* Debugging `ts-node`.
*/
const shouldDebug = yn(process.env.TS_NODE_DEBUG)
const debug = shouldDebug ? console.log.bind(console, 'ts-node') : () => undefined
const debugFn = shouldDebug ?
<T, U> (key: string, fn: (arg: T) => U) => {
let i = 0
return (x: T) => {
debug(key, x, ++i)
return fn(x)
}
} :
<T, U> (_: string, fn: (arg: T) => U) => fn
/**
* Common TypeScript interfaces between versions.
*/
export interface TSCommon {
version: typeof _ts.version
sys: typeof _ts.sys
ScriptSnapshot: typeof _ts.ScriptSnapshot
displayPartsToString: typeof _ts.displayPartsToString
createLanguageService: typeof _ts.createLanguageService
getDefaultLibFilePath: typeof _ts.getDefaultLibFilePath
getPreEmitDiagnostics: typeof _ts.getPreEmitDiagnostics
flattenDiagnosticMessageText: typeof _ts.flattenDiagnosticMessageText
transpileModule: typeof _ts.transpileModule
ModuleKind: typeof _ts.ModuleKind
ScriptTarget: typeof _ts.ScriptTarget
findConfigFile: typeof _ts.findConfigFile
readConfigFile: typeof _ts.readConfigFile
parseJsonConfigFileContent: typeof _ts.parseJsonConfigFileContent
formatDiagnostics: typeof _ts.formatDiagnostics
formatDiagnosticsWithColorAndContext: typeof _ts.formatDiagnosticsWithColorAndContext
}
/**
* Export the current version.
*/
export const VERSION = require('../package.json').version
/**
* Registration options.
*/
export interface Options {
pretty?: boolean | null
typeCheck?: boolean | null
transpileOnly?: boolean | null
logError?: boolean | null
files?: boolean | null
compiler?: string
ignore?: string[]
project?: string
skipIgnore?: boolean | null
skipProject?: boolean | null
preferTsExts?: boolean | null
compilerOptions?: object
ignoreDiagnostics?: Array<number | string>
readFile?: (path: string) => string | undefined
fileExists?: (path: string) => boolean
transformers?: _ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers)
}
/**
* Track the project information.
*/
class MemoryCache {
fileContents = new Map<string, string>()
fileVersions = new Map<string, number>()
constructor (public rootFileNames: string[] = []) {
for (const fileName of rootFileNames) this.fileVersions.set(fileName, 1)
}
}
/**
* Information retrieved from type info check.
*/
export interface TypeInfo {
name: string
comment: string
}
/**
* Default register options.
*/
export const DEFAULTS: Options = {
files: yn(process.env['TS_NODE_FILES']),
pretty: yn(process.env['TS_NODE_PRETTY']),
compiler: process.env['TS_NODE_COMPILER'],
compilerOptions: parse(process.env['TS_NODE_COMPILER_OPTIONS']),
ignore: split(process.env['TS_NODE_IGNORE']),
project: process.env['TS_NODE_PROJECT'],
skipIgnore: yn(process.env['TS_NODE_SKIP_IGNORE']),
skipProject: yn(process.env['TS_NODE_SKIP_PROJECT']),
preferTsExts: yn(process.env['TS_NODE_PREFER_TS_EXTS']),
ignoreDiagnostics: split(process.env['TS_NODE_IGNORE_DIAGNOSTICS']),
typeCheck: yn(process.env['TS_NODE_TYPE_CHECK']),
transpileOnly: yn(process.env['TS_NODE_TRANSPILE_ONLY']),
logError: yn(process.env['TS_NODE_LOG_ERROR'])
}
/**
* Default TypeScript compiler options required by `ts-node`.
*/
const TS_NODE_COMPILER_OPTIONS = {
sourceMap: true,
inlineSourceMap: false,
inlineSources: true,
declaration: false,
noEmit: false,
outDir: '$$ts-node$$'
}
/**
* Split a string array of values.
*/
export function split (value: string | undefined) {
return typeof value === 'string' ? value.split(/ *, */g) : undefined
}
/**
* Parse a string as JSON.
*/
export function parse (value: string | undefined): object | undefined {
return typeof value === 'string' ? JSON.parse(value) : undefined
}
/**
* Replace backslashes with forward slashes.
*/
export function normalizeSlashes (value: string): string {
return value.replace(/\\/g, '/')
}
/**
* TypeScript diagnostics error.
*/
export class TSError extends BaseError {
name = 'TSError'
constructor (public diagnosticText: string, public diagnosticCodes: number[]) {
super(`⨯ Unable to compile TypeScript:\n${diagnosticText}`)
}
/**
* @internal
*/
[INSPECT_CUSTOM] () {
return this.diagnosticText
}
}
/**
* Return type for registering `ts-node`.
*/
export interface Register {
cwd: string
extensions: string[]
ts: TSCommon
compile (code: string, fileName: string, lineOffset?: number): string
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
}
/**
* Cached fs operation wrapper.
*/
function cachedLookup <T> (fn: (arg: string) => T): (arg: string) => T {
const cache = new Map<string, T>()
return (arg: string): T => {
if (!cache.has(arg)) {
cache.set(arg, fn(arg))
}
return cache.get(arg)!
}
}
/**
* Register TypeScript compiler.
*/
export function register (opts: Options = {}): Register {
const options = Object.assign({}, DEFAULTS, opts)
const originalJsHandler = require.extensions['.js'] // tslint:disable-line
const ignoreDiagnostics = [
6059, // "'rootDir' is expected to contain all source files."
18002, // "The 'files' list in config file is empty."
18003, // "No inputs were found in config file."
...(options.ignoreDiagnostics || [])
].map(Number)
const ignore = options.skipIgnore ? [] : (
options.ignore || ['/node_modules/']
).map(str => new RegExp(str))
// Require the TypeScript compiler and configuration.
const cwd = process.cwd()
const typeCheck = options.typeCheck === true || options.transpileOnly !== true
const compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd, __dirname] })
const ts: typeof _ts = require(compiler)
const transformers = options.transformers || undefined
const readFile = options.readFile || ts.sys.readFile
const fileExists = options.fileExists || ts.sys.fileExists
const config = readConfig(cwd, ts, fileExists, readFile, options)
const configDiagnosticList = filterDiagnostics(config.errors, ignoreDiagnostics)
const extensions = ['.ts']
const outputCache = new Map<string, string>()
const diagnosticHost: _ts.FormatDiagnosticsHost = {
getNewLine: () => ts.sys.newLine,
getCurrentDirectory: () => cwd,
getCanonicalFileName: (path) => path
}
// Install source map support and read from memory cache.
sourceMapSupport.install({
environment: 'node',
retrieveFile (path: string) {
return outputCache.get(path) || ''
}
})
const formatDiagnostics = process.stdout.isTTY || options.pretty
? ts.formatDiagnosticsWithColorAndContext
: ts.formatDiagnostics
function createTSError (diagnostics: ReadonlyArray<_ts.Diagnostic>) {
const diagnosticText = formatDiagnostics(diagnostics, diagnosticHost)
const diagnosticCodes = diagnostics.map(x => x.code)
return new TSError(diagnosticText, diagnosticCodes)
}
function reportTSError (configDiagnosticList: _ts.Diagnostic[]) {
const error = createTSError(configDiagnosticList)
if (options.logError) {
// Print error in red color and continue execution.
console.error('\x1b[31m%s\x1b[0m', error)
} else {
// Throw error and exit the script.
throw error
}
}
// Render the configuration errors.
if (configDiagnosticList.length) reportTSError(configDiagnosticList)
// Enable additional extensions when JSX or `allowJs` is enabled.
if (config.options.jsx) extensions.push('.tsx')
if (config.options.allowJs) extensions.push('.js')
if (config.options.jsx && config.options.allowJs) extensions.push('.jsx')
/**
* Get the extension for a transpiled file.
*/
const getExtension = config.options.jsx === ts.JsxEmit.Preserve ?
((path: string) => /\.[tj]sx$/.test(path) ? '.jsx' : '.js') :
((_: string) => '.js')
/**
* Create the basic required function using transpile mode.
*/
let getOutput: (code: string, fileName: string, lineOffset: number) => SourceOutput
let getTypeInfo: (_code: string, _fileName: string, _position: number) => TypeInfo
// Use full language services when the fast option is disabled.
if (typeCheck) {
const memoryCache = new MemoryCache(config.fileNames)
const cachedReadFile = cachedLookup(debugFn('readFile', readFile))
const getCustomTransformers = () => {
if (typeof transformers === 'function') {
const program = service.getProgram()
return program ? transformers(program) : undefined
}
return transformers
}
// Create the compiler host for type checking.
const serviceHost: _ts.LanguageServiceHost = {
getScriptFileNames: () => memoryCache.rootFileNames,
getScriptVersion: (fileName: string) => {
const version = memoryCache.fileVersions.get(fileName)
return version === undefined ? '' : version.toString()
},
getScriptSnapshot (fileName: string) {
let contents = memoryCache.fileContents.get(fileName)
// Read contents into TypeScript memory cache.
if (contents === undefined) {
contents = cachedReadFile(fileName)
if (contents === undefined) return
memoryCache.fileVersions.set(fileName, 1)
memoryCache.fileContents.set(fileName, contents)
}
return ts.ScriptSnapshot.fromString(contents)
},
readFile: cachedReadFile,
readDirectory: cachedLookup(debugFn('readDirectory', ts.sys.readDirectory)),
getDirectories: cachedLookup(debugFn('getDirectories', ts.sys.getDirectories)),
fileExists: cachedLookup(debugFn('fileExists', fileExists)),
directoryExists: cachedLookup(debugFn('directoryExists', ts.sys.directoryExists)),
getNewLine: () => ts.sys.newLine,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
getCurrentDirectory: () => cwd,
getCompilationSettings: () => config.options,
getDefaultLibFileName: () => ts.getDefaultLibFilePath(config.options),
getCustomTransformers: getCustomTransformers
}
const registry = ts.createDocumentRegistry(ts.sys.useCaseSensitiveFileNames, cwd)
const service = ts.createLanguageService(serviceHost, registry)
// Set the file contents into cache manually.
const updateMemoryCache = (contents: string, fileName: string) => {
const fileVersion = memoryCache.fileVersions.get(fileName) || 0
// Add to `rootFiles` when discovered for the first time.
if (fileVersion === 0) memoryCache.rootFileNames.push(fileName)
// Avoid incrementing cache when nothing has changed.
if (memoryCache.fileContents.get(fileName) === contents) return
memoryCache.fileVersions.set(fileName, fileVersion + 1)
memoryCache.fileContents.set(fileName, contents)
}
getOutput = (code: string, fileName: string) => {
updateMemoryCache(code, fileName)
const output = service.getEmitOutput(fileName)
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
const diagnostics = service.getSemanticDiagnostics(fileName)
.concat(service.getSyntacticDiagnostics(fileName))
const diagnosticList = filterDiagnostics(diagnostics, ignoreDiagnostics)
if (diagnosticList.length) reportTSError(diagnosticList)
if (output.emitSkipped) {
throw new TypeError(`${relative(cwd, fileName)}: Emit skipped`)
}
// Throw an error when requiring `.d.ts` files.
if (output.outputFiles.length === 0) {
throw new TypeError(
'Unable to require `.d.ts` file.\n' +
'This is usually the result of a faulty configuration or import. ' +
'Make sure there is a `.js`, `.json` or another executable extension and ' +
'loader (attached before `ts-node`) available alongside ' +
`\`${basename(fileName)}\`.`
)
}
return [output.outputFiles[1].text, output.outputFiles[0].text]
}
getTypeInfo = (code: string, fileName: string, position: number) => {
updateMemoryCache(code, fileName)
const info = service.getQuickInfoAtPosition(fileName, position)
const name = ts.displayPartsToString(info ? info.displayParts : [])
const comment = ts.displayPartsToString(info ? info.documentation : [])
return { name, comment }
}
} else {
if (typeof transformers === 'function') {
throw new TypeError('Transformers function is unavailable in "--transpile-only"')
}
getOutput = (code: string, fileName: string): SourceOutput => {
const result = ts.transpileModule(code, {
fileName,
transformers,
compilerOptions: config.options,
reportDiagnostics: true
})
const diagnosticList = result.diagnostics ?
filterDiagnostics(result.diagnostics, ignoreDiagnostics) :
[]
if (diagnosticList.length) reportTSError(configDiagnosticList)
return [result.outputText, result.sourceMapText as string]
}
getTypeInfo = () => {
throw new TypeError('Type information is unavailable in "--transpile-only"')
}
}
// Create a simple TypeScript compiler proxy.
function compile (code: string, fileName: string, lineOffset = 0) {
const [value, sourceMap] = getOutput(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap, getExtension)
outputCache.set(fileName, output)
return output
}
const register: Register = { cwd, compile, getTypeInfo, extensions, ts }
// Register the extensions.
registerExtensions(options.preferTsExts, extensions, ignore, register, originalJsHandler)
return register
}
/**
* Check if the filename should be ignored.
*/
function shouldIgnore (filename: string, ignore: RegExp[]) {
const relname = normalizeSlashes(filename)
return ignore.some(x => x.test(relname))
}
/**
* "Refreshes" an extension on `require.extentions`.
*
* @param {string} ext
*/
function reorderRequireExtension (ext: string) {
const old = require.extensions[ext] // tslint:disable-line
delete require.extensions[ext] // tslint:disable-line
require.extensions[ext] = old // tslint:disable-line
}
/**
* Register the extensions to support when importing files.
*/
function registerExtensions (
preferTsExts: boolean | null | undefined,
extensions: string[],
ignore: RegExp[],
register: Register,
originalJsHandler: (m: NodeModule, filename: string) => any
) {
// Register new extensions.
for (const ext of extensions) {
registerExtension(ext, ignore, register, originalJsHandler)
}
if (preferTsExts) {
// tslint:disable-next-line
const preferredExtensions = new Set([...extensions, ...Object.keys(require.extensions)])
for (const ext of preferredExtensions) reorderRequireExtension(ext)
}
}
/**
* Register the extension for node.
*/
function registerExtension (
ext: string,
ignore: RegExp[],
register: Register,
originalHandler: (m: NodeModule, filename: string) => any
) {
const old = require.extensions[ext] || originalHandler // tslint:disable-line
require.extensions[ext] = function (m: any, filename) { // tslint:disable-line
if (shouldIgnore(filename, ignore)) {
return old(m, filename)
}
const _compile = m._compile
m._compile = function (code: string, fileName: string) {
debug('module._compile', fileName)
return _compile.call(this, register.compile(code, fileName), fileName)
}
return old(m, filename)
}
}
/**
* Do post-processing on config options to support `ts-node`.
*/
function fixConfig (ts: TSCommon, config: _ts.ParsedCommandLine) {
// Delete options that *should not* be passed through.
delete config.options.out
delete config.options.outFile
delete config.options.composite
delete config.options.declarationDir
delete config.options.declarationMap
delete config.options.emitDeclarationOnly
delete config.options.tsBuildInfoFile
delete config.options.incremental
// Target ES5 output by default (instead of ES3).
if (config.options.target === undefined) {
config.options.target = ts.ScriptTarget.ES5
}
// Target CommonJS modules by default (instead of magically switching to ES6 when the target is ES6).
if (config.options.module === undefined) {
config.options.module = ts.ModuleKind.CommonJS
}
return config
}
/**
* Load TypeScript configuration.
*/
function readConfig (
cwd: string,
ts: TSCommon,
fileExists: (path: string) => boolean,
readFile: (path: string) => string | undefined,
options: Options
): _ts.ParsedCommandLine {
let config: any = { compilerOptions: {} }
let basePath = normalizeSlashes(cwd)
let configFileName: string | undefined = undefined
// Read project configuration when available.
if (!options.skipProject) {
configFileName = options.project
? normalizeSlashes(resolve(cwd, options.project))
: ts.findConfigFile(normalizeSlashes(cwd), fileExists)
if (configFileName) {
const result = ts.readConfigFile(configFileName, readFile)
// Return diagnostics.
if (result.error) {
return { errors: [result.error], fileNames: [], options: {} }
}
config = result.config
basePath = normalizeSlashes(dirname(configFileName))
}
}
// Remove resolution of "files".
if (!options.files) {
config.files = []
config.include = []
}
// Override default configuration options `ts-node` requires.
config.compilerOptions = Object.assign({}, config.compilerOptions, options.compilerOptions, TS_NODE_COMPILER_OPTIONS)
return fixConfig(ts, ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName))
}
/**
* Internal source output.
*/
type SourceOutput = [string, string]
/**
* Update the output remapping the source map.
*/
function updateOutput (outputText: string, fileName: string, sourceMap: string, getExtension: (fileName: string) => string) {
const base64Map = Buffer.from(updateSourceMap(sourceMap, fileName), 'utf8').toString('base64')
const sourceMapContent = `data:application/json;charset=utf-8;base64,${base64Map}`
const sourceMapLength = `${basename(fileName)}.map`.length + (getExtension(fileName).length - extname(fileName).length)
return outputText.slice(0, -sourceMapLength) + sourceMapContent
}
/**
* Update the source map contents for improved output.
*/
function updateSourceMap (sourceMapText: string, fileName: string) {
const sourceMap = JSON.parse(sourceMapText)
sourceMap.file = fileName
sourceMap.sources = [fileName]
delete sourceMap.sourceRoot
return JSON.stringify(sourceMap)
}
/**
* Filter diagnostics.
*/
function filterDiagnostics (diagnostics: _ts.Diagnostic[], ignore: number[]) {
return diagnostics.filter(x => ignore.indexOf(x.code) === -1)
}