-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
144 lines (102 loc) · 2.72 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
const PLUGIN_NAME = 'gulp-svg-icons';
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var extend = require('node.extend');
var gutil = require('gulp-util');
var map = require('map-stream');
var Error = gutil.PluginError;
var error = function(right, message) {
if (!right) {
throw new Error(PLUGIN_NAME, message);
}
};
var Icons = function(dir, options) {
error(dir.constructor === String, 'Missing iconsDir option', true);
error(fs.existsSync(dir), 'iconsDir path not found (' + dir + ')', true);
var settings = extend({
injectOnlyUsedIcons: true,
prefix : 'icon',
placeholder : '<!-- icons -->',
style : function(name) {
return 'icon';
},
external : function(name) {
return '';
}
}, options);
this.dir = dir;
this.settings = settings;
this.prefix = settings.prefix.constructor === String
? function(name) {
return settings.prefix + '-' + name;
}
: function(name) {
return name;
};
this._init();
};
Icons.prototype._init = function(name) {
var self = this;
self._icons = '';
self._collected = [];
if (!self.settings.injectOnlyUsedIcons) {
glob.sync(path.join(self.dir, '*.svg')).forEach(function(file) {
self._collect(path.basename(file, '.svg'));
});
}
};
Icons.prototype._collect = function(name) {
if (this._collected.indexOf(name) < 0) {
var raw = String(fs.readFileSync(path.join(this.dir, name + '.svg')));
this._icons += [
'<symbol id="',
this.prefix(name),
'" ',
/\s(viewBox="[0-9\-\s\.]+")/.exec(raw)[1],
'>',
/<svg[^>]*>([\s\S]*?)<\/svg>/gi.exec(raw)[1],
'</symbol>'
].join('');
this._collected.push(name);
}
};
Icons.prototype.replace = function() {
var self = this;
return map(function(file, done) {
var contents = String(file.contents);
file.contents = new Buffer(contents.replace(/<icon-([a-z0-9\-]+)(?:\s+class="([a-z0-9\-\_ ]*)")?\/?>(?:\s*<\/icon-[a-z0-9\-]+>)?/gi, function(match, name, style) {
style = style ? ' ' + style : '';
if (self.settings.injectOnlyUsedIcons) {
self._collect(name);
}
return [
'<svg class="',
self.settings.style(name),
style,
'"><use xlink:href="',
self.settings.external(name),
'#',
self.prefix(name),
'"></use></svg>'
].join('');
}));
done(null, file);
});
};
Icons.prototype.inject = function() {
var self = this;
var icons = '<svg style="display:none;">' + self._icons + '</svg>';
var stream = map(function(file, done) {
file.contents = new Buffer(
String(file.contents)
.replace(self.settings.placeholder, icons)
);
done(null, file);
});
stream.on('end', function() {
self._init();
});
return stream;
};
module.exports = Icons;