Package Exports
- gulp-svg-sprite
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (gulp-svg-sprite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
gulp-svg-sprite

===============
is a gulp plugin wrapping around svg-sprite which reads in a bunch of SVG files, optimizes them and creates SVG sprites in various flavours:
- Traditional CSS sprites for use with background images (configuration)
- CSS sprites with pre-defined SVG views, suitable for foreground images as well (configuration)
- Inline sprites using the
<defs>
element (configuration) - Inline sprites using the
<symbol>
element (configuration) - SVG stacks (configuration)
Features & configuration? → svg-sprite
This manual covers only gulp specific installation and configuration aspects. For a full list of features and options, please see the svg-sprite manual.
Usage
First, install gulp-svg-sprite
as a development dependency:
npm install --save-dev gulp-svg-sprite
Then, add it to your gulpfile.js
:
var gulp = require('gulp'),
svgSprite = require('gulp-svg-sprite');
gulp.src('assets/*.svg')
.pipe(svgSprite( /* ... Insert your configuration here ... */ ))
.pipe(gulp.dest('out'));
NOTICE: By default, svg-sprite doesn't send any files downstream unless you configure it. There are tons of options available — please see below for some basic examples. Also, you should possibly take care of errors that might occur.
API
svgSprite(options)
As options
argument you may provide a main configuration object as described in the svg-sprite manual. Configuration-wise, svg-sprite and gulp-svg-sprite differ only in one respect:
options.dest
Type: String
Default value: '.'
With gulp, there is no need to specifiy a main output directory, as the generated files are piped to the next step of the running task anyway. The options.dest
value (if given) is simply ignored.
Examples
Basic example
In this very basic example, mostly default settings will be applied to create a traditional CSS sprite (bundle of SVG sprite and CSS stylesheet).
var gulp = require('gulp'),
svgSprite = require('gulp-svg-sprite'),
// Basic configuration example
config = {
mode : {
css : { // Activate the «css» mode
render : {
css : true // Activate CSS output (with default options)
}
}
}
};
gulp.src('**/*.svg', {cwd: 'assets'})
.pipe(svgSprite(config))
.pipe(gulp.dest('out'));
The following files and directories are created:
out
`-- css
|-- sprite.css
`-- svg
`-- sprite.css-495d2010.svg
The cryptical looking part in the SVG's file name is the result of svg-sprite's cache busting feature which is enabled by default for CSS sprites. We'll turn this off in the next example.
More complex example
The following example is a little more complex:
- We'll create a «view» CSS sprite and a «symbol» sprite in one go.
- Instead of CSS, we'll render a Sass stylesheet resource for the «view» sprite.
- We'll turn off cache busting for the «view» sprite and create extra CSS rules specifying each shape's dimensions.
- We'll downscale the SVG shapes to 32×32 pixels if necessary and add 10 pixels padding to all sides.
- We'll keep the intermediate SVG source files.
var gulp = require('gulp'),
svgSprite = require('gulp-svg-sprite'),
// More complex configuration example
config = {
shape : {
dimension : { // Set maximum dimensions
maxWidth : 32,
maxHeight : 32
},
spacing : { // Add padding
padding : 10
},
dest : 'out/intermediate-svg' // Keep the intermediate files
},
mode : {
view : { // Activate the «view» mode
bust : false,
render : {
scss : true // Activate Sass output (with default options)
}
},
symbol : true // Activate the «symbol» mode
}
};
gulp.src('**/*.svg', {cwd: 'assets'})
.pipe(svgSprite(config))
.pipe(gulp.dest('out'));
The following files and directories are created:
out
|-- intermediate-svg
| |-- weather-clear.svg
| |-- weather-snow.svg
| `-- weather-storm.svg
|-- symbol
| `-- svg
| `-- sprite.symbol.svg
`-- view
|-- sprite.scss
`-- svg
`-- sprite.view.svg
Error handling
Errors might always happen — maybe there are some corrupted source SVG files, the default SVGO plugin configuration is too aggressive or there's just an error in svg-sprite's code. To make your tasks more robust, you might consider using plumber and adding your custom error handling:
var gulp = require('gulp'),
svgSprite = require('gulp-svg-sprite'),
plumber = require('gulp-plumber'),
// Basic configuration example
config = {
mode : {
css : {
render : {
css : true
}
}
}
};
gulp.src('**/*.svg', {cwd: 'assets'})
.pipe(plumber())
.pipe(svgSprite(config))
.on('error', function(error){
/* Do some awesome error handling ... */
})
.pipe(gulp.dest('out'));
Advanced features
For more advanced features like
- custom transforms,
- meta data injection,
- customizing output templates or
- introducing new output formats
please refer to the svg-sprite manual.
Release history
v1.0.11 Bugfix release
- Compatible with svg-sprite 1.0.11
- Fixed coordinate distortion in CSS sprites (svg-sprite #41)
v1.0.10 Feature release
- Compatible with svg-sprite 1.0.10
- Added support for custom mode keys
v1.0.9 Maintenance release
- Compatible with svg-sprite 1.0.9
- Updated dependencies
- Introduced
svg
getter in templating shape variables - Fixed logging error in SVGO optimization
- Fixed missing XML namespaces in SVG stack
- Fixed cache busting errors with example HTML document
v1.0.8 Bugfix release
- Compatible with svg-sprite 1.0.8
- Fixed broken rendering template path resolution (#29)
v1.0.7 Feature & bugfix release
- Compatible with svg-sprite 1.0.7
- Improved error handling
- Improved XML & DOCTYPE declaration handling and fixed (grunt-svg-sprite #28)
v1.0.6 Feature release
- Compatible with svg-sprite 1.0.6
- Made shape ID namespacing configurable (grunt-svg-sprite #27)
- Added extended alignment options (svg-sprite #33)
v1.0.5 Bufix release
- Compatible with svg-sprite 1.0.5
- Fixed regression bug with SVG stacks
v1.0.4 Bugfix release
- Compatible with svg-sprite 1.0.4
- Fixed XML & doctype declaration bug with inline sprites (#2)
v1.0.2
- Initial release, compatible with svg-sprite 1.0.2
Legal
Copyright © 2015 Joschi Kuphal (joschi@kuphal.net / @jkphl)
gulp-svg-sprite is licensed under the terms of the MIT license.
The contained example SVG icons are part of the [Tango Icon Library]