-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inline definitions #976
base: main
Are you sure you want to change the base?
inline definitions #976
Conversation
Would be great to have this implemented! 👍 |
Bump this PR :-) |
@GreLI Please implement this PR! It will help many Sketch users. I was actually about to do a PR to solve this problem until I stumbled upon this. |
plugins/inlineDefs.js
Outdated
var useCount = _countUses(uses); | ||
|
||
for (var i = 0; i < uses.length; i++) { | ||
var href = uses[i].attr('xlink:href').value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's href
in SVG 2.0, btw: https://www.w3.org/TR/SVG2/struct.html#UseElement
Cloning the repo and adding in this as a plugin, did not work for me without two steps. I have tried exporting from Sketch without SVGO
Returns this with inlineDefs Export from sketch with SVGO Compressor goes from to this with inlineDefs
So I need to use SVGO Compressor from sketch and then do svgo in node to get it like I want. I tried setting onlyUnique to false, but that often ended in an error. The above is FYI on this PR, so I'll do more research on my part. |
I believe that's because the plugin is currently set to run first so it's applied before the other optimizations are applied. It should probably be set to run last. Also, worth noting that the latest version of the SVGO Sketch plugin supports custom svgo plugins: https://github.com/BohemianCoding/svgo-compressor#custom-svgo-plugins but it would definitely be nice to merge this PR as it's a pretty important SVG optimisation |
@GreLI, @deepsweet, What's needed to get this PR merged? It looks like an incredibly useful tool! |
If the element beeing inlined is a I have the following SVG: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="32" height="32">
<defs>
<symbol id="b" viewBox="0 0 31.55 27.98">
<path fill="#fff" fill-rule="evenodd"
d="M31.36 25.74A1.51 1.51 0 0 1 30.05 28H1.5a1.46 1.46 0 0 1-.74-.19 1.5 1.5 0 0 1-.56-2.07L14.47.76a1.5 1.5 0 0 1 2.61 0z"/>
<path fill="#ff9d00" fill-rule="evenodd"
d="M30.92 26L16.65 1a1 1 0 0 0-1.37-.37 1.09 1.09 0 0 0-.37.37L.63 26A1 1 0 0 0 1 27.35a1 1 0 0 0 .5.13h28.55a1 1 0 0 0 1-1 1 1 0 0 0-.13-.48z"/>
<path d="M30.92 26a1 1 0 0 1 .13.49 1 1 0 0 1-1 1H1.5a1 1 0 0 1-.5-.13A1 1 0 0 1 .63 26L14.91 1a1.09 1.09 0 0 1 .37-.37 1 1 0 0 1 1.37.37zm-.43.25l-14.28-25a.49.49 0 0 0-.68-.18.47.47 0 0 0-.19.18l-14.27 25a.49.49 0 0 0 .18.68.54.54 0 0 0 .25.07h28.55a.5.5 0 0 0 .5-.5.5.5 0 0 0-.06-.26z"
opacity=".5"/>
</symbol>
<symbol id="a" viewBox="0 0 24.78 15.48">
<path d="M16.78 3a2 2 0 1 0 2 2 2 2 0 0 0-2-2m-4-3a1 1 0 1 0 1 1 1 1 0 0 0-1-1m9 14a3 3 0 1 0-3-3 3 3 0 0 0 3 3m-2.69 1.48H.5a.54.54 0 0 1-.25-.06.51.51 0 0 1-.18-.69L8.28.36z"/>
</symbol>
<symbol id="c" viewBox="0 0 24.78 15.48">
<use width="24.78" height="15.48" xlink:href="#a"/>
</symbol>
</defs>
<use width="31.55" height="27.98" transform="translate(.2 3.02)" xlink:href="#b"/>
<use width="24.78" height="15.48" transform="translate(1.22 14.5)" xlink:href="#c"/>
</svg> Which should turn into something like this: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="32" height="32">
<g transform="translate(.2 3.02)">
<path fill="#fff" fill-rule="evenodd" d="M31.36 25.74A1.51 1.51 0 0 1 30.05 28H1.5a1.46 1.46 0 0 1-.74-.19 1.5 1.5 0 0 1-.56-2.07L14.47.76a1.5 1.5 0 0 1 2.61 0z"/>
<path fill="#ff9d00" fill-rule="evenodd" d="M30.92 26L16.65 1a1 1 0 0 0-1.37-.37 1.09 1.09 0 0 0-.37.37L.63 26A1 1 0 0 0 1 27.35a1 1 0 0 0 .5.13h28.55a1 1 0 0 0 1-1 1 1 0 0 0-.13-.48z"/>
<path d="M30.92 26a1 1 0 0 1 .13.49 1 1 0 0 1-1 1H1.5a1 1 0 0 1-.5-.13A1 1 0 0 1 .63 26L14.91 1a1.09 1.09 0 0 1 .37-.37 1 1 0 0 1 1.37.37zm-.43.25l-14.28-25a.49.49 0 0 0-.68-.18.47.47 0 0 0-.19.18l-14.27 25a.49.49 0 0 0 .18.68.54.54 0 0 0 .25.07h28.55a.5.5 0 0 0 .5-.5.5.5 0 0 0-.06-.26z" opacity=".5"/>
</g>
<g transform="translate(1.22 14.5)">
<g>
<path d="M16.78 3a2 2 0 1 0 2 2 2 2 0 0 0-2-2m-4-3a1 1 0 1 0 1 1 1 1 0 0 0-1-1m9 14a3 3 0 1 0-3-3 3 3 0 0 0 3 3m-2.69 1.48H.5a.54.54 0 0 1-.25-.06.51.51 0 0 1-.18-.69L8.28.36z"/>
</g>
</g>
</svg> As a proof of concept I changed the The following code was replaced line 87.
|
Unfortunately it appears from #1055 that SVGO is no longer maintained. Maybe comment in #1055 if you think you have enough time and level of experience to help maintain it. Please don't comment there to criticize anybody or to say that "somebody ought to maintain it" as that creates spam and will not help the situation. Feel free to use upvoting/downvoting to make your opinions known, but do remember that this is a volunteer project so developers are free to come and go as they please. |
The code is taken from #976, refactored with new api, covered types and simplified. Plugin has no dependencies so can be used without changing. ``` const inlineDefs = require('./inlineDefs.js'); module.exports = { plugins: [ 'preset-default', inlineDefs ] }; ```
this inlines definitions
e.g. turns
into
By default, only definitions which are used once are inline. If you set the onlyUnique parameter to
false
, all definitions will be inlined.Should solve #563