forked from Daan-/broccoli-svg-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (47 loc) · 1.41 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
'use strict';
var Filter = require('broccoli-persistent-filter');
var stringify = require('json-stable-stringify');
var RSVP = require('rsvp');
var SVGO = require('svgo');
function SVGOFilter(inputNode, _options) {
var options = _options || {};
if (!options.hasOwnProperty('persist')) {
options.persist = true;
}
Filter.call(this, inputNode, options);
this.options = options;
this.svgo = new SVGO(options.svgoConfig);
}
SVGOFilter.prototype = Object.create(Filter.prototype);
SVGOFilter.prototype.constructor = SVGOFilter;
SVGOFilter.prototype.extensions = ['svg'];
SVGOFilter.prototype.targetExtension = 'svg';
SVGOFilter.prototype.baseDir = function() {
return __dirname;
};
SVGOFilter.prototype.processString = function(svgContent) {
var svgo = this.svgo;
if (!svgContent) {
return '';
}
return new RSVP.Promise(function(resolve, reject) {
svgo.optimize(svgContent, function(result) {
if (result.error) {
reject(result.error);
} else {
resolve(result.data);
}
});
});
};
SVGOFilter.prototype.optionsHash = function() {
if (!this._optionsHash) {
this._optionsHash = stringify(this.options);
}
return this._optionsHash;
};
SVGOFilter.prototype.cacheKeyProcessString = function(string, relativePath) {
return Filter.prototype.cacheKeyProcessString.call(
this, string + this.optionsHash(), relativePath);
};
module.exports = SVGOFilter;