-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
varlet-ui.ts
117 lines (99 loc) · 2.54 KB
/
varlet-ui.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type { ComponentResolveResult, ComponentResolver } from '../../types'
import { kebabCase } from '../utils'
export interface VarletUIResolverOptions {
/**
* support vue version
* vue3 use @varlet/ui, vue2 use @varlet-vue2/ui
*
* @default 'vue3'
*/
version?: 'vue3' | 'vue2'
/**
* import style along with components
*
* @default 'css'
*/
importStyle?: boolean | 'css' | 'less'
/**
* style entry file extname
*
* @default '.mjs'
*/
styleExtname?: string
/**
* auto import for directives
*
* @default true
*/
directives?: boolean
/**
* compatible with unplugin-auto-import
*
* @default false
*/
autoImport?: boolean
/**
* @deprecated use `importStyle: 'css'` instead
*/
importCss?: boolean
/**
* @deprecated use `importStyle: 'less'` instead
*/
importLess?: boolean
}
const varFunctions = ['ImagePreview', 'Snackbar', 'Picker', 'ActionSheet', 'Dialog', 'Locale', 'StyleProvider', 'LoadingBar']
const varDirectives = ['Ripple', 'Lazy', 'Hover']
export function getResolved(name: string, options: VarletUIResolverOptions): ComponentResolveResult {
const {
importStyle = 'css',
importCss = true,
importLess,
styleExtname = '.mjs',
autoImport = false,
version = 'vue3',
} = options
const path = version === 'vue2' ? '@varlet-vue2/ui' : '@varlet/ui'
const sideEffects = []
if (importStyle || importCss) {
if (importStyle === 'less' || importLess)
sideEffects.push(`${path}/es/${kebabCase(name)}/style/less`)
else
sideEffects.push(`${path}/es/${kebabCase(name)}/style/index${styleExtname}`)
}
return {
from: path,
name: autoImport ? name : `_${name}Component`,
sideEffects,
}
}
/**
* Resolver for VarletUI
*
* @link https://github.com/varletjs/varlet
* @link https://github.com/varletjs/varlet-vue2
*/
export function VarletUIResolver(options: VarletUIResolverOptions = {}): ComponentResolver[] {
return [
{
type: 'component',
resolve: (name: string) => {
const { autoImport = false } = options
if (autoImport && varFunctions.includes(name))
return getResolved(name, options)
if (name.startsWith('Var'))
return getResolved(name.slice(3), options)
},
},
{
type: 'directive',
resolve: (name: string) => {
const { directives = true } = options
if (!directives)
return
if (!varDirectives.includes(name))
return
return getResolved(name, options)
},
},
]
}