Skip to content

feat: add resolver for devui #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/core/resolvers/devui.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { kebabCase } from '../utils'
import type { ComponentResolver } from '../../types'

export interface DevResolverOptions {
/**
* bring in components and styles
*
* @default true
*/
importStyle?: boolean

/**
* auto import for directives
*
* @default true
*/
directives?: boolean
}

const LIB_NAME = 'vue-devui'

const findStyle = (name: string) => {
if (!name || !Array.isArray(name)) return `${LIB_NAME}/${name}/style.css`
}

const effectComponentMaps: Record<string, string> = {
'row,col': 'grid',
'aside,content,footer,header,layout': 'layout',
'overlay,fixed-overlay,flexible-overlay': 'overlay',
}

const effectDirectiveMaps: Record<string, string> = {
// Directives exist, but style files are not required
Ripple: '',
Draggable: '',
Droppable: '',

Loading: 'loading',
ImagePreview: 'image-preview',
}

const effectComponentKeys = Object.keys(effectComponentMaps)

// Gets the component style file
function getSideEffects(name: string): string | undefined {
const match = effectComponentKeys.find((key: string) => key.includes(name))
return (match && effectComponentMaps[match]) && findStyle(match)
}

function componentsResolver(name: string) {
if (!name.match(/^D[A-Z]/)) return

// Alert => alert; DatePicker => date-picker
const resolveId = kebabCase(name = name.slice(1))

return {
path: LIB_NAME,
importName: name,
sideEffects: getSideEffects(resolveId),
}
}

function directivesResolver(name: string) {
if (!(name in effectDirectiveMaps)) return

return {
path: LIB_NAME,
importName: `${name}Directive`,
sideEffects: findStyle(effectDirectiveMaps[name]),
}
}

export function DevUiResolver(options: DevResolverOptions = {}): ComponentResolver[] {
const config = { directives: true, importStyle: true, ...options }

const resolvers: ComponentResolver[] = [
{ type: 'component', resolve: componentsResolver },
]

if (config.directives)
resolvers.push({ type: 'directive', resolve: directivesResolver })

return resolvers
}
1 change: 1 addition & 0 deletions src/core/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './view-ui'
export * from './vuetify'
export * from './vueuse'
export * from './quasar'
export * from './devui'