gulp-stacksvg 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +11 -0
- package/README.md +144 -0
- package/index.js +199 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
===========
|
|
3
|
+
|
|
4
|
+
Copyright 2022 Sergey Artemov <firefoxic.dev@gmail.com>
|
|
5
|
+
Copyright 2014–2021 Andrey Kuzmin <unsoundscapes@gmail.com>
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
+
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# gulp-stacksvg
|
|
2
|
+
|
|
3
|
+
<img align="right" width="130" height="175" title="SVG Superman" src="https://raw.githubusercontent.com/firefoxic/gulp-stacksvg/master/svg-superman.png">
|
|
4
|
+
|
|
5
|
+
[![Test Status][test-image]][test-url]
|
|
6
|
+
[![License: MIT][license-image]][license-url]
|
|
7
|
+
[![NPM version][npm-image]][npm-url]
|
|
8
|
+
[![Vulnerabilities count][vulnerabilities-image]][vulnerabilities-url]
|
|
9
|
+
|
|
10
|
+
Combine svg files into one with stack method.
|
|
11
|
+
Read more about this in [the Simurai article](https://simurai.com/blog/2012/04/02/svg-stacks).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```shell
|
|
16
|
+
npm install gulp-stacksvg --save-dev
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Avalable options
|
|
20
|
+
|
|
21
|
+
| Option | Description | Default |
|
|
22
|
+
|-------------|--------------------------------------------------------------------------------------|-------------|
|
|
23
|
+
| `output` | Sets the stack file name. Accepts values both with and without the `.svg` extension. | `stack.svg` |
|
|
24
|
+
| `separator` | Replaces the directory separator for the `id` attribute. | `_` |
|
|
25
|
+
| `spacer` | Joins space-separated words for the `id` attribute. | `-` |
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
The following script will combine all svg sources into single svg file with stack method.
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import { stacksvg } from "gulp-stacksvg"
|
|
33
|
+
import gulp from "gulp"
|
|
34
|
+
|
|
35
|
+
const { src, dest } = gulp
|
|
36
|
+
|
|
37
|
+
function makeStack () {
|
|
38
|
+
return src(`./src/icons/**/*.svg`)
|
|
39
|
+
.pipe(stacksvg())
|
|
40
|
+
.pipe(dest(`./dest/icons`))
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Inlining stacksvg result into markup
|
|
45
|
+
|
|
46
|
+
You just don't have to want it.
|
|
47
|
+
|
|
48
|
+
### Editing id attributes
|
|
49
|
+
|
|
50
|
+
If you need to add prefix to each id, please use [gulp-rename](https://github.com/hparra/gulp-rename):
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
import { stacksvg } from "gulp-stacksvg"
|
|
54
|
+
import rename from "gulp-rename"
|
|
55
|
+
import gulp from "gulp"
|
|
56
|
+
|
|
57
|
+
const { src, dest } = gulp
|
|
58
|
+
|
|
59
|
+
function makeStack () {
|
|
60
|
+
return src(`./src/icons/**/*.svg`, { base: `src/icons` })
|
|
61
|
+
.pipe(rename({ prefix: `icon-` }))
|
|
62
|
+
.pipe(stacksvg())
|
|
63
|
+
.pipe(dest(`./dest/icons`))
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Transform svg sources or combined svg
|
|
68
|
+
|
|
69
|
+
To transform either svg sources or combined svg you may pipe your files through [gulp-cheerio](https://github.com/KenPowers/gulp-cheerio).
|
|
70
|
+
|
|
71
|
+
An example below removes all fill attributes from svg sources before combining them.
|
|
72
|
+
Please note that you have to set `xmlMode: true` to parse svgs as xml file.
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
import { stacksvg } from "gulp-stacksvg"
|
|
76
|
+
import cheerio from "gulp-cheerio"
|
|
77
|
+
import gulp from "gulp"
|
|
78
|
+
|
|
79
|
+
const { src, dest } = gulp
|
|
80
|
+
|
|
81
|
+
function makeStack () {
|
|
82
|
+
return src(`./src/icons/**/*.svg`)
|
|
83
|
+
.pipe(cheerio({
|
|
84
|
+
run: ($) => {
|
|
85
|
+
$(`[fill]`).removeAttr(`fill`)
|
|
86
|
+
},
|
|
87
|
+
parserOptions: { xmlMode: true }
|
|
88
|
+
}))
|
|
89
|
+
.pipe(stacksvg())
|
|
90
|
+
.pipe(dest(`./dest/icons`))
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Possible rendering issues with Clipping Paths in SVG
|
|
95
|
+
|
|
96
|
+
If 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:
|
|
97
|
+
|
|
98
|
+
### Correcting the Clipping Path in the SVG
|
|
99
|
+
|
|
100
|
+
If 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.
|
|
101
|
+
|
|
102
|
+
### Editing the SVG Code
|
|
103
|
+
|
|
104
|
+
If 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 `<clipPath>` into a `<defs>` will fix this issue. Here's an example:
|
|
105
|
+
|
|
106
|
+
```diff
|
|
107
|
+
<defs>
|
|
108
|
+
<path d="M28.4 30.5l5.3 5c0-.1 7-6.9 7-6.9l-4-6.8-8.3 8.7z" id="a"/>
|
|
109
|
+
+ <clipPath id="b">
|
|
110
|
+
+ <use overflow="visible" href="#a"/>
|
|
111
|
+
+ </clipPath>
|
|
112
|
+
</defs>
|
|
113
|
+
-<clipPath id="b">
|
|
114
|
+
- <use overflow="visible" xlink:href="#a"/>
|
|
115
|
+
-</clipPath>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Or you can go further and reduce the size by removing the `<use>` element, like this:
|
|
119
|
+
|
|
120
|
+
```diff
|
|
121
|
+
<defs>
|
|
122
|
+
- <path d="M28.4 30.5l5.3 5c0-.1 7-6.9 7-6.9l-4-6.8-8.3 8.7z" id="a"/>
|
|
123
|
+
<clipPath id="b">
|
|
124
|
+
- <use overflow="visible" href="#a"/>
|
|
125
|
+
+ <path d="M28.4 30.5l5.3 5c0-.1 7-6.9 7-6.9l-4-6.8-8.3 8.7z"/>
|
|
126
|
+
</clipPath>
|
|
127
|
+
</defs>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Using gulp-cheerio to automate this
|
|
131
|
+
|
|
132
|
+
Another possible solution would be to write a transformation with [gulp-cheerio](https://github.com/KenPowers/gulp-cheerio). Check this issue <https://github.com/firefoxic/gulp-stacksvg/issues/98> for the instructions.
|
|
133
|
+
|
|
134
|
+
[test-url]: https://github.com/firefoxic/gulp-stacksvg/actions
|
|
135
|
+
[test-image]: https://github.com/firefoxic/gulp-stacksvg/actions/workflows/test.yml/badge.svg?branch=main
|
|
136
|
+
|
|
137
|
+
[npm-url]: https://npmjs.org/package/gulp-stacksvg
|
|
138
|
+
[npm-image]: https://badge.fury.io/js/gulp-stacksvg.svg
|
|
139
|
+
|
|
140
|
+
[license-url]: https://github.com/firefoxic/gulp-stacksvg/blob/main/LICENSE
|
|
141
|
+
[license-image]: https://img.shields.io/badge/License-MIT-limegreen.svg
|
|
142
|
+
|
|
143
|
+
[vulnerabilities-url]: https://snyk.io/test/github/firefoxic/gulp-stacksvg
|
|
144
|
+
[vulnerabilities-image]: https://snyk.io/test/github/firefoxic/gulp-stacksvg/badge.svg
|
package/index.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { load } from "cheerio"
|
|
2
|
+
import { basename, extname, sep } from "path"
|
|
3
|
+
import { Transform } from "stream"
|
|
4
|
+
import fancyLog from "fancy-log"
|
|
5
|
+
import PluginError from "plugin-error"
|
|
6
|
+
import Vinyl from "vinyl"
|
|
7
|
+
|
|
8
|
+
const presentationAttributes = new Set([
|
|
9
|
+
`alignment-baseline`,
|
|
10
|
+
`baseline-shift`,
|
|
11
|
+
`clip-path`,
|
|
12
|
+
`clip-rule`,
|
|
13
|
+
`clip`,
|
|
14
|
+
`color-interpolation-filters`,
|
|
15
|
+
`color-interpolation`,
|
|
16
|
+
`color-profile`,
|
|
17
|
+
`color-rendering`,
|
|
18
|
+
`color`,
|
|
19
|
+
`cursor`,
|
|
20
|
+
`d`,
|
|
21
|
+
`direction`,
|
|
22
|
+
`display`,
|
|
23
|
+
`dominant-baseline`,
|
|
24
|
+
`enable-background`,
|
|
25
|
+
`fill-opacity`,
|
|
26
|
+
`fill-rule`,
|
|
27
|
+
`fill`,
|
|
28
|
+
`filter`,
|
|
29
|
+
`flood-color`,
|
|
30
|
+
`flood-opacity`,
|
|
31
|
+
`font-family`,
|
|
32
|
+
`font-size-adjust`,
|
|
33
|
+
`font-size`,
|
|
34
|
+
`font-stretch`,
|
|
35
|
+
`font-style`,
|
|
36
|
+
`font-variant`,
|
|
37
|
+
`font-weight`,
|
|
38
|
+
`glyph-orientation-horizontal`,
|
|
39
|
+
`glyph-orientation-vertical`,
|
|
40
|
+
`image-rendering`,
|
|
41
|
+
`kerning`,
|
|
42
|
+
`letter-spacing`,
|
|
43
|
+
`lighting-color`,
|
|
44
|
+
`marker-end`,
|
|
45
|
+
`marker-mid`,
|
|
46
|
+
`marker-start`,
|
|
47
|
+
`mask`,
|
|
48
|
+
`opacity`,
|
|
49
|
+
`overflow`,
|
|
50
|
+
`pointer-events`,
|
|
51
|
+
`shape-rendering`,
|
|
52
|
+
`solid-color`,
|
|
53
|
+
`solid-opacity`,
|
|
54
|
+
`stop-color`,
|
|
55
|
+
`stop-opacity`,
|
|
56
|
+
`stroke-dasharray`,
|
|
57
|
+
`stroke-dashoffset`,
|
|
58
|
+
`stroke-linecap`,
|
|
59
|
+
`stroke-linejoin`,
|
|
60
|
+
`stroke-miterlimit`,
|
|
61
|
+
`stroke-opacity`,
|
|
62
|
+
`stroke-width`,
|
|
63
|
+
`stroke`,
|
|
64
|
+
`style`,
|
|
65
|
+
`text-anchor`,
|
|
66
|
+
`text-decoration`,
|
|
67
|
+
`text-rendering`,
|
|
68
|
+
`transform`,
|
|
69
|
+
`unicode-bidi`,
|
|
70
|
+
`vector-effect`,
|
|
71
|
+
`visibility`,
|
|
72
|
+
`word-spacing`,
|
|
73
|
+
`writing-mode`
|
|
74
|
+
])
|
|
75
|
+
|
|
76
|
+
export function stacksvg (options) {
|
|
77
|
+
|
|
78
|
+
options = options || {}
|
|
79
|
+
|
|
80
|
+
const ids = {}
|
|
81
|
+
const namespaces = {}
|
|
82
|
+
const separator = options.separator ?? `_`
|
|
83
|
+
const spacer = options.spacer ?? `-`
|
|
84
|
+
|
|
85
|
+
let resultSvg = `<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><style>:root{visibility:hidden}:target{visibility:visible}</style><defs/></svg>`
|
|
86
|
+
let isEmpty = true
|
|
87
|
+
let fileName = options.output || `stack.svg`
|
|
88
|
+
|
|
89
|
+
fileName = fileName.endsWith(`.svg`) ? fileName : `${fileName}.svg`
|
|
90
|
+
|
|
91
|
+
const $ = load(resultSvg, { xmlMode: true })
|
|
92
|
+
const $combinedSvg = $(`svg`)
|
|
93
|
+
const $combinedDefs = $(`defs`)
|
|
94
|
+
const stream = new Transform({ objectMode: true })
|
|
95
|
+
|
|
96
|
+
stream._transform = function transform (file, _, cb) {
|
|
97
|
+
|
|
98
|
+
if (file.isStream()) {
|
|
99
|
+
return cb(new PluginError(`gulp-stacksvg`, `Streams are not supported!`))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (file.isNull()) {return cb()}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
const $svg = load(file.contents.toString(), { xmlMode: true })(`svg`)
|
|
106
|
+
|
|
107
|
+
if ($svg.length === 0) {return cb()}
|
|
108
|
+
|
|
109
|
+
const idAttr = basename(
|
|
110
|
+
file.relative.split(sep).join(separator).replace(/\s/g, spacer),
|
|
111
|
+
extname(file.relative)
|
|
112
|
+
)
|
|
113
|
+
const viewBoxAttr = $svg.attr(`viewBox`)
|
|
114
|
+
const widthAttr = $svg.attr(`width`)
|
|
115
|
+
const heightAttr = $svg.attr(`height`)
|
|
116
|
+
const preserveAspectRatioAttr = $svg.attr(`preserveAspectRatio`)
|
|
117
|
+
const $icon = $(`<svg/>`)
|
|
118
|
+
|
|
119
|
+
if (idAttr in ids) {
|
|
120
|
+
return cb(new PluginError(`gulp-stacksvg`, `File name should be unique: ${idAttr}`))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
ids[idAttr] = true
|
|
124
|
+
|
|
125
|
+
if (file && isEmpty) {
|
|
126
|
+
isEmpty = false
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
$icon.attr(`id`, idAttr)
|
|
130
|
+
if (viewBoxAttr) {
|
|
131
|
+
$icon.attr(`viewBox`, viewBoxAttr)
|
|
132
|
+
} else if (widthAttr && heightAttr) {
|
|
133
|
+
$icon.attr(`viewBox`, `0 0 ${widthAttr.replace(/[^0-9]/g,``)} ${heightAttr.replace(/[^0-9]/g,``)}`)
|
|
134
|
+
}
|
|
135
|
+
if (preserveAspectRatioAttr) {
|
|
136
|
+
$icon.attr(`preserveAspectRatio`, preserveAspectRatioAttr)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const attrs = $svg[0].attribs
|
|
140
|
+
for (let attrName in attrs) {
|
|
141
|
+
if (attrName.match(/xmlns:.+/)) {
|
|
142
|
+
const storedNs = namespaces[attrName]
|
|
143
|
+
const attrNs = attrs[attrName]
|
|
144
|
+
|
|
145
|
+
if (storedNs !== undefined) {
|
|
146
|
+
if (storedNs !== attrNs) {
|
|
147
|
+
fancyLog.info(
|
|
148
|
+
`${attrName} namespace appeared multiple times with different value. Keeping the first one : "${storedNs}".\nEach namespace must be unique across files.`
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
for (let nsName in namespaces) {
|
|
153
|
+
if (namespaces[nsName] === attrNs) {
|
|
154
|
+
fancyLog.info(`Same namespace value under different names : ${nsName} and ${attrName}.\nKeeping both.`)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
namespaces[attrName] = attrNs
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const $defs = $svg.find(`defs`)
|
|
163
|
+
if ($defs.length > 0) {
|
|
164
|
+
$combinedDefs.append($defs.contents())
|
|
165
|
+
$defs.remove()
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let $groupWrap = null
|
|
169
|
+
for (let [name, value] of Object.entries($svg.attr())) {
|
|
170
|
+
if (!presentationAttributes.has(name)) {continue}
|
|
171
|
+
if (!$groupWrap) {$groupWrap = $(`<g/>`)}
|
|
172
|
+
$groupWrap.attr(name, value)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if ($groupWrap) {
|
|
176
|
+
$groupWrap.append($svg.contents())
|
|
177
|
+
$icon.append($groupWrap)
|
|
178
|
+
} else {
|
|
179
|
+
$icon.append($svg.contents())
|
|
180
|
+
}
|
|
181
|
+
$combinedSvg.append($icon)
|
|
182
|
+
cb()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
stream._flush = function flush (cb) {
|
|
186
|
+
if (isEmpty) {return cb()}
|
|
187
|
+
if ($combinedDefs.contents().length === 0) {
|
|
188
|
+
$combinedDefs.remove()
|
|
189
|
+
}
|
|
190
|
+
for (let nsName in namespaces) {
|
|
191
|
+
$combinedSvg.attr(nsName, namespaces[nsName])
|
|
192
|
+
}
|
|
193
|
+
const file = new Vinyl({ path: fileName, contents: Buffer.from($.xml()) })
|
|
194
|
+
this.push(file)
|
|
195
|
+
cb()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return stream
|
|
199
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gulp-stacksvg",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Combine svg files into one with stack method",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"lint": "eslint ./",
|
|
12
|
+
"test": "gulp build && mocha",
|
|
13
|
+
"pretest": "npm run lint",
|
|
14
|
+
"preversion": "npm test",
|
|
15
|
+
"postversion": "npm publish",
|
|
16
|
+
"postpublish": "git push origin --all; git push origin --tags"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git://github.com/firefoxic/gulp-stacksvg"
|
|
21
|
+
},
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Sergey Artemov",
|
|
24
|
+
"email": "firefoxic.dev@gmail.com"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/firefoxic/gulp-stacksvg/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/firefoxic/gulp-stacksvg",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"cheerio": "^1.0.0-rc.12",
|
|
33
|
+
"fancy-log": "^2.0.0",
|
|
34
|
+
"plugin-error": "^2.0.0",
|
|
35
|
+
"vinyl": "^2.2.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"eslint": "^8.22.0",
|
|
39
|
+
"finalhandler": "^1.2.0",
|
|
40
|
+
"gulp": "^4.0.2",
|
|
41
|
+
"mocha": "^10.0.0",
|
|
42
|
+
"puppeteer": "^16.2.0",
|
|
43
|
+
"serve-static": "^1.15.0",
|
|
44
|
+
"sinon": "^14.0.0"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": "16"
|
|
48
|
+
},
|
|
49
|
+
"engineStrict": true,
|
|
50
|
+
"keywords": [
|
|
51
|
+
"gulpplugin",
|
|
52
|
+
"svg",
|
|
53
|
+
"icon",
|
|
54
|
+
"stack",
|
|
55
|
+
"sprite"
|
|
56
|
+
]
|
|
57
|
+
}
|