forked from swc-project/swc-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
64 lines (59 loc) · 1.91 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
import {
transform as swcTransform,
transformSync as swcTransformSync,
Options as SwcOptions,
ReactConfig,
} from '@swc/core'
export interface Options {
target?: 'es3' | 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019'
module?: 'commonjs' | 'umd' | 'amd' | 'es6'
sourcemap?: boolean | 'inline'
jsx?: boolean
experimentalDecorators?: boolean
emitDecoratorMetadata?: boolean
dynamicImport?: boolean
esModuleInterop?: boolean
react?: Partial<ReactConfig>
}
function transformOption(path: string, options?: Options, jest = false): SwcOptions {
const opts = options == null ? {} : options
opts.esModuleInterop = opts.esModuleInterop ?? true
return {
filename: path,
jsc: {
target: opts.target ?? 'es2018',
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),
// @ts-expect-error
react: options?.react,
hidden: {
jest,
},
},
},
minify: false,
isModule: true,
module: {
type: 'commonjs',
noInterop: !opts.esModuleInterop,
},
sourceMaps: typeof opts.sourcemap === 'undefined' ? true : opts.sourcemap,
swcrc: false,
}
}
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))
}