|
1 | | -const SVGO = require('svgo'); |
| 1 | +const svgToVue = require('svg-to-vue'); |
2 | 2 | const { getOptions } = require('loader-utils'); |
3 | | -const { compile } = require('vue-template-compiler'); |
4 | | -const stripWith = require('vue-template-es2015-compiler'); |
5 | | - |
6 | | -function getSvg(content, path, svgoConfig) { |
7 | | - if (svgoConfig === false) { |
8 | | - return Promise.resolve(content); |
9 | | - } |
10 | | - |
11 | | - return new SVGO(svgoConfig) |
12 | | - .optimize(content, { path }) |
13 | | - .then(result => result.data); |
14 | | -}; |
15 | 3 |
|
16 | 4 | module.exports = function (content) { |
17 | | - const path = this.resourcePath; |
18 | 5 | const callback = this.async(); |
19 | | - const options = getOptions(this) || {}; |
20 | | - |
21 | | - const { |
22 | | - svgo: svgoConfig = {}, |
23 | | - functional = false, |
24 | | - } = options; |
25 | | - |
26 | | - getSvg(content, path, svgoConfig) |
27 | | - .then((result) => { |
28 | | - let { render: renderFn } = compile(result, { |
29 | | - preserveWhitespace: false, |
30 | | - modules: [ |
31 | | - !!functional && { |
32 | | - transformNode: (el) => { |
33 | | - if (el.tag === 'svg') { |
34 | | - el.classBinding = '[data.class, data.staticClass]'; |
35 | | - el.styleBinding = '[data.style, data.staticStyle]'; |
36 | | - } |
37 | | - }, |
38 | | - }, |
39 | | - ], |
40 | | - }); |
41 | | - |
42 | | - renderFn = ` |
43 | | - function render(${functional ? '_h, _vm' : ''}) { |
44 | | - ${renderFn}; |
45 | | - }; |
46 | | - `; |
47 | | - |
48 | | - renderFn = stripWith(renderFn, { |
49 | | - transforms: { |
50 | | - stripWithFunctional: !!functional, |
51 | | - }, |
52 | | - }); |
53 | | - |
54 | | - const component = ` |
55 | | - ${renderFn} |
56 | | -
|
57 | | - module.exports = { |
58 | | - ${functional ? 'functional: true,' : ''} |
59 | | - render: render, |
60 | | - }; |
61 | | - `; |
| 6 | + const { svgo } = getOptions(this) || {}; |
62 | 7 |
|
63 | | - callback(null, component); |
64 | | - }) |
| 8 | + svgToVue(content, { |
| 9 | + svgoPath: this.resourcePath, |
| 10 | + svgoConfig: svgo, |
| 11 | + }) |
| 12 | + .then(component => callback(null, component)) |
65 | 13 | .catch(callback); |
66 | 14 | }; |
0 commit comments