Skip to content

Commit

Permalink
fix: when componentName is kebabCase, the svg will be shaked
Browse files Browse the repository at this point in the history
  • Loading branch information
Jevon617 committed May 29, 2023
1 parent febf086 commit cc95fa3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 8 additions & 2 deletions examples/vue3-vite/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@

<template>
<div>
<MySvgIcon name="icon-common-logo" style="margin-right: 20px;" />
<MySvgIcon name="icon-icon-addUser" style="margin-right: 20px;" />
<MySvgIcon
name="icon-common-logo"
style="margin-right: 20px;"
/>
<my-svg-icon
name="icon-icon-addUser"
style="margin-right: 20px;"
/>
<MySvgIcon name="icon-icon-card2" style="margin-right: 20px;" />
</div>
</template>
Expand Down
5 changes: 3 additions & 2 deletions src/core/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs/promises'
import path from 'node:path'
import fg from 'fast-glob'
import type { Options } from '../types'
import { tranfromToKebabCase } from './utils'

export default async function scanUsedSvgNames(options: Options) {
const { componentName, scanGlob, iconDir, scanStrategy, symbolIdFormatter, prefix } = options
Expand All @@ -27,10 +28,10 @@ export default async function scanUsedSvgNames(options: Options) {

if (scanStrategy === 'component') {
const svgCompnentRE = new RegExp(
`\\<\\s*${componentName}[^\\<]+name=[\\"\\'](\\S+)[\\"\\'][^\\<]*\\/\\>`, 'g',
`\\<\\s*(${tranfromToKebabCase(componentName!)}|${componentName})[^\\<]+name=[\\"\\'](\\S+)[\\"\\'][^\\<]*\\/\\>`, 'g',
)
svgNameMatches = code.map((c) => {
return Array.from(c.matchAll(svgCompnentRE)).map(match => match[1])
return Array.from(c.matchAll(svgCompnentRE)).map(match => match[2])
})
}
else {
Expand Down
13 changes: 13 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,16 @@ export function transformStyleStrToObject(styleStr: string): Record<string, stri
return ruleMap
}, {})
}

export function tranfromToKebabCase(str: string) {
const arr = str.trim().split('')
const result = arr.map((item, index) => {
if (index === 0)
return `${item.toLowerCase()}`
else if (item.toUpperCase() === item)
return `-${item.toLowerCase()}`
else
return item
}).join('')
return result
}

0 comments on commit cc95fa3

Please sign in to comment.