@@ -3,8 +3,9 @@ import type * as swcWasm from '@swc/wasm';
3
3
import type * as swcTypes from '@swc/core' ;
4
4
import type { CreateTranspilerOptions , Transpiler } from './types' ;
5
5
import type { NodeModuleEmitKind } from '..' ;
6
+ import { getUseDefineForClassFields } from '../ts-internals' ;
6
7
7
- type SwcInstance = typeof swcWasm ;
8
+ type SwcInstance = typeof swcTypes ;
8
9
export interface SwcTranspilerOptions extends CreateTranspilerOptions {
9
10
/**
10
11
* swc compiler to use for compilation
@@ -28,10 +29,7 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
28
29
let swcDepName : string = 'swc' ;
29
30
if ( typeof swc === 'string' ) {
30
31
swcDepName = swc ;
31
- swcInstance = require ( transpilerConfigLocalResolveHelper (
32
- swc ,
33
- true
34
- ) ) as typeof swcWasm ;
32
+ swcInstance = require ( transpilerConfigLocalResolveHelper ( swc , true ) ) as SwcInstance ;
35
33
} else if ( swc == null ) {
36
34
let swcResolved ;
37
35
try {
@@ -47,25 +45,17 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
47
45
) ;
48
46
}
49
47
}
50
- swcInstance = require ( swcResolved ) as typeof swcWasm ;
48
+ swcInstance = require ( swcResolved ) as SwcInstance ;
51
49
} else {
52
- swcInstance = swc ;
50
+ swcInstance = swc as any as SwcInstance ;
53
51
}
54
52
55
53
// Prepare SWC options derived from typescript compiler options
56
- const { nonTsxOptions, tsxOptions } = createSwcOptions (
57
- config . options ,
58
- nodeModuleEmitKind ,
59
- swcInstance ,
60
- swcDepName
61
- ) ;
54
+ const { nonTsxOptions, tsxOptions } = createSwcOptions ( config . options , nodeModuleEmitKind , swcInstance , swcDepName ) ;
62
55
63
56
const transpile : Transpiler [ 'transpile' ] = ( input , transpileOptions ) => {
64
57
const { fileName } = transpileOptions ;
65
- const swcOptions =
66
- fileName . endsWith ( '.tsx' ) || fileName . endsWith ( '.jsx' )
67
- ? tsxOptions
68
- : nonTsxOptions ;
58
+ const swcOptions = fileName . endsWith ( '.tsx' ) || fileName . endsWith ( '.jsx' ) ? tsxOptions : nonTsxOptions ;
69
59
const { code, map } = swcInstance . transformSync ( input , {
70
60
...swcOptions ,
71
61
filename : fileName ,
@@ -90,7 +80,7 @@ targetMapping.set(/* ts.ScriptTarget.ES2019 */ 6, 'es2019');
90
80
targetMapping . set ( /* ts.ScriptTarget.ES2020 */ 7 , 'es2020' ) ;
91
81
targetMapping . set ( /* ts.ScriptTarget.ES2021 */ 8 , 'es2021' ) ;
92
82
targetMapping . set ( /* ts.ScriptTarget.ES2022 */ 9 , 'es2022' ) ;
93
- targetMapping . set ( /* ts.ScriptTarget.ESNext */ 99 , 'es2022 ' ) ;
83
+ targetMapping . set ( /* ts.ScriptTarget.ESNext */ 99 , 'esnext ' ) ;
94
84
95
85
type SwcTarget = typeof swcTargets [ number ] ;
96
86
/**
@@ -108,6 +98,7 @@ const swcTargets = [
108
98
'es2020' ,
109
99
'es2021' ,
110
100
'es2022' ,
101
+ 'esnext' ,
111
102
] as const ;
112
103
113
104
const ModuleKind = {
@@ -152,6 +143,7 @@ export function createSwcOptions(
152
143
strict,
153
144
alwaysStrict,
154
145
noImplicitUseStrict,
146
+ jsxImportSource,
155
147
} = compilerOptions ;
156
148
157
149
let swcTarget = targetMapping . get ( target ! ) ?? 'es3' ;
@@ -162,15 +154,14 @@ export function createSwcOptions(
162
154
for ( ; swcTargetIndex >= 0 ; swcTargetIndex -- ) {
163
155
try {
164
156
swcInstance . transformSync ( '' , {
165
- jsc : { target : swcTargets [ swcTargetIndex ] } ,
157
+ jsc : { target : swcTargets [ swcTargetIndex ] as swcWasm . JscTarget } ,
166
158
} ) ;
167
159
break ;
168
160
} catch ( e ) { }
169
161
}
170
162
swcTarget = swcTargets [ swcTargetIndex ] ;
171
163
const keepClassNames = target ! >= /* ts.ScriptTarget.ES2016 */ 3 ;
172
- const isNodeModuleKind =
173
- module === ModuleKind . Node16 || module === ModuleKind . NodeNext ;
164
+ const isNodeModuleKind = module === ModuleKind . Node16 || module === ModuleKind . NodeNext ;
174
165
// swc only supports these 4x module options [MUST_UPDATE_FOR_NEW_MODULEKIND]
175
166
const moduleType =
176
167
module === ModuleKind . CommonJS
@@ -202,11 +193,10 @@ export function createSwcOptions(
202
193
: true ;
203
194
204
195
const jsxRuntime : swcTypes . ReactConfig [ 'runtime' ] =
205
- jsx === JsxEmit . ReactJSX || jsx === JsxEmit . ReactJSXDev
206
- ? 'automatic'
207
- : undefined ;
208
- const jsxDevelopment : swcTypes . ReactConfig [ 'development' ] =
209
- jsx === JsxEmit . ReactJSXDev ? true : undefined ;
196
+ jsx === JsxEmit . ReactJSX || jsx === JsxEmit . ReactJSXDev ? 'automatic' : undefined ;
197
+ const jsxDevelopment : swcTypes . ReactConfig [ 'development' ] = jsx === JsxEmit . ReactJSXDev ? true : undefined ;
198
+
199
+ const useDefineForClassFields = getUseDefineForClassFields ( compilerOptions ) ;
210
200
211
201
const nonTsxOptions = createVariant ( false ) ;
212
202
const tsxOptions = createVariant ( true ) ;
@@ -217,13 +207,17 @@ export function createSwcOptions(
217
207
sourceMaps : sourceMap ,
218
208
// isModule: true,
219
209
module : moduleType
220
- ? ( {
221
- noInterop : ! esModuleInterop ,
210
+ ? {
222
211
type : moduleType ,
223
- strictMode,
224
- // For NodeNext and Node12, emit as CJS but do not transform dynamic imports
225
- ignoreDynamic : nodeModuleEmitKind === 'nodecjs' ,
226
- } as swcTypes . ModuleConfig )
212
+ ...( moduleType === 'amd' || moduleType === 'commonjs' || moduleType === 'umd'
213
+ ? {
214
+ noInterop : ! esModuleInterop ,
215
+ strictMode,
216
+ // For NodeNext and Node12, emit as CJS but do not transform dynamic imports
217
+ ignoreDynamic : nodeModuleEmitKind === 'nodecjs' ,
218
+ }
219
+ : { } ) ,
220
+ }
227
221
: undefined ,
228
222
swcrc : false ,
229
223
jsc : {
@@ -234,8 +228,8 @@ export function createSwcOptions(
234
228
decorators : experimentalDecorators ,
235
229
dynamicImport : true ,
236
230
importAssertions : true ,
237
- } ,
238
- target : swcTarget ,
231
+ } as swcWasm . TsParserConfig ,
232
+ target : swcTarget as swcWasm . JscTarget ,
239
233
transform : {
240
234
decoratorMetadata : emitDecoratorMetadata ,
241
235
legacyDecorator : true ,
@@ -246,13 +240,16 @@ export function createSwcOptions(
246
240
pragma : jsxFactory ! ,
247
241
pragmaFrag : jsxFragmentFactory ! ,
248
242
runtime : jsxRuntime ,
249
- } as swcTypes . ReactConfig ,
243
+ importSource : jsxImportSource ,
244
+ } ,
245
+ useDefineForClassFields,
250
246
} ,
251
247
keepClassNames,
252
248
experimental : {
253
- keepImportAssertions : true ,
254
- } ,
255
- } as swcTypes . JscConfig ,
249
+ keepImportAttributes : true ,
250
+ emitAssertForImportAttributes : true ,
251
+ } as swcTypes . JscConfig [ 'experimental' ] ,
252
+ } ,
256
253
} ;
257
254
258
255
// Throw a helpful error if swc version is old, for example, if it rejects `ignoreDynamic`
0 commit comments