forked from swc-project/swc-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
99 lines (93 loc) · 2.94 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
transform as swcTransform,
transformSync as swcTransformSync,
Options as SwcOptions,
ReactConfig,
Config,
JscTarget,
} from '@swc/core'
// Oldest LTS Node.js supported target
const DEFAULT_ES_TARGET: JscTarget = 'es2018'
export interface Options {
target?: JscTarget
module?: 'commonjs' | 'umd' | 'amd' | 'es6'
sourcemap?: Config['sourceMaps']
jsx?: boolean
experimentalDecorators?: boolean
emitDecoratorMetadata?: boolean
useDefineForClassFields?: boolean
dynamicImport?: boolean
esModuleInterop?: boolean
keepClassNames?: boolean
externalHelpers?: boolean
react?: Partial<ReactConfig>
baseUrl?: string
paths?: {
[from: string]: [string]
}
swc?: SwcOptions
ignoreDynamic?: boolean
}
function transformOption(path: string, options?: Options, jest = false): SwcOptions {
const opts = options ?? {}
opts.esModuleInterop = opts.esModuleInterop ?? true
const moduleType = options?.module ?? 'commonjs'
return {
filename: path,
jsc: options?.swc?.swcrc
? undefined
: {
target: opts.target ?? DEFAULT_ES_TARGET,
externalHelpers: jest ? true : Boolean(opts.externalHelpers),
parser: {
syntax: 'typescript' as const,
tsx: typeof opts.jsx !== 'undefined' ? opts.jsx : path.endsWith('.tsx'),
decorators: Boolean(opts.experimentalDecorators),
dynamicImport: Boolean(opts.dynamicImport),
},
transform: {
legacyDecorator: Boolean(opts.experimentalDecorators),
decoratorMetadata: Boolean(opts.emitDecoratorMetadata),
useDefineForClassFields: Boolean(opts.useDefineForClassFields),
react: options?.react,
// @ts-expect-error
hidden: {
jest,
},
},
keepClassNames: opts.keepClassNames,
paths: opts.paths,
baseUrl: opts.baseUrl,
},
minify: false,
isModule: true,
module: options?.swc?.swcrc
? undefined
: {
type: moduleType,
...(moduleType === 'commonjs' || moduleType === 'umd' || moduleType === 'amd'
? {
noInterop: !opts.esModuleInterop,
ignoreDynamic: opts.ignoreDynamic,
}
: undefined),
},
sourceMaps: options?.swc?.swcrc
? undefined
: jest || typeof opts.sourcemap === 'undefined'
? 'inline'
: opts.sourcemap,
inlineSourcesContent: true,
swcrc: false,
...(options?.swc ?? {}),
}
}
export function transformSync(source: string, path: string, options?: Options) {
return swcTransformSync(source, transformOption(path, options))
}
export function transformJest(source: string, path: string, options?: Options) {
return swcTransformSync(source, transformOption(path, options, true))
}
export function transform(source: string, path: string, options?: Options) {
return swcTransform(source, transformOption(path, options))
}