Skip to content

Commit 10841a8

Browse files
authored
feat: add resolver for devui (#226)
1 parent 2a9a037 commit 10841a8

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/core/resolvers/devui.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { kebabCase } from '../utils'
2+
import type { ComponentResolver } from '../../types'
3+
4+
export interface DevResolverOptions {
5+
/**
6+
* bring in components and styles
7+
*
8+
* @default true
9+
*/
10+
importStyle?: boolean
11+
12+
/**
13+
* auto import for directives
14+
*
15+
* @default true
16+
*/
17+
directives?: boolean
18+
}
19+
20+
const LIB_NAME = 'vue-devui'
21+
22+
const findStyle = (name: string) => {
23+
if (!name || !Array.isArray(name)) return `${LIB_NAME}/${name}/style.css`
24+
}
25+
26+
const effectComponentMaps: Record<string, string> = {
27+
'row,col': 'grid',
28+
'aside,content,footer,header,layout': 'layout',
29+
'overlay,fixed-overlay,flexible-overlay': 'overlay',
30+
}
31+
32+
const effectDirectiveMaps: Record<string, string> = {
33+
// Directives exist, but style files are not required
34+
Ripple: '',
35+
Draggable: '',
36+
Droppable: '',
37+
38+
Loading: 'loading',
39+
ImagePreview: 'image-preview',
40+
}
41+
42+
const effectComponentKeys = Object.keys(effectComponentMaps)
43+
44+
// Gets the component style file
45+
function getSideEffects(name: string): string | undefined {
46+
const match = effectComponentKeys.find((key: string) => key.includes(name))
47+
return (match && effectComponentMaps[match]) && findStyle(match)
48+
}
49+
50+
function componentsResolver(name: string) {
51+
if (!name.match(/^D[A-Z]/)) return
52+
53+
// Alert => alert; DatePicker => date-picker
54+
const resolveId = kebabCase(name = name.slice(1))
55+
56+
return {
57+
path: LIB_NAME,
58+
importName: name,
59+
sideEffects: getSideEffects(resolveId),
60+
}
61+
}
62+
63+
function directivesResolver(name: string) {
64+
if (!(name in effectDirectiveMaps)) return
65+
66+
return {
67+
path: LIB_NAME,
68+
importName: `${name}Directive`,
69+
sideEffects: findStyle(effectDirectiveMaps[name]),
70+
}
71+
}
72+
73+
export function DevUiResolver(options: DevResolverOptions = {}): ComponentResolver[] {
74+
const config = { directives: true, importStyle: true, ...options }
75+
76+
const resolvers: ComponentResolver[] = [
77+
{ type: 'component', resolve: componentsResolver },
78+
]
79+
80+
if (config.directives)
81+
resolvers.push({ type: 'directive', resolve: directivesResolver })
82+
83+
return resolvers
84+
}

src/core/resolvers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './view-ui'
1212
export * from './vuetify'
1313
export * from './vueuse'
1414
export * from './quasar'
15+
export * from './devui'

0 commit comments

Comments
 (0)