Skip to content

Commit f148008

Browse files
authored
Add compiler option back and use require.resolve (#758)
1 parent abcb13b commit f148008

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Create a new node.js configuration, add `-r ts-node/register` to node args and m
109109
You can set options by passing them before the script path, via programmatic usage or via environment variables.
110110

111111
```sh
112-
ts-node --project src/tsconfig.json hello-world.ts
112+
ts-node --compiler ntypescript --project src/tsconfig.json hello-world.ts
113113
```
114114

115115
### CLI Options
@@ -127,6 +127,7 @@ _Environment variable denoted in parentheses._
127127
* `--cacheDirectory` Configure the output file cache directory (`TS_NODE_CACHE_DIRECTORY`)
128128
* `-I, --ignore [pattern]` Override the path patterns to skip compilation (`TS_NODE_IGNORE`, default: `/node_modules/`)
129129
* `-P, --project [path]` Path to TypeScript JSON project file (`TS_NODE_PROJECT`)
130+
* `-C, --compiler [name]` Specify a custom TypeScript compiler (`TS_NODE_COMPILER`, default: `typescript`)
130131
* `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code (`TS_NODE_IGNORE_DIAGNOSTICS`)
131132
* `-O, --compilerOptions [opts]` JSON object to merge with compiler options (`TS_NODE_COMPILER_OPTIONS`)
132133
* `--files` Load files from `tsconfig.json` on startup (`TS_NODE_FILES`, default: `false`)

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"chai": "^4.0.1",
6363
"istanbul": "^0.4.0",
6464
"mocha": "^5.0.1",
65+
"ntypescript": "^1.201507091536.1",
6566
"proxyquire": "^2.0.0",
6667
"react": "^16.0.0",
6768
"rimraf": "^2.5.4",

src/index.ts

+35-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import yn = require('yn')
55
import arrify = require('arrify')
66
import { BaseError } from 'make-error'
77
import * as util from 'util'
8-
import * as ts from 'typescript'
8+
import * as _ts from 'typescript'
99

1010
/**
1111
* @internal
@@ -26,6 +26,28 @@ const debugFn = shouldDebug ?
2626
} :
2727
<T, U> (_: string, fn: (arg: T) => U) => fn
2828

29+
/**
30+
* Common TypeScript interfaces between versions.
31+
*/
32+
export interface TSCommon {
33+
version: typeof _ts.version
34+
sys: typeof _ts.sys
35+
ScriptSnapshot: typeof _ts.ScriptSnapshot
36+
displayPartsToString: typeof _ts.displayPartsToString
37+
createLanguageService: typeof _ts.createLanguageService
38+
getDefaultLibFilePath: typeof _ts.getDefaultLibFilePath
39+
getPreEmitDiagnostics: typeof _ts.getPreEmitDiagnostics
40+
flattenDiagnosticMessageText: typeof _ts.flattenDiagnosticMessageText
41+
transpileModule: typeof _ts.transpileModule
42+
ModuleKind: typeof _ts.ModuleKind
43+
ScriptTarget: typeof _ts.ScriptTarget
44+
findConfigFile: typeof _ts.findConfigFile
45+
readConfigFile: typeof _ts.readConfigFile
46+
parseJsonConfigFileContent: typeof _ts.parseJsonConfigFileContent
47+
formatDiagnostics: typeof _ts.formatDiagnostics
48+
formatDiagnosticsWithColorAndContext: typeof _ts.formatDiagnosticsWithColorAndContext
49+
}
50+
2951
/**
3052
* Export the current version.
3153
*/
@@ -48,7 +70,7 @@ export interface Options {
4870
ignoreDiagnostics?: number | string | Array<number | string>
4971
readFile?: (path: string) => string | undefined
5072
fileExists?: (path: string) => boolean
51-
transformers?: ts.CustomTransformers
73+
transformers?: _ts.CustomTransformers
5274
}
5375

5476
/**
@@ -142,7 +164,7 @@ export class TSError extends BaseError {
142164
export interface Register {
143165
cwd: string
144166
extensions: string[]
145-
ts: typeof ts
167+
ts: TSCommon
146168
compile (code: string, fileName: string, lineOffset?: number): string
147169
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
148170
}
@@ -182,15 +204,17 @@ export function register (opts: Options = {}): Register {
182204
const cwd = process.cwd()
183205
const { compilerOptions, project, skipProject } = options
184206
const typeCheck = options.typeCheck === true || options.transpileOnly !== true
207+
const compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd] })
208+
const ts: typeof _ts = require(compiler)
185209
const transformers = options.transformers || undefined
186210
const readFile = options.readFile || ts.sys.readFile
187211
const fileExists = options.fileExists || ts.sys.fileExists
188-
const config = readConfig(cwd, fileExists, readFile, compilerOptions, project, skipProject)
212+
const config = readConfig(cwd, ts, fileExists, readFile, compilerOptions, project, skipProject)
189213
const configDiagnosticList = filterDiagnostics(config.errors, ignoreDiagnostics)
190214
const extensions = ['.ts', '.tsx']
191215
const fileNames = options.files ? config.fileNames : []
192216

193-
const diagnosticHost: ts.FormatDiagnosticsHost = {
217+
const diagnosticHost: _ts.FormatDiagnosticsHost = {
194218
getNewLine: () => EOL,
195219
getCurrentDirectory: () => cwd,
196220
getCanonicalFileName: (path) => path
@@ -200,7 +224,7 @@ export function register (opts: Options = {}): Register {
200224
? ts.formatDiagnosticsWithColorAndContext
201225
: ts.formatDiagnostics
202226

203-
function createTSError (diagnostics: ReadonlyArray<ts.Diagnostic>) {
227+
function createTSError (diagnostics: ReadonlyArray<_ts.Diagnostic>) {
204228
const diagnosticText = formatDiagnostics(diagnostics, diagnosticHost)
205229
const diagnosticCodes = diagnostics.map(x => x.code)
206230
return new TSError(diagnosticText, diagnosticCodes)
@@ -398,7 +422,7 @@ function registerExtension (
398422
/**
399423
* Do post-processing on config options to support `ts-node`.
400424
*/
401-
function fixConfig (config: ts.ParsedCommandLine) {
425+
function fixConfig (ts: TSCommon, config: _ts.ParsedCommandLine) {
402426
// Delete options that *should not* be passed through.
403427
delete config.options.out
404428
delete config.options.outFile
@@ -425,12 +449,13 @@ function fixConfig (config: ts.ParsedCommandLine) {
425449
*/
426450
function readConfig (
427451
cwd: string,
452+
ts: TSCommon,
428453
fileExists: (path: string) => boolean,
429454
readFile: (path: string) => string | undefined,
430455
compilerOptions?: object,
431456
project?: string | null,
432457
noProject?: boolean | null
433-
): ts.ParsedCommandLine {
458+
): _ts.ParsedCommandLine {
434459
let config: any = { compilerOptions: {} }
435460
let basePath = normalizeSlashes(cwd)
436461
let configFileName: string | undefined = undefined
@@ -461,7 +486,7 @@ function readConfig (
461486
// Override default configuration options `ts-node` requires.
462487
config.compilerOptions = Object.assign({}, config.compilerOptions, compilerOptions, TS_NODE_COMPILER_OPTIONS)
463488

464-
return fixConfig(ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName))
489+
return fixConfig(ts, ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName))
465490
}
466491

467492
/**
@@ -494,6 +519,6 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
494519
/**
495520
* Filter diagnostics.
496521
*/
497-
function filterDiagnostics (diagnostics: ts.Diagnostic[], ignore: number[]) {
522+
function filterDiagnostics (diagnostics: _ts.Diagnostic[], ignore: number[]) {
498523
return diagnostics.filter(x => ignore.indexOf(x.code) === -1)
499524
}

0 commit comments

Comments
 (0)