We can definitely get a little nerdier with our build tools. At the time of posting this, the poster child of build tools is Grunt. There are competitors, but Grunt has been the most popular for quite a while. If you’re brand new to Grunt, I have a variety of things I’ve written and screencasted about it:
- Grunt for People Who Think Things Like Grunt are Weird and Hard
- #130: First Moments with Grunt
- #134: A Tour of a Site In-Progress Built with Jekyll, Grunt, Sass, an SVG System, and More
Even if you’ve never used Grunt, in this screencast we pretty much start from scratch and get this all going. The idea is that we’re working from the quintessential “folder full of SVGs”. Each file.svg represents some graphic we want to use on the site. We want to squish all that into a SVG defs block that is ready to use. Symbols created, accessibility information added to the best of our automated ability, etc.
Once we get Grunt going, and install the build tool we’re focusing on here, grunt-svgstore, we’re good to go.
In our little demo we have Grunt configured to, via the Gruntfile.js, to look at our folder full of SVG’s and create a defs.svg file we put in an includes folder.
module.exports = function(grunt) {
grunt.initConfig({
svgstore: {
options: {
formatting : {
indent_size : 2
}
},
default: {
files: {
'includes/defs.svg': ['svg/*.svg'],
},
},
},
});
grunt.loadNpmTasks('grunt-svgstore');
};
Leveling up from here would include using a “watch” plugin to watch that folder of SVGs and automatically run this task when any of the files change (or are added or deleted). If you’re using a site builder like Jekyll, you might even trigger a jekyll build
afterward to make sure the new file is available in the built site.
Hello.
I would like to know if you could help me.
I have many icons exported by “Figma”, the structure for that folder is something like this:
-icons
–arrow
—arrow-left
—-scale.svg
—arrow-right
—-scale.svg
–checkbox
—active.svg
—inactive.svg
–chevron
—left-chevron
—-scale.svg
—right-chevron
—-scale.svg
–etc.
The file that I got as result has many duplicated id due to this take the name of the file as the id.
Something like this:
…
…
I need that to be unique ids, I think that I could concatenate the relative path of the file but I don’t know how to do it.
I have tried configuring as this but it doesnt work:
grunt.initConfig({
svgstore: {
options: {
formatting : {
indent_size : 2
},
includeTitleElement: false,
preserveDescElement: false,
allowDuplicateItems: false,
setUniqueIds: true
},
default: {
files: {
‘includes/defs.svg’: [‘icons/**/*.svg’,]
},
},
}
});
Expected result:
…
…
Thanks for all the help that you could give us.