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,26 +41,32 @@ function getTsProperties(args: Parameters<typeof transformer>) {
42
41
compilerOptions = manualTransformOptions . compilerOptions ! ;
43
42
}
44
43
// RunMode: TsNode
45
- else if ( maybeIsTsNode ) {
44
+ else if ( isTsNode ) {
46
45
fileNames = tsNodeProps . fileNames ;
47
-
48
- runMode =
49
- ! program || ( fileNames . length > 1 && program . getRootFileNames ( ) . length === 1 )
50
- ? RunMode . TsNodeTranspileOnly
51
- : RunMode . TsNode ;
52
-
53
- compilerOptions = {
54
- ...( program ?. getCompilerOptions ( ) ?? { } ) ,
55
- ...tsNodeProps ! . compilerOptions
56
- } ;
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
+ } ;
57
62
} else {
58
63
throw new Error (
59
64
`Cannot transform without a Program, ts-node instance, or manual parameters supplied. ` +
60
65
`Make sure you're using ts-patch or ts-node with transpileOnly.`
61
66
) ;
62
67
}
63
68
64
- return { tsInstance, compilerOptions, fileNames, runMode } ;
69
+ return { tsInstance, compilerOptions, fileNames, runMode, tsNodeState } ;
65
70
}
66
71
67
72
// endregion
@@ -88,7 +93,8 @@ export default function transformer(
88
93
tsInstance,
89
94
compilerOptions,
90
95
fileNames,
91
- runMode
96
+ runMode,
97
+ tsNodeState
92
98
} = getTsProperties ( [ program , pluginConfig , transformerExtras , manualTransformOptions ] ) ;
93
99
94
100
const rootDirs = compilerOptions . rootDirs ?. filter ( path . isAbsolute ) ;
@@ -97,14 +103,12 @@ export default function transformer(
97
103
98
104
/* Add supplements for various run modes */
99
105
let emitHost = transformationContext . getEmitHost ( ) ;
100
- if ( ! emitHost || runMode === RunMode . TsNodeTranspileOnly ) {
106
+ if ( ! emitHost || tsNodeState === TsNodeState . Stripped ) {
101
107
if ( ! fileNames )
102
108
throw new Error (
103
109
`No EmitHost found and could not determine files to be processed. Please file an issue with a reproduction!`
104
110
) ;
105
111
emitHost = createSyntheticEmitHost ( compilerOptions , tsInstance , getCanonicalFileName , fileNames as string [ ] ) ;
106
- } else if ( runMode === RunMode . TsNode ) {
107
- Object . assign ( emitHost , { getCompilerOptions : ( ) => compilerOptions } ) ;
108
112
}
109
113
110
114
/* Create Visitor Context */
@@ -125,6 +129,7 @@ export default function transformer(
125
129
tsVersionMinor,
126
130
emitHost,
127
131
runMode,
132
+ tsNodeState,
128
133
excludeMatchers : config . exclude ?. map ( ( globPattern ) => new Minimatch ( globPattern , { matchBase : true } ) ) ,
129
134
outputFileNamesCache : new Map ( ) ,
130
135
// Get paths patterns appropriate for TS compiler version
0 commit comments