-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
es5.ts
52 lines (47 loc) · 1.28 KB
/
es5.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
import { PrettyError } from '../errors'
import { Plugin } from '../plugin'
import { localRequire } from '../utils'
export const es5 = (): Plugin => {
let enabled = false
return {
name: 'es5-target',
esbuildOptions(options) {
if (options.target === 'es5') {
options.target = 'es2020'
enabled = true
}
},
async renderChunk(code, info) {
if (!enabled || !/\.(cjs|js)$/.test(info.path)) {
return
}
const swc: typeof import('@swc/core') = localRequire('@swc/core')
if (!swc) {
throw new PrettyError(
'@swc/core is required for es5 target. Please install it with `npm install @swc/core -D`'
)
}
const result = await swc.transform(code, {
filename: info.path,
sourceMaps: this.options.sourcemap,
minify: Boolean(this.options.minify),
jsc: {
target: 'es5',
parser: {
syntax: 'ecmascript',
},
minify: this.options.minify === true ? {
compress: false,
mangle: {
reserved: this.options.globalName ? [this.options.globalName] : []
},
} : undefined,
},
})
return {
code: result.code,
map: result.map,
}
},
}
}