Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a custom style component when styles are not inlined
- Loading branch information
Showing
7 changed files
with
95 additions
and
30 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,72 @@ | ||
const path = require('path') | ||
const posthtml = require('posthtml') | ||
|
||
const plugin = tree => { | ||
tree.match({ tag: 'svg' }, node => { | ||
// Remove unnecessary attrs on SVG tag | ||
const REGISTER_STYLE_COMPONENT = path.join(__dirname, './lib/registerStyleComponent.js') | ||
|
||
const plugin = state => tree => { | ||
tree.match({ tag: 'svg' }, node => { | ||
const attrs = {} | ||
|
||
node.attrs = node.attrs || {} | ||
for (const name of Object.keys(node.attrs)) { | ||
// Don't add unnecessary attrs | ||
if (name !== 'version' && name !== 'xmlns' && !name.startsWith('xmlns:')) { | ||
attrs[name] = node.attrs[name] | ||
} | ||
delete node.attrs[name] | ||
} | ||
|
||
// Adding v-bind | ||
node.attrs['v-bind'] = `Object.assign(${JSON.stringify(attrs)},data.attrs)` | ||
const hasAttrs = Object.keys(attrs).length > 0 | ||
node.attrs['v-bind'] = hasAttrs ? `Object.assign(${JSON.stringify(attrs)},data.attrs)` : 'data.attrs' | ||
|
||
// Adding v-on | ||
node.attrs['v-on'] = 'data.on' | ||
|
||
return node | ||
}) | ||
|
||
// SVGO will inline styles, so if you don't turn off relevant plugin | ||
// the tree will never match `style` nodes because they don't exist | ||
tree.match({ tag: 'style' }, node => { | ||
// Ignore the style tag if it's empty | ||
if (!node.content || node.content.length === 0) return | ||
|
||
// When SVGO is turned off | ||
// Make `style` tags work by using a Vue component | ||
node.tag = 'svg2vue-style' | ||
state.hasStyleTag = true | ||
return node | ||
}) | ||
} | ||
|
||
const createComponent = svg => { | ||
return `<template functional>${svg}</template>` | ||
const createComponent = (svg, state) => { | ||
let result = `<template functional>${svg}</template>` | ||
|
||
if (state.hasStyleTag) { | ||
result += ` | ||
<script> | ||
import ${JSON.stringify(REGISTER_STYLE_COMPONENT)} | ||
export default {} | ||
</script> | ||
` | ||
} | ||
|
||
return result | ||
} | ||
|
||
module.exports = (input, { sync } = {}) => { | ||
const state = {} | ||
|
||
const stream = posthtml([ | ||
plugin | ||
plugin(state) | ||
]).process(input, { sync }) | ||
|
||
if (stream.then) { | ||
return stream.then(res => ({ | ||
component: createComponent(res.html) | ||
component: createComponent(res.html, state) | ||
})) | ||
} | ||
|
||
return { component: createComponent(stream.html) } | ||
return { component: createComponent(stream.html, state) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Vue from 'vue' | ||
|
||
var NAME = 'svg2vue-style' | ||
|
||
if (!Vue.component(NAME)) { | ||
Vue.component(NAME, { | ||
functional: true, | ||
render: function (h, ctx) { | ||
return h('style', ctx.data, ctx.children) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.