-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
swc.ts
56 lines (48 loc) · 1.36 KB
/
swc.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
/**
* Use SWC to emit decorator metadata
*/
import { JscConfig } from '@swc/core'
import { Plugin } from 'esbuild'
import { Logger } from '../log'
import { localRequire } from '../utils'
export const swcPlugin = ({ logger }: { logger: Logger }): Plugin => {
return {
name: 'swc',
setup(build) {
const swc: typeof import('@swc/core') = localRequire('@swc/core')
if (!swc) {
logger.warn(
build.initialOptions.format!,
`You have emitDecoratorMetadata enabled but @swc/core was not installed, skipping swc plugin`
)
return
}
// Force esbuild to keep class names as well
build.initialOptions.keepNames = true
build.onLoad({ filter: /\.[jt]sx?$/ }, async (args) => {
const isTs = /\.tsx?$/.test(args.path)
const jsc: JscConfig = {
parser: {
syntax: isTs ? 'typescript' : 'ecmascript',
decorators: true,
},
transform: {
legacyDecorator: true,
decoratorMetadata: true,
},
keepClassNames: true,
target: 'es2022',
}
const result = await swc.transformFile(args.path, {
jsc,
sourceMaps: 'inline',
configFile: false,
swcrc: false,
})
return {
contents: result.code,
}
})
},
}
}