在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):w0rm/gulp-svgfallback开源软件地址(OpenSource Url):https://github.com/w0rm/gulp-svgfallback开源编程语言(OpenSource Language):JavaScript 93.3%开源软件介绍(OpenSource Introduction):gulp-svgfallbackGenerate png sprite from svg sources.
Options
Automatic options:
If your workflow is different, please use UsageThe following task will output icons.png and icons.css: var svgfallback = require('gulp-svgfallback');
var gulp = require('gulp');
gulp.task('svgfallback', function () {
return gulp
.src('src/icons/*.svg', {base: 'src/icons'})
.pipe(svgfallback())
.pipe(gulp.dest('dest'));
}); CaveatsPlease ensure that svg sources have width and height: these dimensions will be used to generate png sprite. Custom css templateAn additional option Here is an example of data that is passed to css template: {
"backgroundUrl": "sprite.png",
"icons": [
{
"name": "circle",
"width": 40,
"height": 40,
"left": 0,
"top": 0
},
{
"name": "square",
"width": 40,
"height": 40,
"left": 40,
"top": 0
}
]
} Custom css classesIf you need to add prefix to each css class, please use var gulp = require('gulp');
var rename = require('gulp-rename');
var svgfallback = require('gulp-svgfallback');
gulp.task('default', function () {
return gulp
.src('src/icons/*.svg', {base: 'src/icons'})
.pipe(rename({prefix: 'icon-'})
.pipe(svgfallback())
.pipe(gulp.dest('dest'));
}); Since css class for each icon should be unique, you cannot pass files with the same name.
If you need to have nested directories that may have files with the same name, please
use var gulp = require('gulp');
var rename = require('gulp-rename');
var svgfallback = require('gulp-svgfallback');
gulp.task('default', function () {
return gulp
.src('src/icons/**/*.svg', {base: 'src/icons'})
.pipe(rename(function (path) {
var name = path.dirname.split(path.sep);
name.push(path.basename);
path.basename = name.join('-');
}))
.pipe(svgfallback())
.pipe(gulp.dest('dest'));
}); Add variationsTo add variations (e.g. different colors) into your sprite, you can combine gulp-svgfallback with other gulp plugins. The following task will add var path = require('path');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var lazypipe = require('lazypipe');
var clone = require('gulp-clone');
var cheerio = require('gulp-cheerio');
var rename = require('gulp-rename');
var svgfallback = require('gulp-svgfallback');
function isCircle (file) {
return path.basename(file.relative) === 'circle.svg';
}
function colorize (color) {
var sink;
return (lazypipe()
.pipe(function () {
sink = clone.sink();
return sink;
})
.pipe(cheerio, function ($) {
$('svg').attr('fill', color);
})
.pipe(rename, {suffix: '-' + color})
.pipe(function () {
return sink.tap();
})
)();
}
gulp.task('svgfallback', function () {
return gulp
.src('src/icons/*.svg', {base: 'src/icons'})
.pipe(gulpif(isCircle, colorize('red')))
.pipe(gulpif(isCircle, colorize('blue')))
.pipe(svgfallback())
.pipe(gulp.dest('dest'));
}); Changelog
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论