1
1
import path from "path" ;
2
2
import ts , { CompilerOptions } from "typescript" ;
3
- import { RunMode , TsTransformPathsConfig , TsTransformPathsContext , VisitorContext } from "./types" ;
3
+ import { RunMode , TsNodeState , TsTransformPathsConfig , TsTransformPathsContext , VisitorContext } from "./types" ;
4
4
import { nodeVisitor } from "./visitor" ;
5
5
import { createHarmonyFactory } from "./harmony" ;
6
6
import { Minimatch } from "minimatch" ;
@@ -16,6 +16,7 @@ function getTsProperties(args: Parameters<typeof transformer>) {
16
16
let fileNames : readonly string [ ] | undefined ;
17
17
let compilerOptions : CompilerOptions ;
18
18
let runMode : RunMode ;
19
+ let tsNodeState : TsNodeState | undefined ;
19
20
20
21
const { 0 : program , 2 : extras , 3 : manualTransformOptions } = args ;
21
22
@@ -25,13 +26,11 @@ function getTsProperties(args: Parameters<typeof transformer>) {
25
26
26
27
/* Determine RunMode & Setup */
27
28
// Note: ts-node passes a Program with the paths property stripped, so we do some comparison to determine if it's the caller
28
- const maybeIsTsNode =
29
- tsNodeProps &&
30
- ( ! program ||
31
- ( compilerOptions ! . configFilePath === tsNodeProps . compilerOptions . configFilePath && ! compilerOptions ! . paths ) ) ;
29
+ const isTsNode =
30
+ tsNodeProps && ( ! program || compilerOptions ! . configFilePath === tsNodeProps . compilerOptions . configFilePath ) ;
32
31
33
32
// RunMode: Program
34
- if ( program && ! maybeIsTsNode ) {
33
+ if ( program && ! isTsNode ) {
35
34
runMode = RunMode . Program ;
36
35
compilerOptions = compilerOptions ! ;
37
36
}
@@ -42,18 +41,32 @@ function getTsProperties(args: Parameters<typeof transformer>) {
42
41
compilerOptions = manualTransformOptions . compilerOptions ! ;
43
42
}
44
43
// RunMode: TsNode
45
- else if ( maybeIsTsNode ) {
46
- runMode = RunMode . TsNode ;
44
+ else if ( isTsNode ) {
47
45
fileNames = tsNodeProps . fileNames ;
48
- compilerOptions = tsNodeProps . compilerOptions ;
46
+ runMode = RunMode . TsNode ;
47
+
48
+ tsNodeState =
49
+ ! program ||
50
+ ( fileNames . length > 1 && program ?. getRootFileNames ( ) . length === 1 ) ||
51
+ ( ! compilerOptions ! . paths && tsNodeProps ! . compilerOptions . paths )
52
+ ? TsNodeState . Stripped
53
+ : TsNodeState . Full ;
54
+
55
+ compilerOptions =
56
+ tsNodeState === TsNodeState . Full
57
+ ? compilerOptions !
58
+ : {
59
+ ...( program ?. getCompilerOptions ( ) ?? { } ) ,
60
+ ...tsNodeProps ! . compilerOptions ,
61
+ } ;
49
62
} else {
50
63
throw new Error (
51
64
`Cannot transform without a Program, ts-node instance, or manual parameters supplied. ` +
52
65
`Make sure you're using ts-patch or ts-node with transpileOnly.`
53
66
) ;
54
67
}
55
68
56
- return { tsInstance, compilerOptions, fileNames, runMode } ;
69
+ return { tsInstance, compilerOptions, fileNames, runMode, tsNodeState } ;
57
70
}
58
71
59
72
// endregion
@@ -80,7 +93,8 @@ export default function transformer(
80
93
tsInstance,
81
94
compilerOptions,
82
95
fileNames,
83
- runMode
96
+ runMode,
97
+ tsNodeState
84
98
} = getTsProperties ( [ program , pluginConfig , transformerExtras , manualTransformOptions ] ) ;
85
99
86
100
const rootDirs = compilerOptions . rootDirs ?. filter ( path . isAbsolute ) ;
@@ -89,14 +103,12 @@ export default function transformer(
89
103
90
104
/* Add supplements for various run modes */
91
105
let emitHost = transformationContext . getEmitHost ( ) ;
92
- if ( ! emitHost ) {
106
+ if ( ! emitHost || tsNodeState === TsNodeState . Stripped ) {
93
107
if ( ! fileNames )
94
108
throw new Error (
95
109
`No EmitHost found and could not determine files to be processed. Please file an issue with a reproduction!`
96
110
) ;
97
111
emitHost = createSyntheticEmitHost ( compilerOptions , tsInstance , getCanonicalFileName , fileNames as string [ ] ) ;
98
- } else if ( runMode === RunMode . TsNode ) {
99
- Object . assign ( emitHost , { getCompilerOptions : ( ) => compilerOptions } ) ;
100
112
}
101
113
102
114
/* Create Visitor Context */
@@ -117,6 +129,7 @@ export default function transformer(
117
129
tsVersionMinor,
118
130
emitHost,
119
131
runMode,
132
+ tsNodeState,
120
133
excludeMatchers : config . exclude ?. map ( ( globPattern ) => new Minimatch ( globPattern , { matchBase : true } ) ) ,
121
134
outputFileNamesCache : new Map ( ) ,
122
135
// Get paths patterns appropriate for TS compiler version
0 commit comments