Skip to content

Commit 78639c4

Browse files
committed
feat(element-plus): support auto import directives
1 parent 77940ba commit 78639c4

File tree

2 files changed

+88
-40
lines changed

2 files changed

+88
-40
lines changed

src/core/resolvers/element-plus.ts

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import compareVersions from 'compare-versions'
2-
import { ComponentResolver, SideEffectsInfo } from '../../types'
2+
import { ComponentInfo, ComponentResolver, SideEffectsInfo } from '../../types'
33
import { getPkgVersion, kebabCase } from '../utils'
44

55
export interface ElementPlusResolverOptions {
@@ -21,8 +21,17 @@ export interface ElementPlusResolverOptions {
2121
* @default installed version
2222
*/
2323
version?: string
24+
25+
/**
26+
* auto import for directives
27+
*
28+
* @default true
29+
*/
30+
directives?: boolean
2431
}
2532

33+
type ElementPlusResolverOptionsResolved = Required<ElementPlusResolverOptions>
34+
2635
/**
2736
* @deprecated
2837
* @param partialName
@@ -32,9 +41,9 @@ export interface ElementPlusResolverOptions {
3241
*/
3342
function getSideEffectsLegacy(
3443
partialName: string,
35-
options: ElementPlusResolverOptions,
44+
options: ElementPlusResolverOptionsResolved,
3645
): SideEffectsInfo | undefined {
37-
const { importStyle = 'css' } = options
46+
const { importStyle } = options
3847
if (!importStyle)
3948
return
4049

@@ -52,61 +61,99 @@ function getSideEffectsLegacy(
5261
}
5362
}
5463

55-
function getSideEffects(dirName: string, options: ElementPlusResolverOptions): SideEffectsInfo | undefined {
56-
const { importStyle = 'css', ssr } = options
64+
function getSideEffects(dirName: string, options: ElementPlusResolverOptionsResolved): SideEffectsInfo | undefined {
65+
const { importStyle, ssr } = options
5766
const themeFolder = 'element-plus/theme-chalk'
5867
const esComponentsFolder = 'element-plus/es/components'
5968

6069
if (importStyle === 'sass')
6170
return ssr ? `${themeFolder}/src/${dirName}.scss` : `${esComponentsFolder}/${dirName}/style/index`
62-
6371
else if (importStyle === true || importStyle === 'css')
6472
return ssr ? `${themeFolder}/el-${dirName}.css` : `${esComponentsFolder}/${dirName}/style/css`
6573
}
6674

75+
function resolveComponent(name: string, options: ElementPlusResolverOptionsResolved): ComponentInfo | undefined {
76+
if (!name.match(/^El[A-Z]/))
77+
return
78+
79+
const partialName = kebabCase(name.slice(2))// ElTableColumn -> table-column
80+
const { version, ssr } = options
81+
82+
// >=1.1.0-beta.1
83+
if (compareVersions.compare(version, '1.1.0-beta.1', '>=')) {
84+
return {
85+
importName: name,
86+
path: `element-plus/${ssr ? 'lib' : 'es'}`,
87+
sideEffects: getSideEffects(partialName, options),
88+
}
89+
}
90+
// >=1.0.2-beta.28
91+
else if (compareVersions.compare(version, '1.0.2-beta.28', '>=')) {
92+
return {
93+
path: `element-plus/es/el-${partialName}`,
94+
sideEffects: getSideEffectsLegacy(partialName, options),
95+
}
96+
}
97+
// for <=1.0.1
98+
else {
99+
return {
100+
path: `element-plus/lib/el-${partialName}`,
101+
sideEffects: getSideEffectsLegacy(partialName, options),
102+
}
103+
}
104+
}
105+
106+
function resolveDirective(name: string, options: ElementPlusResolverOptionsResolved): ComponentInfo | undefined {
107+
if (!options.directives) return
108+
109+
const directives: Record<string, { importName: string; styleName: string}> = {
110+
Loading: { importName: 'ElLoadingDirective', styleName: 'loading' },
111+
Popover: { importName: 'ElPopoverDirective', styleName: 'popover' },
112+
InfiniteScroll: { importName: 'ElInfiniteScroll', styleName: 'infinite-scroll' },
113+
}
114+
115+
const directive = directives[name]
116+
if (!directive) return
117+
118+
const { version, ssr } = options
119+
120+
// >=1.1.0-beta.1
121+
if (compareVersions.compare(version, '1.1.0-beta.1', '>=')) {
122+
return {
123+
importName: directive.importName,
124+
path: `element-plus/${ssr ? 'lib' : 'es'}`,
125+
sideEffects: getSideEffects(directive.styleName, options),
126+
}
127+
}
128+
}
129+
67130
/**
68131
* Resolver for Element Plus
69132
*
70133
* See https://github.com/antfu/vite-plugin-components/pull/28 for more details
71134
* See https://github.com/antfu/vite-plugin-components/issues/117 for more details
72135
*
73-
* @author @develar @nabaonan
74-
* @link https://element-plus.org/#/en-US for element-plus
136+
* @author @develar @nabaonan @sxzz
137+
* @link https://element-plus.org/ for element-plus
75138
*
76139
*/
77140
export function ElementPlusResolver(
78141
options: ElementPlusResolverOptions = {},
79142
): ComponentResolver {
80-
return (name: string) => {
81-
if (name.match(/^El[A-Z]/)) {
82-
const {
83-
ssr,
84-
version = getPkgVersion('element-plus', '1.0.2'),
85-
} = options
86-
const partialName = kebabCase(name.slice(2))// ElTableColumn->table-column
87-
88-
// >=1.1.0-beta.1
89-
if (compareVersions.compare(version, '1.1.0-beta.1', '>=')) {
90-
return {
91-
importName: name,
92-
path: `element-plus/${ssr ? 'lib' : 'es'}`,
93-
sideEffects: getSideEffects(partialName, options),
94-
}
95-
}
96-
// >=1.0.2-beta.28
97-
else if (compareVersions.compare(version, '1.0.2-beta.28', '>=')) {
98-
return {
99-
path: `element-plus/es/el-${partialName}`,
100-
sideEffects: getSideEffectsLegacy(partialName, options),
101-
}
102-
}
103-
// for <=1.0.1
104-
else {
105-
return {
106-
path: `element-plus/lib/el-${partialName}`,
107-
sideEffects: getSideEffectsLegacy(partialName, options),
108-
}
109-
}
143+
const optionsResolved: ElementPlusResolverOptionsResolved = {
144+
ssr: false,
145+
version: getPkgVersion('element-plus', '1.1.0-beta.21'),
146+
importStyle: 'css',
147+
directives: true,
148+
...options,
149+
}
150+
151+
return (name: string, type) => {
152+
switch (type) {
153+
case 'component':
154+
return resolveComponent(name, optionsResolved)
155+
case 'directive':
156+
return resolveDirective(name, optionsResolved)
110157
}
111158
}
112159
}

test/resolvers/__snapshots__/element-plus.test.ts.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
exports[`Element Plus Resolver components and directives should be transformed 1`] = `
44
Object {
5-
"code": "/* unplugin-vue-components disabled */import { ElButton as __unplugin_components_0 } from 'element-plus/es';import 'element-plus/es/components/button/style/css';
5+
"code": "/* unplugin-vue-components disabled */import { ElLoadingDirective as __unplugin_directives_0 } from 'element-plus/es';import 'element-plus/es/components/loading/style/css';
6+
import { ElButton as __unplugin_components_0 } from 'element-plus/es';import 'element-plus/es/components/button/style/css';
67
78
(_ctx, _cache) => {
89
const _component_el_button = __unplugin_components_0
9-
const _directive_loading = _resolveDirective(\\"loading\\")
10+
const _directive_loading = __unplugin_directives_0
1011
1112
return (_openBlock(), _createElementBlock(_Fragment, null, [
1213
_createVNode(_component_el_button, null, {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy