A gulp plugin to use inline SVG as background-images
npm install gulp-inline-svgThis plugin takes a couple of SVG files and provides them inside a Sass mixin as inlined background-images. By doing so, you can use them without having to touch your markup.
First, install gulp-inline-svg as a development dependency:
``shell`
npm install --save-dev gulp-inline-svg
Then, add it to your gulpfile.js. I'd recommend to use it in conjunction with gulp-svgmin to clean the SVG before inlining them.
`javascript
var inlineSvg = require("gulp-inline-svg"),
svgMin = require('gulp-svgmin');
gulp.task('inline-svg', function() {
return gulp.src("images/svgs/*/.svg")
.pipe(svgMin())
.pipe(inlineSvg())
.pipe(gulp.dest("sass"));
});
`
This will create a _svg.scss file inside your 'sass' folder.
#### inline-svg mixin
`scss`
.my-icon {
// will set the svg as background-image and width/height
@include inline-svg($icon-name);
}
#### inline-svg-width function
`scss`
.my-icon {
margin-left: inline-svg-width($icon-name);
}
#### inline-svg-height function
`scss`
.my-icon {
margin-bottom: inline-svg-height($icon-name);
}
`javascript``
gulp.src("images/svgs/*/.svg")
.pipe(svgMin())
.pipe(inlineSvg({
filename: '_svg-file.scss',
template: 'mytemplate.mustache'
}))
.pipe(gulp.dest("sass"));
You can use the default template as a blueprint to create your own. And please don't hesitate to share your template if you create one for an additional language.