|
1 |
| -const svg = require('svgo'); |
| 1 | +const svgo = require('svgo'); |
2 | 2 | const loaderUtils = require('loader-utils');
|
3 | 3 | const compiler = require('vue-template-compiler');
|
| 4 | +const transpile = require('vue-template-es2015-compiler'); |
4 | 5 |
|
5 | 6 | module.exports = function (content) {
|
6 | 7 | const options = loaderUtils.getOptions(this) || {};
|
7 |
| - const query = loaderUtils.parseQuery(this.resourceQuery || '?'); |
8 |
| - const svgo = new svg(options.svgo || { |
9 |
| - plugins: [{ removeDoctype: true }, { removeComments: true }], |
10 |
| - }); |
| 8 | + const svg = new svgo(options.svgo || {}); |
| 9 | + const path = this.resourcePath; |
11 | 10 |
|
12 | 11 | this.cacheable && this.cacheable(true);
|
13 | 12 | this.addDependency(this.resourcePath);
|
14 | 13 |
|
15 | 14 | const cb = this.async();
|
| 15 | + let component; |
16 | 16 |
|
17 |
| - svgo.optimize(content, (result) => { |
18 |
| - if (result.error) { |
19 |
| - return cb(result.error); |
20 |
| - } |
| 17 | + svg |
| 18 | + .optimize(content, { path: path }) |
| 19 | + .then((result) => { |
| 20 | + const compiled = compiler.compile(result.data, { preserveWhitespace: false }); |
21 | 21 |
|
22 |
| - const compiled = compiler.compile(result.data, { preserveWhitespace: false }); |
23 |
| - let component = `render: function () {${compiled.render}}`; |
| 22 | + component = transpile(`var render = function () {${compiled.render}};`); |
| 23 | + component += `var toString = function () {return ${JSON.stringify(path)}};`; |
| 24 | + component += `module.exports = { render: render, toString: toString };`; |
24 | 25 |
|
25 |
| - if (options.includePath || query.includePath) { |
26 |
| - const filename = loaderUtils.interpolateName(this, '[path][name].[ext]', { context: this.options.context }); |
27 |
| - component = `${component}, path:${JSON.stringify(filename)}`; |
28 |
| - } |
29 |
| - |
30 |
| - cb(null, `module.exports = {${component}};`); |
31 |
| - }); |
| 26 | + cb(null, component); |
| 27 | + }) |
| 28 | + .catch(cb); |
32 | 29 | };
|
0 commit comments