1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import { basename } from 'node:path'
- import { optimize } from 'svgo'
- import SVGCompiler from 'svg-baker'
- import { readFileSync } from 'node:fs'
- const optimizeSvg = (filepath) => {
-
- const content = readFileSync(filepath, 'utf-8')
- const result = optimize(content, {
- plugins: [{ name: 'preset-default' }],
- })
- return result.data || content
- }
- export default () => ({
- name: 'zammad-plugin-svgo',
- enforce: 'pre',
-
- async load(id) {
- if (id.endsWith('.svg?symbol')) {
- const filepath = id.replace(/\?.*$/, '')
- const svgContent = optimizeSvg(filepath)
- const compiler = new SVGCompiler()
- const name = basename(filepath).split('.')[0]
- const symbol = await compiler.addSymbol({
- id: `icon-${name}`,
- content: svgContent,
- path: filepath,
- })
- return {
- code: `export default \`${symbol.render()}\``,
- }
- }
- },
- })
|