Skip to content

Commit

Permalink
fix(icons-to-woff): ignore fill="none" in svgs
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Sep 14, 2016
1 parent 1dbb1fd commit caf29be
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 22 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ With this plugin it is pretty easy to use pixel images, svgs and font-icons side

## When should you use icon fonts over pure SVGs?

Icon fonts are dead - long live font icons.
SVGs have some disadvantages and lack certain features especially when used inside pseudo elements:

* [CSS-Transform issues in older Internet Explorer versions](http://stackoverflow.com/questions/21298338/css-transform-on-svg-elements-ie9)
* [IE9 - IE 11 scaling issues](https://gist.github.com/larrybotha/7881691)
* Fill color doesn't work for background SVG images
* Inline SVGs are resolved relative to the page not to the css

## What about all the rant on icon fonts?

Like all technologies there are disadvantages when using icon fonts.
We tried to apply best practices to solve the main issues for you.

* Screen Reader friendly: All generated icons use the [Unicode Private Use Areas](https://en.wikipedia.org/wiki/Private_Use_Areas)
* Prevents [FOUT](http://www.paulirish.com/2009/fighting-the-font-face-fout/)

# License

This project is licensed under [MIT](https://github.com/jantimon/iconfont-webpack-plugin/blob/master/LICENSE).
Expand Down
14 changes: 1 addition & 13 deletions example/default/account.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion example/default/dist/index.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<link rel="stylesheet" type="text/css" href="./styles.css">
<script src="bundle.js"></script>
<a>click me</a>
<a href="#">click me</a>
8 changes: 7 additions & 1 deletion example/default/dist/styles.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
@font-face {
font-family: i96002e;
src: url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAAQkAAsAAAAABlAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAAB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PcA8r/An38eoN08gUsSncUif7LLRnef6utK1SU6hJMD5bC11oGzq9Ueujqg7J1LlYxdbkas6uzjKjSmt2OnLB1rlyNhrF4geRyZEigkGBuKkOS2gk2CNDCHvVvdQrpi0q+rVWmCDA+Cq1YKpokiGVxobJNY6sFQ48bUrXMa34Ws7kpLnMat4kIyv+77q3oxPRD7BtpkrMMOITX+SD5g75Pz0RXqgAAAB4nGNgYoAAVgbsgImRiZGZgS0zOT9P14CBAQAN+wIdAA==") format('woff');
src: url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAAQwAAsAAAAABkwAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1VCAAABCAAAADsAAABUIIwleU9TLzIAAAFEAAAAPwAAAFYvsjszY21hcAAAAYQAAABKAAABcOEoo6pnbHlmAAAB0AAAAJgAAACkgs5VC2hlYWQAAAJoAAAALgAAADYJC91ZaGhlYQAAApgAAAAeAAAAJAPxAe9obXR4AAACuAAAAAgAAAAIAe7//2xvY2EAAALAAAAABgAAAAYAUgAAbWF4cAAAAsgAAAAeAAAAIAENAEBuYW1lAAAC6AAAASkAAAIWm5e+CnBvc3QAAAQUAAAAGwAAACxqPAEEeJxjYGRgYOBiMGCwY2DKSSzJY+BzcfMJYZBiYGGAAJA8MpsxJzM9kYEDxgPKsYBpDiBmg4gCAClZBUgAeJxjYGT4zjiBgZWBgdGSMY2BgcEdSn9lkGRoYWBgYmBlZsAKAtJcUxgcHjA8AKoBAj0mZgZGIA3CDACcLwhKAHic7ZCxDYAwEAPPykOBGIEqNbNQsb/YJPkY2CKWzpJPXz2wACU5kwDdiJErrewLm31QfRPDP7T2d+5wo5WZ3X18y796QR0aJglRAAB4nA2IQQ6CMBAAu9vSLVBaqVJMDxKNgWiMhpDQhAPc/IH//4I/0M4kcxgGvwR8kbOJsXGOyQ1W2HCKL4hTnPu0Vmh9BxZakhZkCu/Qt54yobWr902zxM6KijghAuXGPa7hdMxyiSC4kCqvrV4yVQgSrr6fn94YjjCstnchKOsqR6Uy+hNQFVRqVcK4u7xvhbYHz9gfcc4RpXicY2BkYGAA4pNz7BPj+W2+MnAzMYDA5X/ndGH0fyBg/MDEDORyMIClAXeIDkgAAHicY2BkYGBiAAHGd//////H+IGBkQEVMAEAg2cF4gAAAAAAAAHu//8AAAAAAFIAAHicY2BkYGBgYjBhANEgFgMDFxAyMPwH8xkADasBTgAAeJxlkD1uwkAUhMdgSAJSghQpKbNVCiKZn5IDQE9Bl8KYtTGyvdZ6QaLLCXKEHCGniHKCHChj82hgLT9/M2/e7soABviFh3p5uG1qvVq4oTpxm/Qg7JOfhTvo40W4S38o3MMbpsJ9POKdO3j+HZ0BSuEW7vEh3Kb/KeyTv4Q7eMK3cJf+j3APK/wJ9/HqDdPIFLEp3FIn+yy0Z3n+rrStUlOoSTA+WwtdaBs6vVHro6oOydS5WMXW5GrOrs4yo0prdjpywda5cjYaxeIHkcmRIoJBgbipDktoJNgjQwh71b3UK6YtKvq1VpggwPgqtWCqaJIhlcaGyTWOrBUOPG1K1zGt+FrO5KS5zGreJCMr/u+6t6MT0Q+wbaZKzDDiE1/kg+YO+T89EV6oAAAAeJxjYGKAAFYG7ICJkYmRmYEp04CBAQADvgCsAA==") format('woff');
font-weight: normal;
font-style: normal;
}

a:before {
font-family: i96002e;
text-rendering: auto;
-webkit-font-smoothing: auto;
-moz-osx-font-smoothing: auto;
font-weight: normal;
content: '\E000';
transition: 0.5s color;
}
Expand Down
22 changes: 18 additions & 4 deletions lib/icons-to-woff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var assert = require('assert');
var svgicons2svgfont = require('svgicons2svgfont');
var svg2ttf = require('svg2ttf');
var ttf2woff = require('ttf2woff');
var Readable = require('stream').Readable;
var fs = require('fs');

module.exports = function createIconFont (icons, options) {
Expand All @@ -11,7 +12,10 @@ module.exports = function createIconFont (icons, options) {
name: options.name,
normalize: true,
fontHeight: 512,
log: function () {}
log: function () {},
error: function (err) {
reject(err);
}
});
var svgFont = '';
fontStream
Expand All @@ -25,16 +29,26 @@ module.exports = function createIconFont (icons, options) {
reject(err);
});
icons.forEach((filename, i) => {
var glyph = fs.createReadStream(filename);
var glyph = new Readable();
glyph._read = function noop () {};
glyph.metadata = {
unicode: [String.fromCodePoint('\ue000'.charCodeAt(0) + i)],
name: 'icon-' + i
name: 'i' + i
};
fontStream.write(glyph);
fs.readFile(filename, 'utf8', function (err, result) {
if (err) {
return reject(err);
}
// prevent svgs with fill="none" from beeing translated into an empty symbol
result = result.replace(/\sfill\s*=\s*["']?none['"]?/ig, '');
glyph.push(result);
glyph.push(null);
});
});
fontStream.end();
})
.then((svgFont) => svg2ttf(svgFont, {}).buffer)
.then((ttfFont) => ttf2woff(ttfFont).buffer)
.then((wofffont) => new Buffer(wofffont).toString('base64'));
.then((woffFont) => new Buffer(woffFont).toString('base64'));
};
9 changes: 8 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ module.exports = function () {
.then((result) => {
var url = '"data:application/x-font-woff;charset=utf-8;base64,' + result + '"';
callback(null, 'module.exports=' + JSON.stringify(url) + ';');
}, callback);
}, function (err) {
var url = '"data:application/x-font-woff;charset=utf-8;base64,"';
if (typeof err === 'string') {
err = new Error(err);
}
err.message += ' - Tried to compile: ' + JSON.stringify(query.svgs, null, 2);
callback(new Error(err), 'module.exports=' + JSON.stringify(url) + ';');
});
};
24 changes: 23 additions & 1 deletion lib/postcss-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ function replaceIconFontDeclarations (fontName, postCssRoot, unresolvedSvgPaths)
prop: 'font-family',
value: fontName
});
decl.cloneBefore({
prop: 'text-rendering',
value: 'auto'
});
decl.cloneBefore({
prop: '-webkit-font-smoothing',
value: 'auto'
});
decl.cloneBefore({
prop: '-moz-osx-font-smoothing',
value: 'auto'
});
decl.cloneBefore({
prop: 'font-weight',
value: 'normal'
});
// Look up the index of the svg in the array to generate the unicode char position
var iconCharCode = unresolvedSvgPaths.indexOf(getRelativeIconPath(decl.value));
decl.value = '\'\\e' + _.padStart(iconCharCode.toString(16), 3, '0') + '\'';
Expand All @@ -70,7 +86,13 @@ function addFontDeclaration (fontName, postCssRoot, resolvedSvgs) {
// Use ~ to tell the loader-utils of the css loader that this is a webpack path
// Use !! to tell webpack that we don't want any other loader to kick in
var url = '~!!' + require.resolve('./loader.js') + '?' + JSON.stringify(options) + '!' + require.resolve('../');
postCssRoot.prepend(postcss.parse('@font-face { font-family: ' + fontName + '; src:url(\'' + url + '\') format(\'woff\') }'));
postCssRoot.prepend(postcss.parse(
'@font-face { ' +
'font-family: ' + fontName + '; src:url(\'' + url + '\') format(\'woff\');' +
'font-weight: normal;' +
'font-style: normal;' +
'}'
));
}

module.exports = postcss.plugin('iconfont-webpack', function (opts) {
Expand Down

0 comments on commit caf29be

Please sign in to comment.