-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
element-plus.ts
213 lines (186 loc) · 5.86 KB
/
element-plus.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { compare } from 'compare-versions'
import type { ComponentInfo, ComponentResolver, SideEffectsInfo } from '../../types'
import { getPkgVersion, kebabCase } from '../utils'
export interface ElementPlusResolverOptions {
/**
* import style css or sass with components
*
* @default 'css'
*/
importStyle?: boolean | 'css' | 'sass'
/**
* use commonjs lib & source css or scss for ssr
*/
ssr?: boolean
/**
* specify element-plus version to load style
*
* @default installed version
*/
version?: string
/**
* auto import for directives
*
* @default true
*/
directives?: boolean
/**
* exclude component name, if match do not resolve the name
*/
exclude?: RegExp
/**
* a list of component names that have no styles, so resolving their styles file should be prevented
*/
noStylesComponents?: string[]
/**
* nightly version
*/
nightly?: boolean
}
type ElementPlusResolverOptionsResolved = Required<Omit<ElementPlusResolverOptions, 'exclude'>> &
Pick<ElementPlusResolverOptions, 'exclude'>
/**
* @deprecated
* @param partialName
* @param options
*/
function getSideEffectsLegacy(
partialName: string,
options: ElementPlusResolverOptionsResolved,
): SideEffectsInfo | undefined {
const { importStyle } = options
if (!importStyle)
return
if (importStyle === 'sass') {
return [
'element-plus/packages/theme-chalk/src/base.scss',
`element-plus/packages/theme-chalk/src/${partialName}.scss`,
]
}
else if (importStyle === true || importStyle === 'css') {
return [
'element-plus/lib/theme-chalk/base.css',
`element-plus/lib/theme-chalk/el-${partialName}.css`,
]
}
}
function getSideEffects(dirName: string, options: ElementPlusResolverOptionsResolved): SideEffectsInfo | undefined {
const { importStyle, ssr, nightly } = options
const themeFolder = nightly ? '@element-plus/nightly/theme-chalk' : 'element-plus/theme-chalk'
const esComponentsFolder = nightly ? '@element-plus/nightly/es/components' : 'element-plus/es/components'
if (importStyle === 'sass') {
return ssr
? [`${themeFolder}/src/base.scss`, `${themeFolder}/src/${dirName}.scss`]
: [`${esComponentsFolder}/base/style/index`, `${esComponentsFolder}/${dirName}/style/index`]
}
else if (importStyle === true || importStyle === 'css') {
return ssr
? [`${themeFolder}/base.css`, `${themeFolder}/el-${dirName}.css`]
: [`${esComponentsFolder}/base/style/css`, `${esComponentsFolder}/${dirName}/style/css`]
}
}
function resolveComponent(name: string, options: ElementPlusResolverOptionsResolved): ComponentInfo | undefined {
if (options.exclude && name.match(options.exclude))
return
if (!name.match(/^El[A-Z]/))
return
if (name.match(/^ElIcon.+/)) {
return {
name: name.replace(/^ElIcon/, ''),
from: '@element-plus/icons-vue',
}
}
const partialName = kebabCase(name.slice(2))// ElTableColumn -> table-column
const { version, ssr, nightly } = options
// >=1.1.0-beta.1
if (compare(version, '1.1.0-beta.1', '>=') || nightly) {
return {
name,
from: `${nightly ? '@element-plus/nightly' : 'element-plus'}/${ssr ? 'lib' : 'es'}`,
sideEffects: getSideEffects(partialName, options),
}
}
// >=1.0.2-beta.28
else if (compare(version, '1.0.2-beta.28', '>=')) {
return {
from: `element-plus/es/el-${partialName}`,
sideEffects: getSideEffectsLegacy(partialName, options),
}
}
// for <=1.0.1
else {
return {
from: `element-plus/lib/el-${partialName}`,
sideEffects: getSideEffectsLegacy(partialName, options),
}
}
}
function resolveDirective(name: string, options: ElementPlusResolverOptionsResolved): ComponentInfo | undefined {
if (!options.directives)
return
const directives: Record<string, { importName: string, styleName: string }> = {
Loading: { importName: 'ElLoadingDirective', styleName: 'loading' },
Popover: { importName: 'ElPopoverDirective', styleName: 'popover' },
InfiniteScroll: { importName: 'ElInfiniteScroll', styleName: 'infinite-scroll' },
}
const directive = directives[name]
if (!directive)
return
const { version, ssr, nightly } = options
// >=1.1.0-beta.1
if (compare(version, '1.1.0-beta.1', '>=') || nightly) {
return {
name: directive.importName,
from: `${nightly ? '@element-plus/nightly' : 'element-plus'}/${ssr ? 'lib' : 'es'}`,
sideEffects: getSideEffects(directive.styleName, options),
}
}
}
const noStylesComponents = ['ElAutoResizer']
/**
* Resolver for Element Plus
*
* See https://github.com/antfu/vite-plugin-components/pull/28 for more details
* See https://github.com/antfu/vite-plugin-components/issues/117 for more details
*
* @author @develar @nabaonan @sxzz
* @link https://element-plus.org/ for element-plus
*
*/
export function ElementPlusResolver(
options: ElementPlusResolverOptions = {},
): ComponentResolver[] {
let optionsResolved: ElementPlusResolverOptionsResolved
async function resolveOptions() {
if (optionsResolved)
return optionsResolved
optionsResolved = {
ssr: false,
version: await getPkgVersion('element-plus', '2.2.2'),
importStyle: 'css',
directives: true,
exclude: undefined,
noStylesComponents: options.noStylesComponents || [],
nightly: false,
...options,
}
return optionsResolved
}
return [
{
type: 'component',
resolve: async (name: string) => {
const options = await resolveOptions()
if ([...options.noStylesComponents, ...noStylesComponents].includes(name))
return resolveComponent(name, { ...options, importStyle: false })
else return resolveComponent(name, options)
},
},
{
type: 'directive',
resolve: async (name: string) => {
return resolveDirective(name, await resolveOptions())
},
},
]
}