Skip to content

Commit

Permalink
Be able to change SVG icon size
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Aug 14, 2018
1 parent b6af410 commit cd19c20
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
31 changes: 31 additions & 0 deletions buildtools/svg-viewbox-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const simpleHTMLTokenizer = require('simple-html-tokenizer');

module.exports = function(source) {
this.cacheable(true);
let tokens = simpleHTMLTokenizer.tokenize(source);

tokens = tokens.map((tag) => {
let width = undefined;
let height = undefined;
if (tag.type === 'StartTag' && tag.tagName === 'svg') {
for (const attribute of tag.attributes) {
if (attribute[0] === 'width') {
width = parseFloat(attribute[1]);
}
if (attribute[0] === 'height') {
height = parseFloat(attribute[1]);
}
}
if (width !== undefined && height != undefined) {
tag.attributes = tag.attributes.filter((attribute) => {
return attribute[0] !== 'width' && attribute[0] != 'height'
})
tag.attributes.push(['viewBox', `0 0 ${width} ${height}`, true]);
tag.attributes.push(['height', '1em', true]);
}
}
return tag;
})

return simpleHTMLTokenizer.generate(tokens)
};
16 changes: 9 additions & 7 deletions buildtools/webpack.commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ const htmlRule = {

const svgRule = {
test: /\.svg$/,
use: [{
loader: 'svg-inline-loader',
options: {
removeSVGTagAttrs: false,
use: [
{
loader: 'svg-inline-loader',
options: {
removeSVGTagAttrs: false,
},
},
}, {
loader: 'svgo-loader',
}]
'./buildtools/svg-viewbox-loader',
'svgo-loader',
]
};


Expand Down

0 comments on commit cd19c20

Please sign in to comment.