You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
67 lines
2.1 KiB
const noStylesComponents: any[] = []
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export default (options: any = {}): any => {
|
|
let optionsResolved: any
|
|
|
|
async function resolveOptions() {
|
|
if (optionsResolved) return optionsResolved
|
|
optionsResolved = {
|
|
exclude: undefined,
|
|
noStylesComponents: options.noStylesComponents || [],
|
|
...options
|
|
}
|
|
return optionsResolved
|
|
}
|
|
return {
|
|
type: 'component',
|
|
resolve: async (name: string) => {
|
|
const options = await resolveOptions()
|
|
|
|
if ([...options.noStylesComponents, ...noStylesComponents].includes(name))
|
|
return resolveComponent(name, { ...options, importStyle: false })
|
|
else return resolveComponent(name, options)
|
|
}
|
|
}
|
|
}
|
|
|
|
// function kebabCase(key: string) {
|
|
// const result = key.replace(/([A-Z])/g, ' $1').trim()
|
|
// return result.split(' ').join('-').toLowerCase()
|
|
// }
|
|
|
|
function pascalCase(key: string): string {
|
|
// 第一步:将所有分隔符(-、_、空格)替换为空格,统一处理
|
|
const replaced = key.replace(/[-_\s]/g, ' ')
|
|
// 第二步:处理可能的连续大写字母(如HTML),在大写字母前加空格(除了开头)
|
|
const spaced = replaced.replace(/([A-Z])/g, (_, p1, index) => {
|
|
return index === 0 ? p1 : ` ${p1}`
|
|
})
|
|
// 第三步:去除首尾空格,并按空格拆分为单词数组
|
|
const words = spaced.trim().split(/\s+/)
|
|
// 第四步:每个单词首字母大写,其余小写,然后拼接
|
|
return words
|
|
.map((word) => {
|
|
if (word.length === 0) return ''
|
|
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
|
|
})
|
|
.join('')
|
|
}
|
|
|
|
function resolveComponent(name: string, options: any) {
|
|
if (options.exclude && name.match(options.exclude)) return
|
|
|
|
if (!name.match(/^Bo[A-Z]/)) return
|
|
|
|
const partialName = pascalCase(name.slice(2))
|
|
return {
|
|
name,
|
|
from: `${'bolt-ui'}`,
|
|
sideEffects: getSideEffects(partialName)
|
|
}
|
|
}
|
|
|
|
function getSideEffects(dirName: string) {
|
|
const componentsFolder = 'bolt-ui/components'
|
|
return [`${componentsFolder}/${dirName}/style/index`, 'bolt-ui/theme-chalk/src/theme/index.scss']
|
|
}
|
|
|