1
- import { relative , basename , extname , resolve , dirname , join } from 'path'
1
+ import { relative , basename , extname , resolve , dirname } from 'path'
2
2
import sourceMapSupport = require( 'source-map-support' )
3
3
import yn from 'yn'
4
4
import { BaseError } from 'make-error'
@@ -10,20 +10,13 @@ import * as _ts from 'typescript'
10
10
*/
11
11
export const REGISTER_INSTANCE = Symbol . for ( 'ts-node.register.instance' )
12
12
13
- /**
14
- * Expose `ts-node` instance information globally for consumers.
15
- */
16
- export interface RegisterInstance {
17
- config : _ts . ParsedCommandLine
18
- }
19
-
20
13
/**
21
14
* Expose `REGISTER_INSTANCE` information on node.js `process`.
22
15
*/
23
16
declare global {
24
17
namespace NodeJS {
25
18
interface Process {
26
- [ REGISTER_INSTANCE ] ?: RegisterInstance
19
+ [ REGISTER_INSTANCE ] ?: Register
27
20
}
28
21
}
29
22
}
@@ -76,9 +69,9 @@ export interface TSCommon {
76
69
export const VERSION = require ( '../package.json' ) . version
77
70
78
71
/**
79
- * Registration options .
72
+ * Options for creating a new TypeScript compiler instance .
80
73
*/
81
- export interface Options {
74
+ export interface CreateOptions {
82
75
cwd ?: string
83
76
scope ?: boolean | null
84
77
pretty ?: boolean | null
@@ -99,6 +92,13 @@ export interface Options {
99
92
transformers ?: _ts . CustomTransformers | ( ( p : _ts . Program ) => _ts . CustomTransformers )
100
93
}
101
94
95
+ /**
96
+ * Options for registering a TypeScript compiler instance globally.
97
+ */
98
+ export interface RegisterOptions extends CreateOptions {
99
+ preferTsExts ?: boolean | null
100
+ }
101
+
102
102
/**
103
103
* Track the project information.
104
104
*/
@@ -122,7 +122,7 @@ export interface TypeInfo {
122
122
/**
123
123
* Default register options.
124
124
*/
125
- export const DEFAULTS : Options = {
125
+ export const DEFAULTS : RegisterOptions = {
126
126
cwd : process . env . TS_NODE_CWD ,
127
127
scope : yn ( process . env . TS_NODE_SCOPE ) ,
128
128
files : yn ( process . env [ 'TS_NODE_FILES' ] ) ,
@@ -196,8 +196,8 @@ export class TSError extends BaseError {
196
196
*/
197
197
export interface Register {
198
198
cwd : string
199
- extensions : string [ ]
200
199
ts : TSCommon
200
+ config : _ts . ParsedCommandLine
201
201
enabled ( enabled ?: boolean ) : boolean
202
202
ignored ( fileName : string ) : boolean
203
203
compile ( code : string , fileName : string , lineOffset ?: number ) : string
@@ -220,12 +220,32 @@ function cachedLookup <T> (fn: (arg: string) => T): (arg: string) => T {
220
220
}
221
221
222
222
/**
223
- * Register TypeScript compiler.
223
+ * Register TypeScript compiler instance onto node.js
224
224
*/
225
- export function register ( opts : Options = { } ) : Register {
225
+ export function register ( opts : RegisterOptions = { } ) : Register {
226
226
const options = Object . assign ( { } , DEFAULTS , opts )
227
227
const originalJsHandler = require . extensions [ '.js' ] // tslint:disable-line
228
+ const service = create ( options )
229
+ const extensions = [ '.ts' ]
228
230
231
+ // Enable additional extensions when JSX or `allowJs` is enabled.
232
+ if ( service . config . options . jsx ) extensions . push ( '.tsx' )
233
+ if ( service . config . options . allowJs ) extensions . push ( '.js' )
234
+ if ( service . config . options . jsx && service . config . options . allowJs ) extensions . push ( '.jsx' )
235
+
236
+ // Expose registered instance globally.
237
+ process [ REGISTER_INSTANCE ] = service
238
+
239
+ // Register the extensions.
240
+ registerExtensions ( options . preferTsExts , extensions , service , originalJsHandler )
241
+
242
+ return service
243
+ }
244
+
245
+ /**
246
+ * Create TypeScript compiler instance.
247
+ */
248
+ export function create ( options : CreateOptions = { } ) : Register {
229
249
const ignoreDiagnostics = [
230
250
6059 , // "'rootDir' is expected to contain all source files."
231
251
18002 , // "The 'files' list in config file is empty."
@@ -248,7 +268,6 @@ export function register (opts: Options = {}): Register {
248
268
const fileExists = options . fileExists || ts . sys . fileExists
249
269
const config = readConfig ( cwd , ts , fileExists , readFile , options )
250
270
const configDiagnosticList = filterDiagnostics ( config . errors , ignoreDiagnostics )
251
- const extensions = [ '.ts' ]
252
271
const outputCache = new Map < string , string > ( )
253
272
254
273
const diagnosticHost : _ts . FormatDiagnosticsHost = {
@@ -289,11 +308,6 @@ export function register (opts: Options = {}): Register {
289
308
// Render the configuration errors.
290
309
if ( configDiagnosticList . length ) reportTSError ( configDiagnosticList )
291
310
292
- // Enable additional extensions when JSX or `allowJs` is enabled.
293
- if ( config . options . jsx ) extensions . push ( '.tsx' )
294
- if ( config . options . allowJs ) extensions . push ( '.js' )
295
- if ( config . options . jsx && config . options . allowJs ) extensions . push ( '.jsx' )
296
-
297
311
/**
298
312
* Get the extension for a transpiled file.
299
313
*/
@@ -450,15 +464,8 @@ export function register (opts: Options = {}): Register {
450
464
let active = true
451
465
const enabled = ( enabled ?: boolean ) => enabled === undefined ? active : ( active = ! ! enabled )
452
466
const ignored = ( fileName : string ) => ! active || ! isScoped ( fileName ) || shouldIgnore ( fileName , ignore )
453
- const register : Register = { cwd, compile, getTypeInfo, extensions, ts, ignored, enabled }
454
-
455
- // Expose registered instance globally.
456
- process [ REGISTER_INSTANCE ] = { config }
457
-
458
- // Register the extensions.
459
- registerExtensions ( options . preferTsExts , extensions , register , originalJsHandler )
460
467
461
- return register
468
+ return { cwd , ts , config , compile , getTypeInfo , ignored , enabled }
462
469
}
463
470
464
471
/**
@@ -563,7 +570,7 @@ function readConfig (
563
570
ts : TSCommon ,
564
571
fileExists : ( path : string ) => boolean ,
565
572
readFile : ( path : string ) => string | undefined ,
566
- options : Options
573
+ options : CreateOptions
567
574
) : _ts . ParsedCommandLine {
568
575
let config : any = { compilerOptions : { } }
569
576
let basePath = normalizeSlashes ( cwd )
0 commit comments