Skip to content

Commit 275dae7

Browse files
committed
feat(varlet-ui):support auto import directives
1 parent 0069cf4 commit 275dae7

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

src/core/resolvers/varlet-ui.ts

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ComponentResolver } from '../../types'
1+
import { ComponentResolver, ComponentResolveResult } from '../../types'
22
import { kebabCase } from '../utils'
33

44
export interface VarletUIResolverOptions {
@@ -9,48 +9,69 @@ export interface VarletUIResolverOptions {
99
*/
1010
importStyle?: boolean | 'css' | 'less'
1111

12+
/**
13+
* auto import for directives
14+
*
15+
* @default true
16+
*/
17+
directives?: boolean
18+
1219
/**
1320
* @deprecated use `importStyle: 'css'` instead
1421
*/
1522
importCss?: boolean
23+
1624
/**
1725
* @deprecated use `importStyle: 'less'` instead
1826
*/
1927
importLess?: boolean
2028
}
2129

30+
export function getResolved(name: string, options: VarletUIResolverOptions): ComponentResolveResult {
31+
const {
32+
importStyle = 'css',
33+
importCss = true,
34+
importLess,
35+
} = options
36+
37+
const sideEffects = []
38+
39+
if (importStyle || importCss) {
40+
if (importStyle === 'less' || importLess)
41+
sideEffects.push(`@varlet/ui/es/${kebabCase(name)}/style/less.js`)
42+
else
43+
sideEffects.push(`@varlet/ui/es/${kebabCase(name)}/style`)
44+
}
45+
46+
return {
47+
path: '@varlet/ui',
48+
importName: `_${name}Component`,
49+
sideEffects,
50+
}
51+
}
52+
2253
/**
2354
* Resolver for VarletUI
2455
*
2556
* @link https://github.com/haoziqaq/varlet
2657
*/
27-
export function VarletUIResolver(options: VarletUIResolverOptions = {}): ComponentResolver {
28-
return {
29-
type: 'component',
30-
resolve: (name: string) => {
31-
const {
32-
importStyle = 'css',
33-
importCss = true,
34-
importLess,
35-
} = options
36-
37-
if (name.startsWith('Var')) {
38-
const partialName = name.slice(3)
39-
const sideEffects = []
40-
41-
if (importStyle || importCss) {
42-
if (importStyle === 'less' || importLess)
43-
sideEffects.push(`@varlet/ui/es/${kebabCase(partialName)}/style/less.js`)
44-
else
45-
sideEffects.push(`@varlet/ui/es/${kebabCase(partialName)}/style`)
46-
}
47-
48-
return {
49-
importName: `_${partialName}Component`,
50-
path: '@varlet/ui',
51-
sideEffects,
52-
}
53-
}
58+
export function VarletUIResolver(options: VarletUIResolverOptions = {}): ComponentResolver[] {
59+
return [
60+
{
61+
type: 'component',
62+
resolve: (name: string) => {
63+
if (name.startsWith('Var')) return getResolved(name.slice(3), options)
64+
},
5465
},
55-
}
66+
{
67+
type: 'directive',
68+
resolve: (name: string) => {
69+
const { directives = true } = options
70+
71+
if (!directives) return
72+
73+
return getResolved(name, options)
74+
},
75+
},
76+
]
5677
}

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