@@ -12,8 +12,11 @@ import {
12
12
assign ,
13
13
attemptRequireWithV8CompileCache ,
14
14
cachedLookup ,
15
+ createProjectLocalResolveHelper ,
16
+ getBasePathForProjectLocalDependencyResolution ,
15
17
normalizeSlashes ,
16
18
parse ,
19
+ ProjectLocalResolveHelper ,
17
20
split ,
18
21
yn ,
19
22
} from './util' ;
@@ -265,6 +268,8 @@ export interface CreateOptions {
265
268
* Transpile with swc instead of the TypeScript compiler, and skip typechecking.
266
269
*
267
270
* Equivalent to setting both `transpileOnly: true` and `transpiler: 'ts-node/transpilers/swc'`
271
+ *
272
+ * For complete instructions: https://typestrong.org/ts-node/docs/transpilers
268
273
*/
269
274
swc ?: boolean ;
270
275
/**
@@ -371,6 +376,8 @@ type ModuleTypes = Record<string, 'cjs' | 'esm' | 'package'>;
371
376
/** @internal */
372
377
export interface OptionBasePaths {
373
378
moduleTypes ?: string ;
379
+ transpiler ?: string ;
380
+ compiler ?: string ;
374
381
}
375
382
376
383
/**
@@ -499,6 +506,8 @@ export interface Service {
499
506
enableExperimentalEsmLoaderInterop ( ) : void ;
500
507
/** @internal */
501
508
transpileOnly : boolean ;
509
+ /** @internal */
510
+ projectLocalResolveHelper : ProjectLocalResolveHelper ;
502
511
}
503
512
504
513
/**
@@ -587,17 +596,22 @@ export function create(rawOptions: CreateOptions = {}): Service {
587
596
* be changed by the tsconfig, so we have to do this twice.
588
597
*/
589
598
function loadCompiler ( name : string | undefined , relativeToPath : string ) {
590
- const compiler = require . resolve ( name || 'typescript' , {
591
- paths : [ relativeToPath , __dirname ] ,
592
- } ) ;
599
+ const projectLocalResolveHelper =
600
+ createProjectLocalResolveHelper ( relativeToPath ) ;
601
+ const compiler = projectLocalResolveHelper ( name || 'typescript' , true ) ;
593
602
const ts : typeof _ts = attemptRequireWithV8CompileCache ( require , compiler ) ;
594
- return { compiler, ts } ;
603
+ return { compiler, ts, projectLocalResolveHelper } ;
595
604
}
596
605
597
606
// Compute minimum options to read the config file.
598
- let { compiler, ts } = loadCompiler (
607
+ let { compiler, ts, projectLocalResolveHelper } = loadCompiler (
599
608
compilerName ,
600
- rawOptions . projectSearchDir ?? rawOptions . project ?? cwd
609
+ getBasePathForProjectLocalDependencyResolution (
610
+ undefined ,
611
+ rawOptions . projectSearchDir ,
612
+ rawOptions . project ,
613
+ cwd
614
+ )
601
615
) ;
602
616
603
617
// Read config file and merge new options between env and CLI options.
@@ -615,6 +629,21 @@ export function create(rawOptions: CreateOptions = {}): Service {
615
629
...( rawOptions . require || [ ] ) ,
616
630
] ;
617
631
632
+ // Re-load the compiler in case it has changed.
633
+ // Compiler is loaded relative to tsconfig.json, so tsconfig discovery may cause us to load a
634
+ // different compiler than we did above, even if the name has not changed.
635
+ if ( configFilePath ) {
636
+ ( { compiler, ts, projectLocalResolveHelper } = loadCompiler (
637
+ options . compiler ,
638
+ getBasePathForProjectLocalDependencyResolution (
639
+ configFilePath ,
640
+ rawOptions . projectSearchDir ,
641
+ rawOptions . project ,
642
+ cwd
643
+ )
644
+ ) ) ;
645
+ }
646
+
618
647
// Experimental REPL await is not compatible targets lower than ES2018
619
648
const targetSupportsTla = config . options . target ! >= ts . ScriptTarget . ES2018 ;
620
649
if ( options . experimentalReplAwait === true && ! targetSupportsTla ) {
@@ -635,13 +664,6 @@ export function create(rawOptions: CreateOptions = {}): Service {
635
664
tsVersionSupportsTla &&
636
665
targetSupportsTla ;
637
666
638
- // Re-load the compiler in case it has changed.
639
- // Compiler is loaded relative to tsconfig.json, so tsconfig discovery may cause us to load a
640
- // different compiler than we did above, even if the name has not changed.
641
- if ( configFilePath ) {
642
- ( { compiler, ts } = loadCompiler ( options . compiler , configFilePath ) ) ;
643
- }
644
-
645
667
// swc implies two other options
646
668
// typeCheck option was implemented specifically to allow overriding tsconfig transpileOnly from the command-line
647
669
// So we should allow using typeCheck to override swc
@@ -733,14 +755,10 @@ export function create(rawOptions: CreateOptions = {}): Service {
733
755
typeof transpiler === 'string' ? transpiler : transpiler [ 0 ] ;
734
756
const transpilerOptions =
735
757
typeof transpiler === 'string' ? { } : transpiler [ 1 ] ?? { } ;
736
- // TODO mimic fixed resolution logic from loadCompiler main
737
- // TODO refactor into a more generic "resolve dep relative to project" helper
738
- const transpilerPath = require . resolve ( transpilerName , {
739
- paths : [ cwd , __dirname ] ,
740
- } ) ;
758
+ const transpilerPath = projectLocalResolveHelper ( transpilerName , true ) ;
741
759
const transpilerFactory : TranspilerFactory = require ( transpilerPath ) . create ;
742
760
customTranspiler = transpilerFactory ( {
743
- service : { options, config } ,
761
+ service : { options, config, projectLocalResolveHelper } ,
744
762
...transpilerOptions ,
745
763
} ) ;
746
764
}
@@ -925,7 +943,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
925
943
ts,
926
944
cwd,
927
945
config,
928
- configFilePath ,
946
+ projectLocalResolveHelper ,
929
947
} ) ;
930
948
serviceHost . resolveModuleNames = resolveModuleNames ;
931
949
serviceHost . getResolvedModuleWithFailedLookupLocationsFromCache =
@@ -1076,10 +1094,10 @@ export function create(rawOptions: CreateOptions = {}): Service {
1076
1094
} = createResolverFunctions ( {
1077
1095
host,
1078
1096
cwd,
1079
- configFilePath,
1080
1097
config,
1081
1098
ts,
1082
1099
getCanonicalFileName,
1100
+ projectLocalResolveHelper,
1083
1101
} ) ;
1084
1102
host . resolveModuleNames = resolveModuleNames ;
1085
1103
host . resolveTypeReferenceDirectives = resolveTypeReferenceDirectives ;
@@ -1356,6 +1374,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
1356
1374
installSourceMapSupport,
1357
1375
enableExperimentalEsmLoaderInterop,
1358
1376
transpileOnly,
1377
+ projectLocalResolveHelper,
1359
1378
} ;
1360
1379
}
1361
1380
0 commit comments