This repository was archived by the owner on Jul 2, 2024. It is now read-only.
forked from svg-sprite/gulp-svg-sprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (64 loc) · 1.83 KB
/
index.js
File metadata and controls
75 lines (64 loc) · 1.83 KB
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
'use strict';
/**
* gulp-svg-sprite is a Gulp plugin for creating SVG sprites
*
* @see https://github.com/jkphl/gulp-svg-sprite
*
* @author Joschi Kuphal <joschi@kuphal.net> (https://github.com/jkphl)
* @copyright © 2018 Joschi Kuphal
* @license MIT https://raw.github.com/jkphl/gulp-svg-sprite/master/LICENSE.txt
*/
var through2 = require('through2'),
PluginError = require('plugin-error'),
SVGSpriter = require('@fullstax/svg-sprite'),
PLUGIN_NAME = 'gulp-svg-sprite';
/**
* Plugin level function
*
* @param {Object} config SVGSpriter main configuration
*/
function gulpSVGSprite(config) {
// Extend plugin error
function extendError(pError, error) {
if (error && (typeof error === 'object')) {
['name', 'errno'].forEach(function(property) {
if (property in error) {
this[property] = error[property];
}
}, pError);
}
return pError;
}
// Instanciate spriter instance
var spriter = new SVGSpriter(config);
var shapes = 0;
// Intercept error log and convert to plugin errors
spriter.config.log.error = function(message, error) {
this.emit('error', extendError(new PluginError(PLUGIN_NAME, message), error));
};
return through2.obj(function (file, enc, cb) {
var error = null;
try {
spriter.add(file);
++shapes;
} catch(e) {
error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ? extendError(new PluginError(PLUGIN_NAME, e.message), e) : e;
}
return cb(error);
}, function(cb) {
var stream = this;
spriter.compile(function(error, result /*, data*/){
if (error) {
stream.emit('error', new PluginError(PLUGIN_NAME, error));
} else if (shapes > 0) {
for (var mode in result) {
for (var resource in result[mode]) {
stream.push(result[mode][resource]);
}
}
}
cb();
});
});
}
module.exports = gulpSVGSprite;