-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (53 loc) · 1.57 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
'use strict';
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const SVGSpriter = require('svg-sprite');
class SvgSpriteCompiler {
constructor(config) {
this.config = config.plugins.svgsprite;
this.files = {};
this.dirty = false;
}
compile(data, file, callback) {
this.dirty = true;
this.files[file] = data;
return callback(null, null);
}
onCompile(files) {
var spriter = null;
for (var file in this.files) {
if (fs.existsSync(file)) {
(spriter || (spriter = new SVGSpriter(this.config))).add(path.resolve(file), null, this.files[file]);
} else {
delete this.files[file];
// Force compilation
this.dirty = true;
}
}
if (spriter && this.dirty) {
spriter.compile(function(error, result) {
for (var mode in result) {
for (var resource in result[mode]) {
mkdirp.sync(path.dirname(result[mode][resource].path));
fs.writeFileSync(result[mode][resource].path, result[mode][resource].contents);
}
}
});
} else if (this.dirty) {
// There are no SVG files, remove sprite
var symbol = this.config.mode.symbol;
var file = path.resolve(symbol.dest, symbol.sprite);
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
} else {
return;
}
this.dirty = false;
}
}
SvgSpriteCompiler.prototype.brunchPlugin = true;
SvgSpriteCompiler.prototype.type = 'template';
SvgSpriteCompiler.prototype.extension = 'svg';
module.exports = SvgSpriteCompiler;