forked from egoist/svg-to-vue-component
/
loader.js
71 lines (59 loc) · 1.63 KB
/
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
const path = require('path')
const { promisify } = require('util')
const fs = require('fs')
const { optimize, loadConfig } = require('svgo')
const { getOptions } = require('loader-utils')
const merge = require('merge-deep')
const JoyCon = require('joycon').default
const toSFC = require('.')
module.exports = async function(source) {
this.cacheable()
const cb = this.async()
const { svgoConfig } = getOptions(this) || {}
try {
if (this.issuer && this.issuer.endsWith('.css')) {
throw new Error(
`Please configure another loader to handle .svg files imported in .css files\nSee more: https://github.com/egoist/svg-to-vue-component#usage`
)
}
if (svgoConfig !== false) {
const options = await getSvgoConfig(svgoConfig, path.dirname(this.resourcePath))
const result = optimize(source, Object.assign(
{
path: this.resourcePath
},
options
));
source = result.data
}
const { component } = await toSFC(source)
cb(null, component)
} catch (err) {
cb(err)
}
}
async function getSvgoConfig(svgoConfig, cwd) {
return merge(
{ plugins: ['prefixIds'] },
svgoConfig,
await loadSvgoConfig(cwd)
)
}
function loadSvgoConfig(cwd) {
const joycon = new JoyCon()
const readFile = promisify(fs.readFile)
joycon.addLoader({
test: /\.yml$/,
async load(filepath) {
const content = await readFile(filepath, 'utf8')
return require('js-yaml').safeLoad(content)
}
})
return joycon
.load(
['.svgo.yml', '.svgo.js', '.svgo.json'],
cwd,
path.dirname(process.cwd())
)
.then(res => res.data)
}