-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
54 lines (39 loc) · 1.6 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
'use strict';
var SVGSpriter = require('svg-sprite'),
path = require('path'),
mkdirp = require('mkdirp'),
fs = require('fs'),
glob = require('glob'),
_ = require('lodash');
function SVGSprites(options) {
this.options = options || {};
}
SVGSprites.prototype.apply = function (compiler) {
var self = this;
compiler.plugin('compile', function () {
for (var bundle in self.options['sprites_conf']) {
var retfunc = function (bundle, local_conf) {
return function (err, files) {
let local_conf = _.merge(self.options, self.options['sprites_conf'][bundle]);
let spriter = new SVGSpriter(local_conf)
files.forEach(function (file) {
// Create and add file instance for each SVG
spriter.add(path.resolve(file), file, fs.readFileSync(path.resolve(file), {encoding: 'utf-8'}));
});
// Compile the sprite
spriter.compile(function (error, result, data) {
for (var mode in result) {
for (var type in result[mode]) {
mkdirp.sync(path.dirname(result[mode][type].path));
fs.writeFileSync(result[mode][type].path, result[mode][type].contents);
}
}
});
}
}
glob.glob(self.options.src + bundle + '**/*.svg', retfunc(bundle))
}
;
});
}
module.exports = SVGSprites;