Skip to content

Commit b1ec8c4

Browse files
Add class/style merging behavior for functional SVG components
1 parent c8b280c commit b1ec8c4

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ const compiler = require('vue-template-compiler');
44
const transpile = require('vue-template-es2015-compiler');
55

66
module.exports = function (content) {
7-
const options = loaderUtils.getOptions(this) || {};
8-
const svg = new svgo(options.svgo || {});
7+
const {
8+
svgo: svgoOptions = {},
9+
functional = false,
10+
} = loaderUtils.getOptions(this) || {};
11+
12+
const svg = new svgo(svgoOptions);
913
const path = this.resourcePath;
1014

1115
this.cacheable && this.cacheable(true);
@@ -19,19 +23,29 @@ module.exports = function (content) {
1923
.then((result) => {
2024
const compiled = compiler.compile(result.data, {
2125
preserveWhitespace: false,
26+
modules: [
27+
!!functional && {
28+
transformNode: (el) => {
29+
if (el.tag === 'svg') {
30+
el.classBinding = '[data.class, data.staticClass]';
31+
el.styleBinding = '[data.style, data.staticStyle]';
32+
}
33+
},
34+
}
35+
],
2236
});
2337

24-
const transpileCode = `var render = function (${options.functional ? '_h, _vm' : ''}) { ${compiled.render} };`;
38+
const transpileCode = `var render = function (${functional ? '_h, _vm' : ''}) { ${compiled.render} };`;
2539

2640
const transpileOptions = {
2741
transforms: {
28-
stripWithFunctional: !!options.functional || false,
42+
stripWithFunctional: !!functional,
2943
},
3044
};
3145

3246
component = `${transpile(transpileCode, transpileOptions)}\n`;
3347

34-
if (options.functional) {
48+
if (functional) {
3549
component += 'module.exports = { functional: true, render: render };';
3650
} else {
3751
component += 'module.exports = { render: render };';

0 commit comments

Comments
 (0)