-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
veui.ts
154 lines (130 loc) · 3.38 KB
/
veui.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { join, normalize } from 'node:path'
import { resolvePathSync } from 'mlly'
import type { ComponentResolver, SideEffectsInfo } from '../../types'
import { camelCase, kebabCase, pascalCase } from '../utils'
interface VeuiPeerConfig {
/**
* The package name of the peer module.
*/
package: string
/**
* The directory path of the peer module.
* @default 'components'
*/
path?: string
/**
* The file name template for the peer module.
* @default '{module}.css'
*/
fileName?: `${string}{module}${string}`
/**
* The text transform to be applied to the '{module}' part of the file name.
* @default 'kebab-case'
*/
transform?: 'kebab-case' | 'camelCase' | 'PascalCase' | false
}
type SupportedLocale = 'en-US' | 'zh-Hans'
export interface VeuiResolverOptions {
/**
* The alias of 'veui` package.
* @default 'veui'
*/
alias?: string
/**
* Peer modules to be injected.
*/
modules?: VeuiPeerConfig[]
/**
* Locale modules to be injected.
* @default 'zh-Hans'
*/
locale?: SupportedLocale | SupportedLocale[] | false
/**
* Global modules to be injected to all components.
* @default []
*/
global?: string[]
}
interface ComponentInfo {
name: string
path: string
}
const VEUI_PACKAGE_NAME = 'veui'
let components: Set<string> | undefined
/**
* Resolver for VEUI
*
* @link https://github.com/ecomfe/veui
*/
export function VeuiResolver(options: VeuiResolverOptions = {}): ComponentResolver {
const { alias = VEUI_PACKAGE_NAME } = options
if (!components) {
try {
// eslint-disable-next-line ts/no-require-imports
const componentsData = require(`${alias}/components.json`) as ComponentInfo[]
components = new Set(componentsData.map(({ name }) => name))
}
catch {
throw new Error('[unplugin-vue-components:veui] VEUI is not installed')
}
}
return {
type: 'component',
resolve: (name: string) => {
if (name.match(/^Veui[A-Z]/)) {
const componentName = name.slice(4)
if (!components!.has(componentName))
return
const sideEffects = getSideEffects(componentName, options)
return { name: componentName, from: alias, sideEffects }
}
},
}
}
const formatters = {
'kebab-case': kebabCase,
'camelCase': camelCase,
'PascalCase': pascalCase,
}
const peerPaths = new Map<string, boolean>()
function assertPeerPath(peerPath: string) {
if (!peerPaths.has(peerPath)) {
try {
resolvePathSync(peerPath)
peerPaths.set(peerPath, true)
}
catch {
peerPaths.set(peerPath, false)
}
}
return peerPaths.get(peerPath) as boolean
}
function getSideEffects(
name: string,
{
alias = VEUI_PACKAGE_NAME,
modules = [],
locale = 'zh-Hans',
global = [],
}: VeuiResolverOptions,
): SideEffectsInfo {
const localeModules = (locale
? Array.isArray(locale)
? locale
: [locale]
: []
).map(locale => `${alias}/locale/${locale}/${name}.js`)
const peerModules = modules.map(
({
package: pack,
path = 'components',
fileName = '{module}.css',
transform = 'kebab-case',
}) => {
const peer = transform ? formatters[transform](name) : name
const file = fileName.replace(/\$?\{module\}/g, peer)
return normalize(join(pack, path, file))
},
)
return [...localeModules, ...global, ...peerModules].filter(assertPeerPath)
}