-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
44 lines (39 loc) · 1.24 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
import * as parser from '@babel/parser'
import _generator from '@babel/generator'
import logger from './log'
import saveFiles from './utils'
import type { Config } from './config'
import { getTransformers } from './config'
// @ts-expect-error: https://github.com/babel/babel/issues/15269
const generator = _generator.default as typeof _generator
export default (config?: Config) => {
const userConfig: Config = config || {
components: {},
}
if (userConfig.silent === true)
logger.print = () => {}
return {
name: 'partial-evaluate-optimizer-plugin',
async transform(code: string, id: string) {
const transformers = getTransformers(id, userConfig)
if (transformers.length === 0)
return null
const ast = parser.parse(code, { sourceType: 'module' })
for (const transformer of transformers)
transformer(ast, id, userConfig)
const { code: transformedCode } = generator(ast, { retainLines: true })
const log = saveFiles(id, code, transformedCode)
logger.log(`${log}\n`)
return code === transformedCode
? null
: {
code: transformedCode,
map: null,
}
},
closeBundle() {
logger.print()
logger.clear()
},
}
}