Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/haensl/gulp-embed-svg
Gulp plugin to inline/embed SVG images into html files.
https://github.com/haensl/gulp-embed-svg
embed embed-svg-images embedding gulp gulp-plugins gulpplugin svg svg-files svg-images svg-source
Last synced: 2 months ago
JSON representation
Gulp plugin to inline/embed SVG images into html files.
- Host: GitHub
- URL: https://github.com/haensl/gulp-embed-svg
- Owner: haensl
- License: mit
- Created: 2017-11-19T23:50:18.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-18T23:20:35.000Z (3 months ago)
- Last Synced: 2024-10-19T02:28:48.712Z (3 months ago)
- Topics: embed, embed-svg-images, embedding, gulp, gulp-plugins, gulpplugin, svg, svg-files, svg-images, svg-source
- Language: JavaScript
- Size: 326 KB
- Stars: 7
- Watchers: 4
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# gulp-embed-svg
[![NPM](https://nodei.co/npm/gulp-embed-svg.png?downloads=true)](https://nodei.co/npm/gulp-embed-svg/)
[![npm version](https://badge.fury.io/js/gulp-embed-svg.svg)](http://badge.fury.io/js/gulp-embed-svg)
[![CircleCI](https://circleci.com/gh/haensl/gulp-embed-svg.svg?style=svg)](https://circleci.com/gh/haensl/gulp-embed-svg)Gulp plugin to inline/embed SVG images into html files.
## Features
* Inline/embed any images with an SVG source attribute (i.e. ``) and `` tags with a `src` attribute (i.e. ``).
* Preserves all/select attributes via RegEx.
* Optionally create a spritesheet from the inlined SVGs.
## Installation
```shell
npm i --save-dev gulp-embed-svg
```## Quick Start
```javascript
const embedSvg = require('gulp-embed-svg');gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg())
.pipe(gulp.dest('dist/')));
```This gulp task will inline/embed any images with an SVG source attribute (i.e. ``) and `` tags with a `src` attribute.
## Options
* [`attrs`](#attrs): Provide a regular expression to transfer select attributes from matched tags to embedded ``s.
* [`createSpritesheet`](#create-spritesheet): Set to `true` to embed SVGs via a spritesheet. This reduces generated HTML filesize if you use the same SVG several times on a page.
* [`decodeEntities`](#decode-entities): Set to `true` to decode HTML entities within the document.
* [`root`](#root): Provide the root folder where SVG source images are located.
* [`selectors`](#selectors): Provide custom CSS selectors to specify which tags should be replaced by embedded SVGs.
* [`spriteIdFn`](#sprite-id-fn): Customize the `id` assigned to the sprites by providing a function that resolves path and index to a string.
* [`spritesheetClass`](#spritesheet-class): Customize the CSS class assigned to the generated spritesheet.
* [`xmlMode`](#xmlmode): Whether or not to read files in xml mode.### selectors `string | Array`
Provide custom CSS selectors to specify which tags should be replaced by embedded SVGs.
#### default: `['img[src$=".svg"]', 'svg[src$=".svg"]']`
All `` and `` tags with an svg source.
#### Example: Only embed tags with a specific class
**HTML layout**
```html
```
**Gulp task**
```javascript
const embedSvg = require('gulp-embed-svg');gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg({
selectors: '.inline-svg' // only replace tags with the class inline-svg
}))
.pipe(gulp.dest('dist/')));
```**Output**
```html
```
Provide a regular expression to transfer select attributes from matched tags to embedded ``s.
**Attention:** Attributes from matched tags take precedence over corresponding attributes in the source `.svg` file.
#### default: `^(?!src).*$`
Transfer/preserve any attribute **but** `src`.
#### Example: Preserve/transfer specific attribute
**HTML layout**
```html
```
**Gulp task**
```javascript
const embedSvg = require('gulp-embed-svg');gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg({
attrs: /class/ // only transfer/preserve class attribute
}))
.pipe(gulp.dest('dist/')));
```**Output**
```html
```
Set to `true` to decode HTML entities within the document.
#### default: `false`
##### Example: Replace potential entities in document with html entities
**HTML layout**
```html
Foo © bar 𝌆 baz ☃ qux
```
**Gulp task**
```javascript
const embedSvg = require('gulp-embed-svg');gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg({
decodeEntities: true
}))
.pipe(gulp.dest('dist/')));
```**Output**
```html
Foo © bar 𝌆 baz ☃ qux
```
Provide the root folder where SVG source images are located.
#### default: [`__dirname`](https://nodejs.org/docs/latest/api/globals.html#globals_dirname)
The folder in which the task is executed.
#### Example: Alternate svg root
**HTML layout**
```html
```
**Folder structure**
```bash
/src
index.html
gulpfile.js
/assets
github-icon.svg
```**Gulp task**
```javascript
const embedSvg = require('gulp-embed-svg');gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg({
root: './assets'
}))
.pipe(gulp.dest('dist/')));
```### createSpritesheet `boolean`
Set to `true` to embed SVGs via a spritesheet. This reduces generated HTML filesize if you use the same SVG several times on a page.
#### default: `false`
**Attention:** If your SVGs contain gradients, please make sure their respective `id` attribute values are unique among *all* gradients that are embedded on your page. The reason behind this is that, in order to support most (if not all) browsers, we need to extract gradient definitions from the SVGs and save them separately in the spritesheet. If there are duplicate gradient `id`s, the browser is having a hard time determining which one to use.
#### Example: Create an svg spritesheet
**HTML Layout**
```html
```
**Folder structure**
```bash
/src
index.html
gulpfile.js
/assets
github-icon.svg
```**Gulp task**
```javascript
const embedSvg = require('gulp-embed-svg');gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg({
root: './assets',
createSpritesheet: true
}))
.pipe(gulp.dest('dist/')));
```**Output**
```html
```
Customize the CSS class assigned to the generated spritesheet.
#### default: `svg-sprites`
#### Example: Change spritesheet class to my-sprites
**HTML Layout**
```html
```
**Folder structure**
```bash
/src
index.html
gulpfile.js
/assets
github-icon.svg
```**Gulp task**
```javascript
const embedSvg = require('gulp-embed-svg');gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg({
root: './assets',
createSpritesheet: true,
spritesheetClass: 'my-sprites'
}))
.pipe(gulp.dest('dist/')));
```**Output**
```html
```
Customize the `id` assigned to the sprites by providing a function that resolves path and index to a string. The function receives the absolute/resolved path to the source SVG file as well as the index of the sprite within the page as parameters.
#### default: ``(path, i) => `svg-sprite-${i}` ``
#### Example: Use the filename as id
**HTML Layout**
```html
```
**Folder structure**
```bash
/src
index.html
gulpfile.js
/assets
github-icon.svg
javascript-icon.svg
```**Gulp task**
```javascript
const embedSvg = require('gulp-embed-svg');
const basename = require('path').basename;gulp.task('embedSvgs', () =>
gulp.src('*.html')
.pipe(embedSvg({
root: './assets',
createSpritesheet: true,
spriteIdFn: (path, i) => basename(path, '.svg')
}))
.pipe(gulp.dest('dist/')));
```**Output**
```html
```
Flag to toggle [xml mode](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode).
#### default: `true`
## [Changelog](CHANGELOG.md)
## [License](LICENSE)