-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
163 lines (138 loc) · 5.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const cheerio = require('cheerio')
const path = require('path')
const Stream = require('stream')
const fancyLog = require('fancy-log')
const PluginError = require('plugin-error')
const Vinyl = require('vinyl')
const presentationAttributes = new Set([
'style', 'alignment-baseline', 'baseline-shift', 'clip', 'clip-path',
'clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters',
'color-profile', 'color-rendering', 'cursor', 'd', 'direction', 'display',
'dominant-baseline', 'enable-background', 'fill', 'fill-opacity', 'fill-rule',
'filter', 'flood-color', 'flood-opacity', 'font-family', 'font-size',
'font-size-adjust', 'font-stretch', 'font-style', 'font-variant',
'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical',
'image-rendering', 'kerning', 'letter-spacing', 'lighting-color',
'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'overflow',
'pointer-events', 'shape-rendering', 'solid-color', 'solid-opacity',
'stop-color', 'stop-opacity', 'stroke', 'stroke-dasharray',
'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit',
'stroke-opacity', 'stroke-width', 'text-anchor', 'text-decoration',
'text-rendering', 'transform', 'unicode-bidi', 'vector-effect', 'visibility',
'word-spacing', 'writing-mode'
]);
module.exports = function (config) {
config = config || {}
const namespaces = {}
let isEmpty = true
let fileName
const inlineSvg = config.inlineSvg || false
const ids = {}
let resultSvg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs/></svg>'
if (!inlineSvg) {
resultSvg =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' +
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' +
resultSvg
}
const $ = cheerio.load(resultSvg, { xmlMode: true })
const $combinedSvg = $('svg')
const $combinedDefs = $('defs')
const stream = new Stream.Transform({ objectMode: true })
stream._transform = function transform (file, _, cb) {
if (file.isStream()) {
return cb(new PluginError('gulp-svgstore', 'Streams are not supported!'))
}
if (file.isNull()) return cb()
const $svg = cheerio.load(file.contents.toString(), { xmlMode: true })('svg')
if ($svg.length === 0) return cb()
const idAttr = path.basename(file.relative, path.extname(file.relative))
const viewBoxAttr = $svg.attr('viewBox')
const preserveAspectRatioAttr = $svg.attr('preserveAspectRatio')
const $symbol = $('<symbol/>')
if (idAttr in ids) {
return cb(new PluginError('gulp-svgstore', 'File name should be unique: ' + idAttr))
}
ids[idAttr] = true
if (!fileName) {
fileName = path.basename(file.base)
if (fileName === '.' || !fileName) {
fileName = 'svgstore.svg'
} else {
fileName = fileName.split(path.sep).shift() + '.svg'
}
}
if (file && isEmpty) {
isEmpty = false
}
$symbol.attr('id', idAttr)
if (viewBoxAttr) {
$symbol.attr('viewBox', viewBoxAttr)
}
if (preserveAspectRatioAttr) {
$symbol.attr('preserveAspectRatio', preserveAspectRatioAttr)
}
const attrs = $svg[0].attribs
for (let attrName in attrs) {
if (attrName.match(/xmlns:.+/)) {
const storedNs = namespaces[attrName]
const attrNs = attrs[attrName]
if (storedNs !== undefined) {
if (storedNs !== attrNs) {
fancyLog.info(
attrName + ' namespace appeared multiple times with different value.' +
' Keeping the first one : "' + storedNs +
'".\nEach namespace must be unique across files.'
)
}
} else {
for (let nsName in namespaces) {
if (namespaces[nsName] === attrNs) {
fancyLog.info(
'Same namespace value under different names : ' +
nsName +
' and ' +
attrName +
'.\nKeeping both.'
)
}
}
namespaces[attrName] = attrNs;
}
}
}
const $defs = $svg.find('defs')
if ($defs.length > 0) {
$combinedDefs.append($defs.contents())
$defs.remove()
}
let $groupWrap = null
for (let [name, value] of Object.entries($svg.attr())) {
if (!presentationAttributes.has(name)) continue;
if (!$groupWrap) $groupWrap = $('<g/>')
$groupWrap.attr(name, value)
}
if ($groupWrap) {
$groupWrap.append($svg.contents())
$symbol.append($groupWrap)
} else {
$symbol.append($svg.contents())
}
$combinedSvg.append($symbol)
cb()
}
stream._flush = function flush (cb) {
if (isEmpty) return cb()
if ($combinedDefs.contents().length === 0) {
$combinedDefs.remove()
}
for (let nsName in namespaces) {
$combinedSvg.attr(nsName, namespaces[nsName])
}
const file = new Vinyl({ path: fileName, contents: Buffer.from($.xml()) })
this.push(file)
cb()
}
return stream;
}