From 50a8c8af20b4e58869af6217d0db961caab6aaed Mon Sep 17 00:00:00 2001 From: deepsweet Date: Fri, 26 Oct 2012 23:46:09 +0300 Subject: [PATCH] allow to do a multiple optimizations with one init (close #25) --- lib/coa.js | 5 ++-- lib/config.js | 6 ++++- lib/js2svg.js | 7 ++--- lib/svg2js.js | 6 ++++- lib/svgo.js | 60 +++++++++++++++++++++++++++++------------- test/plugins/_index.js | 60 ++++++++++++++++++++++-------------------- 6 files changed, 91 insertions(+), 53 deletions(-) diff --git a/lib/coa.js b/lib/coa.js index 91c39ed85..defd5bbb2 100644 --- a/lib/coa.js +++ b/lib/coa.js @@ -123,7 +123,7 @@ module.exports = require('coa').Cmd() .then(function(svg) { startBytes = Buffer.byteLength(svg, 'utf-8'); - return SVGO(svg, { coa: options }); + return new SVGO({ coa: options }).optimize(svg); }) .then(function(svgmin) { endTime = Date.now(); @@ -166,7 +166,8 @@ module.exports = require('coa').Cmd() } } - }); + }) + .end(); }); diff --git a/lib/config.js b/lib/config.js index cd698dc73..f6fc6c830 100644 --- a/lib/config.js +++ b/lib/config.js @@ -66,7 +66,11 @@ module.exports = function(options) { return readConfig(defaultConfigPath) .then(function(defaultConfig) { - return extend(true, defaultConfig, options); + if (options) { + return extend(true, defaultConfig, options); + } + + return defaultConfig; }); diff --git a/lib/js2svg.js b/lib/js2svg.js index 3fa1c425c..4413b382b 100644 --- a/lib/js2svg.js +++ b/lib/js2svg.js @@ -1,4 +1,5 @@ -var INHERIT = require('inherit'); +var INHERIT = require('inherit'), + extend = require('./tools').extend; /** * Convert SVG-as-JS object to SVG (XML) string. @@ -27,11 +28,11 @@ var Converter = INHERIT(/** @lends Nodes.prototype */{ __constructor: function(config) { /** - * Converter config. + * Shallow copy of converter config. * * @type {Object} */ - this.config = config; + this.config = extend({}, config); /** * Current indent level hack. diff --git a/lib/svg2js.js b/lib/svg2js.js index 9e42a3a1e..f7b11ddec 100644 --- a/lib/svg2js.js +++ b/lib/svg2js.js @@ -1,7 +1,8 @@ var UTIL = require('util'), Q = require('q'), SAX = require('sax'), - JSAPI = require('./jsAPI'); + JSAPI = require('./jsAPI'), + extend = require('./tools').extend; /** * Convert SVG (XML) string to SVG-as-JS object. @@ -14,6 +15,9 @@ var UTIL = require('util'), */ module.exports = function(svgdata, config) { + // shallow copy + config = extend({}, config); + var strict = config.strict; delete config.strict; diff --git a/lib/svgo.js b/lib/svgo.js index fc27ba1e9..a4d34a5a8 100644 --- a/lib/svgo.js +++ b/lib/svgo.js @@ -1,8 +1,3 @@ -var CONFIG = require('./config'), - SVG2JS = require('./svg2js'), - PLUGINS = require('./plugins'), - JS2SVG = require('./js2svg'); - /** * SVGO is a Nodejs-based tool for optimizing SVG vector graphics files. * @@ -10,26 +5,55 @@ var CONFIG = require('./config'), * * @module svgo * - * @param {String} svgdata input data - * @param {Object} [options] options - * @return {String} output data deferred promise - * * @author Kir Belevich (https://github.com/deepsweet) * @copyright © 2012 Kir Belevich * @license MIT https://raw.github.com/deepsweet/svgo/master/LICENSE */ -module.exports = function(svgdata, options) { - return CONFIG(options) - .then(function(config) { +var INHERIT = require('inherit'), + CONFIG = require('./config'), + SVG2JS = require('./svg2js'), + PLUGINS = require('./plugins'), + JS2SVG = require('./js2svg'); + +/** + * @class SVGO. + */ +module.exports = INHERIT(/** @lends SVGO.prototype */{ + + /** + * @param {Object} [config] config to extend + * + * @constructs + * + * @private + */ + __constructor: function(config) { + + this.config = CONFIG(config); + + }, + + /** + * Main optimize function. + * + * @param {String} svgdata input data + * @return {String} output data deferred promise + */ + optimize: function(svgdata) { + + return this.config + .then(function(config) { + + return SVG2JS(svgdata, config.svg2js) + .then(function(jsdata) { - return SVG2JS(svgdata, config.svg2js) - .then(function(jsdata) { + return JS2SVG(PLUGINS(jsdata, config.plugins), config.js2svg); - return JS2SVG(PLUGINS(jsdata, config.plugins), config.js2svg); + }); - }); + }); - }); + } -}; +}); diff --git a/test/plugins/_index.js b/test/plugins/_index.js index 61b28ce70..8f49e363a 100644 --- a/test/plugins/_index.js +++ b/test/plugins/_index.js @@ -1,10 +1,34 @@ -var QFS = require('q-fs'), +var INHERIT = require('inherit'), + QFS = require('q-fs'), FS = require('fs'), PATH = require('path'), - CONFIG = require('../../lib/config'), - SVGO = require('../../lib/svgo'), regFilename = /^(.*)\.(\d+)\.orig\.svg$/; +var MySVGO = INHERIT(require('../../lib/svgo'), { + + enableOnlyOne: function(name) { + + this.config = this.config.then(function(config) { + + for (var type in config.plugins) { + config.plugins[type].forEach(function(plugin) { + plugin.active = plugin.name === name; + }); + } + + return config; + + }); + + } + + }), + mySVGO = new MySVGO({ + js2svg: { + pretty: true + } + }); + describe('plugins tests', function() { FS.readdirSync(__dirname).forEach(function(file) { @@ -37,12 +61,11 @@ describe('plugins tests', function() { }); function getResult(name, index) { - return prepareConfig(name) - .then(function(config) { - return readFile(name + '.' + index + '.orig.svg') - .then(function(input) { - return SVGO(input.toString(), config); - }); + return readFile(name + '.' + index + '.orig.svg') + .then(function(input) { + mySVGO.enableOnlyOne(name); + + return mySVGO.optimize(input.toString()); }) .then(function(min) { return readFile(name + '.' + index +'.should.svg') @@ -52,25 +75,6 @@ function getResult(name, index) { }); } -function prepareConfig(name) { - - return CONFIG() - .then(function(config) { - for (var type in config.plugins) { - config.plugins[type].forEach(function(plugin) { - if (plugin.name !== name) { - plugin.active = false; - } - }); - } - - config.js2svg.pretty = true; - - return config; - }); - -} - function readFile(path) { return QFS.read(PATH.resolve(__dirname, path)); }