-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathelement-plus.ts
104 lines (95 loc) · 2.68 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
import gt from 'compare-versions'
import { 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'
/**
* specify element-plus version to load style
*
* @default installed version
*/
version?: string
}
/**
* @deprecated
* @param partialName
* @param options
*
* @returns
*/
function getSideEffectsLegacy(
partialName: string,
options: ElementPlusResolverOptions,
): SideEffectsInfo | undefined {
const { importStyle = 'css' } = 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: ElementPlusResolverOptions): SideEffectsInfo | undefined {
const { importStyle = 'css' } = options
if (importStyle === 'sass')
return `element-plus/es/components/${dirName}/style`
else if (importStyle === true || importStyle === 'css')
return `element-plus/es/components/${dirName}/style/css`
}
/**
* 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
* @link https://element-plus.org/#/en-US for element-plus
*
*/
export function ElementPlusResolver(
options: ElementPlusResolverOptions = {},
): ComponentResolver {
return (name: string) => {
if (name.match(/^El[A-Z]/)) {
const {
version = getPkgVersion('element-plus', '1.0.2'),
} = options
const partialName = kebabCase(name.slice(2))// ElTableColumn->table-column
// >=1.1.0-beta.1
if (gt(version, '1.1.0-beta.1')) {
return {
importName: name,
path: 'element-plus/es',
sideEffects: getSideEffects(partialName, options),
}
}
// >=1.0.2-beta.28
else if (gt(version, '1.0.2-beta.28')) {
return {
path: `element-plus/es/el-${partialName}`,
sideEffects: getSideEffectsLegacy(partialName, options),
}
}
// for <=1.0.1
else {
return {
path: `element-plus/lib/el-${partialName}`,
sideEffects: getSideEffectsLegacy(partialName, options),
}
}
}
}
}