forked from vue-macros/vue-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (44 loc) · 1.26 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
import { createUnplugin } from 'unplugin'
import {
REGEX_SETUP_SFC,
REGEX_VUE_SFC,
REGEX_VUE_SUB,
createFilter,
detectVueVersion,
} from '@vue-macros/common'
import { type BaseOptions, type MarkRequired } from '@vue-macros/common'
import { type UnpluginContextMeta } from 'unplugin'
import { transformDefineOptions } from './core/transform'
export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
function resolveOption(
options: Options,
framework: UnpluginContextMeta['framework']
): OptionsResolved {
const version = options.version || detectVueVersion()
return {
include: [REGEX_VUE_SFC, REGEX_SETUP_SFC].concat(
framework === 'webpack' ? REGEX_VUE_SUB : []
),
...options,
version,
}
}
const name = 'unplugin-vue-define-options'
export default createUnplugin<Options | undefined, false>(
(userOptions = {}, { framework }) => {
const options = resolveOption(userOptions, framework)
const filter = createFilter(options)
return {
name,
enforce: 'pre',
transformInclude(id) {
return filter(id)
},
transform(code, id) {
return transformDefineOptions(code, id)
},
}
}
)
export { transformDefineOptions as transform }