@@ -427,10 +427,14 @@ export class TSError extends BaseError {
427
427
}
428
428
}
429
429
430
+ const TS_NODE_SERVICE_BRAND = Symbol ( 'TS_NODE_SERVICE_BRAND' ) ;
431
+
430
432
/**
431
433
* Primary ts-node service, which wraps the TypeScript API and can compile TypeScript to JavaScript
432
434
*/
433
435
export interface Service {
436
+ /** @internal */
437
+ [ TS_NODE_SERVICE_BRAND ] : true ;
434
438
ts : TSCommon ;
435
439
config : _ts . ParsedCommandLine ;
436
440
options : RegisterOptions ;
@@ -446,6 +450,8 @@ export interface Service {
446
450
readonly shouldReplAwait : boolean ;
447
451
/** @internal */
448
452
addDiagnosticFilter ( filter : DiagnosticFilter ) : void ;
453
+ /** @internal */
454
+ installSourceMapSupport ( ) : void ;
449
455
}
450
456
451
457
/**
@@ -477,12 +483,25 @@ export function getExtensions(config: _ts.ParsedCommandLine) {
477
483
return { tsExtensions, jsExtensions } ;
478
484
}
479
485
486
+ /**
487
+ * Create a new TypeScript compiler instance and register it onto node.js
488
+ */
489
+ export function register ( opts ?: RegisterOptions ) : Service ;
480
490
/**
481
491
* Register TypeScript compiler instance onto node.js
482
492
*/
483
- export function register ( opts : RegisterOptions = { } ) : Service {
493
+ export function register ( service : Service ) : Service ;
494
+ export function register (
495
+ serviceOrOpts : Service | RegisterOptions | undefined
496
+ ) : Service {
497
+ // Is this a Service or a RegisterOptions?
498
+ let service = serviceOrOpts as Service ;
499
+ if ( ! ( serviceOrOpts as Service ) ?. [ TS_NODE_SERVICE_BRAND ] ) {
500
+ // Not a service; is options
501
+ service = create ( ( serviceOrOpts ?? { } ) as RegisterOptions ) ;
502
+ }
503
+
484
504
const originalJsHandler = require . extensions [ '.js' ] ;
485
- const service = create ( opts ) ;
486
505
const { tsExtensions, jsExtensions } = getExtensions ( service . config ) ;
487
506
const extensions = [ ...tsExtensions , ...jsExtensions ] ;
488
507
@@ -660,38 +679,41 @@ export function create(rawOptions: CreateOptions = {}): Service {
660
679
}
661
680
662
681
// Install source map support and read from memory cache.
663
- sourceMapSupport . install ( {
664
- environment : 'node' ,
665
- retrieveFile ( pathOrUrl : string ) {
666
- let path = pathOrUrl ;
667
- // If it's a file URL, convert to local path
668
- // Note: fileURLToPath does not exist on early node v10
669
- // I could not find a way to handle non-URLs except to swallow an error
670
- if ( options . experimentalEsmLoader && path . startsWith ( 'file://' ) ) {
671
- try {
672
- path = fileURLToPath ( path ) ;
673
- } catch ( e ) {
674
- /* swallow error */
682
+ installSourceMapSupport ( ) ;
683
+ function installSourceMapSupport ( ) {
684
+ sourceMapSupport . install ( {
685
+ environment : 'node' ,
686
+ retrieveFile ( pathOrUrl : string ) {
687
+ let path = pathOrUrl ;
688
+ // If it's a file URL, convert to local path
689
+ // Note: fileURLToPath does not exist on early node v10
690
+ // I could not find a way to handle non-URLs except to swallow an error
691
+ if ( options . experimentalEsmLoader && path . startsWith ( 'file://' ) ) {
692
+ try {
693
+ path = fileURLToPath ( path ) ;
694
+ } catch ( e ) {
695
+ /* swallow error */
696
+ }
675
697
}
676
- }
677
- path = normalizeSlashes ( path ) ;
678
- return outputCache . get ( path ) ?. content || '' ;
679
- } ,
680
- redirectConflictingLibrary : true ,
681
- onConflictingLibraryRedirect (
682
- request ,
683
- parent ,
684
- isMain ,
685
- options ,
686
- redirectedRequest
687
- ) {
688
- debug (
689
- `Redirected an attempt to require source-map-support to instead receive @cspotcode/source-map-support. " ${
690
- ( parent as NodeJS . Module ) . filename
691
- } " attempted to require or resolve " ${ request } " and was redirected to " ${ redirectedRequest } ".`
692
- ) ;
693
- } ,
694
- } ) ;
698
+ path = normalizeSlashes ( path ) ;
699
+ return outputCache . get ( path ) ?. content || '' ;
700
+ } ,
701
+ redirectConflictingLibrary : true ,
702
+ onConflictingLibraryRedirect (
703
+ request ,
704
+ parent ,
705
+ isMain ,
706
+ options ,
707
+ redirectedRequest
708
+ ) {
709
+ debug (
710
+ `Redirected an attempt to require source-map-support to instead receive @cspotcode/source-map-support. " ${
711
+ ( parent as NodeJS . Module ) . filename
712
+ } " attempted to require or resolve " ${ request } " and was redirected to " ${ redirectedRequest } ".`
713
+ ) ;
714
+ } ,
715
+ } ) ;
716
+ }
695
717
696
718
const shouldHavePrettyErrors =
697
719
options . pretty === undefined ? process . stdout . isTTY : options . pretty ;
@@ -1239,6 +1261,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
1239
1261
}
1240
1262
1241
1263
return {
1264
+ [ TS_NODE_SERVICE_BRAND ] : true ,
1242
1265
ts,
1243
1266
config,
1244
1267
compile,
@@ -1250,6 +1273,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
1250
1273
moduleTypeClassifier,
1251
1274
shouldReplAwait,
1252
1275
addDiagnosticFilter,
1276
+ installSourceMapSupport,
1253
1277
} ;
1254
1278
}
1255
1279
0 commit comments