Combine svg files into one with
Read more about this in CSS Tricks article.
If you need similar plugin for grunt, I encourage you to check grunt-svgstore.
The following options are set automatically based on file data:
idattribute of the
<symbol>element is set to the name of corresponding file;
- result filename is the name of base directory of the first file.
If your workflow is different, please use
gulp-rename to rename sources or result.
The only available option is:
- inlineSvg — output only
DOCTYPEto use inline, default:
npm install gulp-svgstore --save-dev
The following script will combine all svg sources into single svg file with
The name of result svg is the base directory name of the first file
Additionally pass through gulp-svgmin to minify svg and ensure unique ids.
var gulp = ;var svgstore = ;var svgmin = ;var path = ;gulp;
Inlining svgstore result into html body
To inline combined svg into html body I suggest using gulp-inject. The following gulp task will inject svg into
In your html file (using
visuallyhidden from html5-boilerplate to fix the gradients):
<!-- inject:svg --><!-- endinject -->
In your gulp tasks:
var gulp = ;var svgstore = ;var inject = ;gulp;
Generating id attributes
Id of symbol element is calculated from file name. You cannot pass files with the same name, because id should be unique.
If you need to add prefix to each id, please use
var gulp = ;var rename = ;var svgstore = ;gulp;
If you need to have nested directories that may have files with the same name, please
gulp-rename. The following example will concatenate relative path with the name of the file,
var gulp = ;var path = ;var rename = ;var svgstore = ;gulp;
Using svg as external file
PNG sprite fallback for unsupported browsers
gulp-svgfallback is a gulp plugin that generates png sprite and css file with background offsets from svg sources. Please check it and leave feedback.
Transform svg sources or combined svg
To transform either svg sources or combined svg you may pipe your files through gulp-cheerio.
Transform svg sources
An example below removes all fill attributes from svg sources before combining them.
Please note that you have to set
xmlMode: true to parse svgs as xml file.
var gulp = ;var svgstore = ;var cheerio = ;gulp
Transform combined svg
The following example sets
style="display:none" on the combined svg:
(beware if you use gradients and masks, display:none breaks those and just show
nothing, best method is to use the method show above )
var gulp = ;var svgstore = ;var cheerio = ;gulp;
Extracting metadata from combined svg
You can extract data with cheerio.
The following example extracts viewBox and id from each symbol in combined svg.
var gulp = ;var Vinyl = ;var svgstore = ;var through2 = ;var cheerio = ;gulp;
Possible rendering issues with Clipping Paths in SVG
If you're running into issues with SVGs not rendering correctly in some browsers (see issue #47), the issue might be that clipping paths might not have been properly intersected in the SVG file. There are currently two ways of fixing this issue:
Correcting the Clipping Path in the SVG
If you have the source file, simply converting the clipping path to a nice coded shape will fix this issue. Select the object, open up the Pathfinder panel, and click the Intersect icon.
Editing the SVG Code
If you don't have the source file or an SVG Editor (Adobe Illustrator etc.), you can manually edit the SVG code in the file. Wrapping the
<clipPath> into a
<defs> will fix this issue. Here's an example:
<defs> <path d="M28.4 30.5l5.3 5c0-.1 7-6.9 7-6.9l-4-6.8-8.3 8.7z" id="a"/> </defs> <clipPath id="b"><use overflow="visible" xlink:href="#a"/></clipPath>
<defs> <path d="M28.4 30.5l5.3 5c0-.1 7-6.9 7-6.9l-4-6.8-8.3 8.7z" id="a"/> <clipPath id="b"><use overflow="visible" xlink:href="#a"/></clipPath> </defs>
Or you can go further and reduce the size by removing the
<use> element, like this:
<defs> <clipPath id="b"><path d="M28.4 30.5l5.3 5c0-.1 7-6.9 7-6.9l-4-6.8-8.3 8.7z"/></clipPath> </defs>
- Include xmlns:xlink in svg definition #96
- Stop using deprecated
- Drop support for node 0.12
- Stop using deprecated
- Removed dependency on gulp-util to support gulp 4
- Copy preserveAspectRatio attribute from source svg to to symbol #76
- Removed cache of the cheerio object #61
- Correctly set namespaces of the combined svg
- Skip null and invalid files
- Updated readme with a way to ensure unique ids
- Updated direct dependencies
- Removed cheerio from devDependencies #34
- Removed prefix and fileName options
- Ensure unique file names
- Improved readme with gulp-rename usage to generate id for nested directories
- Added cheerio to devDependencies
transformSvg, pipe files through gulp-cheerio instead.
- Made cheerio 0.* a peer dependency, allows to choose what version to use.
file.cheerioif cached in gulp file object and also sets it for the combined svg.
- Improved readme.
- Used cheerio instead of libxmljs (changes transformSvg syntax)
- Added check for inputs before generating SVG.
- Added check for missing viewBox in original svg.
- Initial release.