-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (46 loc) · 1.36 KB
/
Copy pathindex.js
File metadata and controls
52 lines (46 loc) · 1.36 KB
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
const fs = require('fs')
const path = require('path')
const { optimize } = require('svgo')
const SVGCompiler = require('svg-baker')
const { defaultPlugins } = require('svgo/lib/svgo/config')
module.exports = function svgSymbolLoader (options = {}) {
const { svgo, id } = options
const inner = [
{
name: "removeAttrs",
params: {
attrs: "(class)"
}
},
'removeXMLNS'
]
const svgoConfig = [...new Set(defaultPlugins.concat(inner, Array.isArray(svgo) ? svgo : []))]
const svgRegex = /\.svg(\?(raw|symbol))?$/
return {
name: 'svg-symbol',
enforce: 'pre',
// resolveid (url) {
// return url.match(svgRegex) ? url : null
// },
async load(url) {
if (!url.match(svgRegex)) return
const [filePath, query] = url.split('?', 2)
let raw = await fs.promises.readFile(filePath, 'utf-8')
if (svgo !== false) {
const { data } = await optimize(raw, {plugins: svgoConfig});
raw = data;
}
if (!query || query === 'symbol') {
const { name } = path.parse(filePath)
const svgSymbol = await new SVGCompiler().addSymbol({
id: id ? id.replace(/\[name\]/g, name) : name,
content: raw,
path: filePath,
});
raw = svgSymbol.render()
}
return `export default ${JSON.stringify(raw)}`
}
}
}
module.exports.default = module.exports