npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-svg-combine

v1.0.2

Published

Combine SVG files to one with gulp and use by 'symbols'; Fork from https://github.com/Hiswe/gulp-svg-symbols.git

Downloads

1

Readme

gulp-svg-symbols

NPM version Build Status

gulp-svg-symbols is a minimal plugin for gulp.
It converts a bunch of svg files to a single svg file containing each one as a symbol.
See css-trick for more details.

Install

npm install --save-dev gulp-svg-symbols

Example

In your gulpfile.js:

var gulp       = require('gulp');
var svgSymbols = require('gulp-svg-symbols');

gulp.task('sprites', function () {
  return gulp.src('assets/svg/*.svg')
    .pipe(svgSymbols())
    .pipe(gulp.dest('assets'));
});

In your HTML, you first have to reference the SVG
then:

<svg role="img" class="github">
  <use xlink:href="#github"></use>
</svg>
  • class is the one generated in the CSS file
  • xlink:href is the symbol id in the SVG file

Options

You can override the default options by passing an object as an argument to svgSymbols()

Basics

id and className

default: '%f' and '.%f'

Text templates for generating icon class & symbols id
%f is the speakingurled file name placeholder.
See more about the name in the slug option

fontSize

default: 0

This option lets you define a base font.
If it's superior to 0, then the sizes in your CSS file will be in em else sizes are provided with px.

title

default: false

Specify whether or not you want to add a missing title tag in your SVG symbols.
It should be better for accessibility.
It takes a text template (like for id/classname):

title: '%f icon'

svgClassname

default: false

Specify a class for the <svg> container tag in the default SVG template.

svgClassname: 'svg-icon-lib',

output:

<svg xmlns="http://www.w3.org/2000/svg" class="svg-icon-lib">

This is usefull when you want to include the SVG symbols directly in the DOM (i.e. no external reference)

A secure way of hiding the svg is by styling it this way:

.svg-icon-lib {
  border: 0 !important;
  clip: rect(0 0 0 0) !important;
  height: 1px !important;
  margin: -1px !important;
  overflow: hidden !important;
  padding: 0 !important;
  position: absolute !important;
  width: 1px !important;
}

A simple display: none will mess with defs rendering (gradients and so on…)

slug

default: {}

In order to have nice ids in the template and to keep the gulp task quite simple, gulp-svg-symbols use speakingurl.

You can pass a speakingurl's config here:

gulp.src('*.svg')
.pipe(svgSymbols({
  slug: {
    separator: '_',
  },
}))

You can also provide a custom function which should return a string:

gulp.src('*.svg')
.pipe(svgSymbols({
  slug: function (name) {
    return name.replace(/\s/g, '-');
  },
}))

Or if you want to use gulp-rename:

gulp.src('*.svg')
.pipe(rename(/* gulp rename options*/))
.pipe(svgSymbols({
  slug: function (name) { return name; },
}))

templates

default: ['default-svg', 'default-css']

gulp-svg-symbols come with some default templates.
Their names are:

default-svg: responsible of generating the bundled SVG file
default-css: responsible of generating the CSS file containing the symbols sizes and the CSS rules coming from your SVG files
default-demo: the demo page with the snippets you can copy & paste in your HTML

You can control which file are generated by specifying only the templates to keep:

templates: ['default-svg']

will output only the SVG file.

css generation

You can deactivate CSS output by removing the CSS template from the template array.
See templates option for more details.

warn

default: true

Disable plugin warn messages (like: missing viewBox).

Advanced

templates

Specify your own templates by providing an absolute path:

templates: [
  path.join(__dirname, 'path/to/my/template.stylus'),
  path.join(__dirname, 'path/to/another/template.html'),
  // You can still access to default templates by providing:
  'default-svg',
  'default-css',
  'default-demo'
]
  • template engine is lodash.
  • all svg files info are stored in the icons array and passed to every templates.
  • the output files will have the same name & extension as your files.

transformData

With the ability to provide custom templates, you also have the ability to configure custom data.

transformData: function(svg, defaultData, options) {
  /******
  svg is the object containing :
    content (svg markup)
    width   (in numeric — no units)
    height  (in numeric — no units)
    viewBox (as a string)
    name    (svg filename without extension)
    originalAttributes (object of what was gathered from svg tag)

  defaultData are the ones needed by default templates
  see /lib/get-default-data.js

  options are the one you have set in your gulpfile,
    minus templates & transformData
  *******/

  return {
    // Return every datas you need
    id:         defaultData.id,
    className:  defaultData.className,
    width:      svg.width + 'em',
    height:     svg.height + 'em'
  };
}

In your templates, svg original data are accessible in icon.svg.
Of course default templates need defaultData.

Other observations

  • If you want to manipulate your icons files, use gulp-cheerio
  • If you want to optimize your icons files or the SVG output, use gulp-svgmin (using SVGO)
  • If you want to change the generated files name, again use gulp-rename
  • If you want different destination for the files, use gulp-if
  • Unlike gulp-svg-sprites there is no way to add padding to svg files.

Other stuff

Migrating

See MIGRATING.md

More examples

Go in the examples folder, then npm install && gulp.
You will have a list of all task examples there

Usefull frontend lib

  • svg4everybody leverage external SVG for browser which doesn't support it

Credits

Alternatives