jingrow-ui/vite/lucideIcons.js
jingrow c7bac1a7a0
Some checks failed
Publish on NPM / publish (push) Has been cancelled
Build and Deploy Storybook / build (push) Has been cancelled
Tests / test (push) Has been cancelled
initial commit
2025-10-24 00:40:30 +08:00

69 lines
1.7 KiB
JavaScript

import * as LucideIcons from 'lucide-static'
import AutoImport from 'unplugin-auto-import/vite'
import Icons from 'unplugin-icons/vite'
import Components from 'unplugin-vue-components/vite'
import IconsResolver from 'unplugin-icons/resolver'
export function lucideIcons() {
const resolverObj = {
resolvers: [
IconsResolver({
prefix: false,
enabledCollections: ['lucide'],
}),
],
}
return [
AutoImport(resolverObj),
Components(resolverObj),
Icons({
customCollections: {
lucide: getIcons(),
},
}),
]
}
function getIcons() {
let icons = {}
for (const icon in LucideIcons) {
if (icon === 'default') {
continue
}
let iconSvg = LucideIcons[icon]
// set stroke-width to 1.5
if (typeof iconSvg === 'string' && iconSvg.includes('stroke-width')) {
iconSvg = iconSvg.replace(/stroke-width="2"/g, 'stroke-width="1.5"')
}
icons[icon] = iconSvg
let dashKeys = camelToDash(icon)
for (let dashKey of dashKeys) {
if (dashKey !== icon) {
icons[dashKey] = iconSvg
}
}
}
return icons
}
function camelToDash(key) {
// barChart2 -> bar-chart-2
let withNumber = key.replace(/[A-Z0-9]/g, (m) => '-' + m.toLowerCase())
if (withNumber.startsWith('-')) {
withNumber = withNumber.substring(1)
}
// barChart2 -> bar-chart2
let withoutNumber = key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())
if (withoutNumber.startsWith('-')) {
withoutNumber = withoutNumber.substring(1)
}
if (withNumber !== withoutNumber) {
// both are required because unplugin icon resolver doesn't put a dash before numbers
return [withNumber, withoutNumber]
}
return [withNumber]
}