forked from jpkleemans/vite-svg-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (25 loc) · 837 Bytes
/
index.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
const { extname } = require('path')
const fs = require('fs').promises
const { compileTemplate } = require('@vue/compiler-sfc')
const { optimize: optimizeSvg } = require('svgo')
module.exports = function svgLoader (options = {}) {
const { svgoConfig, svgo } = options
return {
name: 'svg-loader',
enforce: 'pre',
async load (id) {
const [path, parameter] = id.split('?')
if (!extname(path).startsWith('.svg') || parameter === 'url') {
return null
}
const svg = await fs.readFile(path, 'utf-8')
const optimizedSvg = svgo === false ? svg : optimizeSvg(svg, svgoConfig).data
const { code } = compileTemplate({
id: JSON.stringify(id),
source: optimizedSvg,
transformAssetUrls: false
})
return `${code}\nexport default render`
}
}
}