broccoli-svg-optimizer
Advanced tools
Comparing version 1.0.2 to 1.1.0
97
index.js
'use strict'; | ||
var Filter = require('broccoli-persistent-filter'); | ||
var stringify = require('json-stable-stringify'); | ||
var RSVP = require('rsvp'); | ||
var SVGO = require('svgo'); | ||
const PersistentFilter = require('broccoli-persistent-filter'); | ||
const _ = require('lodash'); | ||
const stringify = require('json-stable-stringify'); | ||
const { Promise } = require('rsvp'); | ||
const DefaultSVGO = require('svgo'); | ||
function SVGOFilter(inputNode, _options) { | ||
var options = _options || {}; | ||
function promisify(optimize) { | ||
return (svg) => { | ||
return new Promise((resolve, reject) => { | ||
optimize(svg, (result) => { | ||
if (result.error) { | ||
reject(result.error); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
}; | ||
} | ||
if (!options.hasOwnProperty('persist')) { | ||
options.persist = true; | ||
function promisifyIfNeeded(optimize) { | ||
let isPromise = false; | ||
try { | ||
isPromise = 'then' in optimize(''); | ||
} catch (e) { | ||
// pass | ||
} | ||
Filter.call(this, inputNode, options); | ||
this.options = options; | ||
this.svgo = new SVGO(options.svgoConfig); | ||
return isPromise ? optimize : promisify(optimize); | ||
} | ||
SVGOFilter.prototype = Object.create(Filter.prototype); | ||
SVGOFilter.prototype.constructor = SVGOFilter; | ||
SVGOFilter.prototype.extensions = ['svg']; | ||
SVGOFilter.prototype.targetExtension = 'svg'; | ||
class SVGOFilter extends PersistentFilter { | ||
constructor(inputNode, options) { | ||
options = options || {}; | ||
SVGOFilter.prototype.baseDir = function() { | ||
return __dirname; | ||
}; | ||
super(inputNode, { | ||
name: 'SVGOFilter', | ||
extensions: ['svg'], | ||
targetExtension: 'svg', | ||
persist: _.isUndefined(options.persist) ? true : options.persist, | ||
async: options.async, | ||
annotation: options.annotation | ||
}); | ||
SVGOFilter.prototype.processString = function(svgContent) { | ||
var svgo = this.svgo; | ||
let SVGO = options.svgoModule || DefaultSVGO; | ||
let svgo = new SVGO(options.svgoConfig); | ||
let optimize = svgo.optimize.bind(svgo); | ||
this.optimize = promisifyIfNeeded(optimize); | ||
this.optionsHash = stringify(options); | ||
} | ||
if (!svgContent) { | ||
return ''; | ||
processString(svg) { | ||
return svg | ||
? this.optimize(svg).then(({ data }) => data) | ||
: Promise.resolve(''); | ||
} | ||
return new RSVP.Promise(function(resolve, reject) { | ||
svgo.optimize(svgContent, function(result) { | ||
if (result.error) { | ||
reject(result.error); | ||
} else { | ||
resolve(result.data); | ||
} | ||
}); | ||
}); | ||
}; | ||
cacheKeyProcessString(string, relativePath) { | ||
return super.cacheKeyProcessString(string + this.optionsHash, relativePath); | ||
} | ||
SVGOFilter.prototype.optionsHash = function() { | ||
if (!this._optionsHash) { | ||
this._optionsHash = stringify(this.options); | ||
baseDir() { | ||
return __dirname; | ||
} | ||
} | ||
return this._optionsHash; | ||
}; | ||
SVGOFilter.prototype.cacheKeyProcessString = function(string, relativePath) { | ||
return Filter.prototype.cacheKeyProcessString.call( | ||
this, string + this.optionsHash(), relativePath); | ||
}; | ||
module.exports = SVGOFilter; |
{ | ||
"name": "broccoli-svg-optimizer", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Broccoli plugin for optimizing SVG files with SVGO", | ||
"scripts": { | ||
"test": "mocha test/ --timeout 8000" | ||
"prepublishOnly": "npm test", | ||
"lint": "eslint ./*.js tests", | ||
"nodetest": "mocha tests/*test.js --timeout 8000", | ||
"test": "npm run lint && npm run nodetest" | ||
}, | ||
@@ -27,15 +30,25 @@ "main": "index.js", | ||
"json-stable-stringify": "^1.0.1", | ||
"rsvp": "^3.2.1", | ||
"svgo": "^0.6.6" | ||
"lodash": "^4.17.10", | ||
"rsvp": "^4.8.2", | ||
"svgo": "0.6.6" | ||
}, | ||
"devDependencies": { | ||
"broccoli": "^0.16.9", | ||
"broccoli-fixture": "^0.1.0", | ||
"chai": "^3.5.0", | ||
"chai-as-promised": "^5.3.0", | ||
"eslint-config-airbnb-base": "^3.0.1", | ||
"eslint-plugin-import": "^1.8.0", | ||
"mocha": "^2.5.1", | ||
"mocha-eslint": "^2.0.2" | ||
"broccoli": "^1.1.4", | ||
"broccoli-fixture": "^1.0.0", | ||
"chai": "^4.1.2", | ||
"chai-as-promised": "^7.1.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb-base": "^12.1.0", | ||
"eslint-plugin-import": "^2.11.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"mocha": "^5.1.1" | ||
}, | ||
"engines": { | ||
"node": "6.* || >= 8.*" | ||
}, | ||
"greenkeeper": { | ||
"ignore": [ | ||
"svgo" | ||
] | ||
} | ||
} |
@@ -5,2 +5,3 @@ # broccoli-svg-optimizer | ||
[![Build status](https://ci.appveyor.com/api/projects/status/26lyufkk6ueam952/branch/master?svg=true)](https://ci.appveyor.com/project/ivanvotti/broccoli-svg-optimizer) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/ivanvotti/broccoli-svg-optimizer.svg)](https://greenkeeper.io/) | ||
@@ -19,3 +20,3 @@ Broccoli plugin for optimizing SVG files by [SVGO](https://github.com/svg/svgo) with a persistent cache for fast restarts. | ||
persist: false, | ||
svgoConfig : { | ||
svgoConfig: { | ||
plugins: [ | ||
@@ -40,3 +41,3 @@ { removeTitle: true } | ||
```js | ||
svgoConfig : { | ||
svgoConfig: { | ||
plugins: [ | ||
@@ -51,2 +52,19 @@ { removeUselessStrokeAndFill: false }, | ||
### svgoModule | ||
Type: reference to a custom `svgo` module | ||
Default: `svgo` module defined in `broccoli-svg-optimizer` dependencies | ||
Sets custom `svgo` module. | ||
Example: | ||
```js | ||
const SVGOptimizer = require('broccoli-svg-optimizer'); | ||
let outputNode = new SVGOptimizer(inputNode, { | ||
svgoModule: require('svgo') | ||
}); | ||
``` | ||
### persist | ||
@@ -53,0 +71,0 @@ |
6220
58
88
5
9
+ Addedlodash@^4.17.10
+ Addedrsvp@4.8.5(transitive)
Updatedrsvp@^4.8.2
Updatedsvgo@0.6.6