-
Notifications
You must be signed in to change notification settings - Fork 3
/
svg-loader.js
71 lines (62 loc) · 1.73 KB
/
svg-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Slightly updated version of vite-plugin-vue2-svg plugin.
// For Vue 3, consider using vite-svg-loader
import { readFileSync } from 'fs'
import { basename } from 'path'
import { optimize } from 'svgo'
import { compileTemplate, parse } from '@vue/component-compiler-utils'
import * as compiler from 'vue-template-compiler'
function compileSvg(svg, id) {
const template = parse({
source: `
<template>
${svg}
</template>
`,
compiler: compiler,
filename: `${basename(id)}.vue`
}).template
if (!template) return ''
const result = compileTemplate({
compiler: compiler,
source: template.content,
filename: `${basename(id)}.vue`
})
return `
${result.code}
export default {
render: render
}
`
}
function optimizeSvg(content, svgoConfig) {
const result = optimize(content, svgoConfig)
if ('data' in result) {
return result.data
}
throw new Error(`[vite-plugin-vue2-svg] cannot optimize SVG ${svgoConfig.path}`)
}
export default function (options = {}) {
const { svgoConfig } = options
const svgRegex = /\.svg(\?(component))?$/
return {
name: 'vite-svg-loader',
async transform(_source, id) {
if (!/\?component/.test(id)) {
return null
}
const fname = id.replace(/\?.*$/, '')
const isMatch = svgRegex.test(fname)
if (isMatch) {
const code = readFileSync(fname, { encoding: 'utf-8' })
let svg = await optimizeSvg(code, { path: fname, ...svgoConfig })
if (!svg) {
throw new Error(`[vite-svg-loader] fail to compile ${id}`)
}
svg = svg.replace('<svg', '<svg v-on="$listeners"')
const result = compileSvg(svg, fname)
return result
}
return null
}
}
}