在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):w0rm/gulp-svgstore开源软件地址(OpenSource Url):https://github.com/w0rm/gulp-svgstore开源编程语言(OpenSource Language):JavaScript 94.5%开源软件介绍(OpenSource Introduction):gulp-svgstoreCombine svg files into one with If you need similar plugin for grunt, I encourage you to check grunt-svgstore. Options:The following options are set automatically based on file data:
If your workflow is different, please use The only available option is:
Installnpm install gulp-svgstore --save-dev UsageThe following script will combine all svg sources into single svg file with Additionally pass through gulp-svgmin to minify svg and ensure unique ids. const gulp = require('gulp');
const svgstore = require('gulp-svgstore');
const svgmin = require('gulp-svgmin');
const path = require('path');
gulp.task('svgstore', () => {
return gulp
.src('test/src/*.svg')
.pipe(svgmin((file) => {
const prefix = path.basename(file.relative, path.extname(file.relative));
return {
plugins: [{
cleanupIDs: {
prefix: prefix + '-',
minify: true
}
}]
}
}))
.pipe(svgstore())
.pipe(gulp.dest('test/dest'));
}); Inlining svgstore result into html bodyTo inline combined svg into html body I suggest using gulp-inject. The following gulp task will inject svg into In your html file (using <div class="sr-only">
<!-- inject:svg --><!-- endinject -->
</div> In your gulp tasks: const gulp = require('gulp');
const svgstore = require('gulp-svgstore');
const inject = require('gulp-inject');
gulp.task('svgstore', () => {
const svgs = gulp
.src('test/src/*.svg')
.pipe(svgstore({ inlineSvg: true }));
function fileContents (filePath, file) {
return file.contents.toString();
}
return gulp
.src('test/src/inline-svg.html')
.pipe(inject(svgs, { transform: fileContents }))
.pipe(gulp.dest('test/dest'));
}); Generating id attributesId 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 const gulp = require('gulp');
const rename = require('gulp-rename');
const svgstore = require('gulp-svgstore');
gulp.task('default', () => {
return gulp
.src('src/svg/**/*.svg', { base: 'src/svg' })
.pipe(rename({prefix: 'icon-'}))
.pipe(svgstore())
.pipe(gulp.dest('dest'));
}); If you need to have nested directories that may have files with the same name, please
use const gulp = require('gulp');
const path = require('path');
const rename = require('gulp-rename');
const svgstore = require('gulp-svgstore');
gulp.task('default', () => {
return gulp
.src('src/svg/**/*.svg', { base: 'src/svg' })
.pipe(rename((file) => {
const name = file.dirname.split(path.sep);
name.push(file.basename);
file.basename = name.join('-');
}))
.pipe(svgstore())
.pipe(gulp.dest('dest'));
}); Using svg as external fileThere is a problem with PNG sprite fallback for unsupported browsersgulp-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 svgTo transform either svg sources or combined svg you may pipe your files through gulp-cheerio. Transform svg sourcesAn example below removes all fill attributes from svg sources before combining them.
Please note that you have to set const gulp = require('gulp');
const svgstore = require('gulp-svgstore');
const cheerio = require('gulp-cheerio');
gulp.task('svgstore', () => {
return gulp
.src('test/src/*.svg')
.pipe(cheerio({
run: ($) => {
$('[fill]').removeAttr('fill');
},
parserOptions: { xmlMode: true }
}))
.pipe(svgstore({ inlineSvg: true })
.pipe(gulp.dest('test/dest'));
}); Transform combined svgThe following example sets const gulp = require('gulp');
const svgstore = require('gulp-svgstore');
const cheerio = require('gulp-cheerio');
gulp.task('svgstore', () => {
return gulp
.src('test/src/*.svg')
.pipe(svgstore({ inlineSvg: true }))
.pipe(cheerio({
run: ($) => {
$('svg').attr('style', 'display:none');
},
parserOptions: { xmlMode: true }
}))
.pipe(gulp.dest('test/dest'));
}); Extracting metadata from combined svgYou can extract data with cheerio. The following example extracts viewBox and id from each symbol in combined svg. const gulp = require('gulp');
const Vinyl = require('vinyl');
const svgstore = require('gulp-svgstore');
const through2 = require('through2');
const cheerio = require('cheerio');
gulp.task('metadata', () => {
return gulp
.src('test/src/*.svg')
.pipe(svgstore())
.pipe(through2.obj(function (file, encoding, cb) {
const $ = cheerio.load(file.contents.toString(), {xmlMode: true});
const data = $('svg > symbol').map(() => {
return {
name: $(this).attr('id'),
viewBox: $(this).attr('viewBox')
};
}).get();
const jsonFile = new Vinyl({
path: 'metadata.json',
contents: Buffer.from(JSON.stringify(data))
});
this.push(jsonFile);
this.push(file);
cb();
}))
.pipe(gulp.dest('test/dest'));
}); Possible rendering issues with Clipping Paths in SVGIf 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 three ways of fixing this issue: Correcting the Clipping Path in the SVGIf 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 CodeIf 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
Becomes:
Or you can go further and reduce the size by removing the
Using gulp-cheerio to automate thisAnother possible solution would be to write a transformation with gulp-cheerio. Check this issue #98 for the instructions. Changelog
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论