Skip to content

Commit

Permalink
feat: add optimizeOptions option
Browse files Browse the repository at this point in the history
  • Loading branch information
Jevon617 committed Oct 27, 2022
1 parent 7454a0c commit 258d38e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ UnpluginSvgComponent({
nameArr.unshift(prefix)
return nameArr.join('-').replace(/\.svg$/, '')
}, // default, format symbolId
optimizeOptions: undefined // default, svgo optimize options
})
```

Expand Down
3 changes: 2 additions & 1 deletion README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ UnpluginSvgComponent({
componentStyle: 'width: 1em; height: 1em; fill:currentColor;', // 默认值
// 通常, 插件会把svg文件的fill, stroke属性替换为'currentColor', 使用这个属性可以让插件保留svg原来的颜色
preserveColor: /logo\.svg$/,
prefix: '',
prefix: '', // 默认值
symbolIdFormatter: (svgName: string, prefix: string): string => {
const nameArr = svgName.split('/')
if (prefix)
nameArr.unshift(prefix)
return nameArr.join('-').replace(/\.svg$/, '')
}, // 默认值, 自定义symbolId的格式
optimizeOptions: undefined // 默认值, svgo 优化配置
})
```

Expand Down
1 change: 1 addition & 0 deletions examples/vite/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default defineConfig({
nameArr.unshift(prefix)
return nameArr.join('-').replace(/\.svg$/, '')
},
optimizeOptions: undefined,
}),
],
})
10 changes: 5 additions & 5 deletions src/core/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs/promises'
import fg from 'fast-glob'
import { optimize } from 'svgo'
import SvgCompiler from 'svg-baker'
import type { OptimizedSvg } from 'svgo'
import type { OptimizeOptions, OptimizedSvg } from 'svgo'
import type { Options } from '../types'

export default async function createSvgSprite(options: Options) {
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function createSymbol(
}>,
svgCompiler,
) {
const { iconDir, prefix = '', preserveColor, symbolIdFormatter } = options
const { iconDir, prefix = '', preserveColor, symbolIdFormatter, optimizeOptions } = options

const svgPath = path.resolve(iconDir, svgName)
const svgContent = await fs.readFile(svgPath)
Expand All @@ -49,7 +49,7 @@ export async function createSymbol(
else if (typeof preserveColor === 'object')
isPreserveColor = preserveColor.test(svgPath)

const OptimizedSvgContent = await optimizeSvg(svgContent, isPreserveColor)
const OptimizedSvgContent = await optimizeSvg(svgContent, isPreserveColor, optimizeOptions)
const svgSymbol = (await svgCompiler.addSymbol({
path: svgPath,
content: OptimizedSvgContent,
Expand All @@ -62,8 +62,8 @@ export async function createSymbol(
}
}

async function optimizeSvg(source: Buffer, preserveColor: boolean) {
const { data: optimizedSvgContent } = await optimize(source) as OptimizedSvg
async function optimizeSvg(source: Buffer, preserveColor: boolean, optimizeOptions?: OptimizeOptions) {
const { data: optimizedSvgContent } = await optimize(source, optimizeOptions) as OptimizedSvg
if (preserveColor) {
return optimizedSvgContent
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { OptimizeOptions } from 'svgo'

export interface Options {
iconDir: string
prefix?: string
Expand All @@ -8,5 +10,6 @@ export interface Options {
preserveColor?: string | RegExp
componentStyle?: string
symbolIdFormatter?: (name: string, prefix: string) => string
optimizeOptions?: OptimizeOptions
}

0 comments on commit 258d38e

Please sign in to comment.