Skip to content

Commit

Permalink
Allow providing custom plugins
Browse files Browse the repository at this point in the history
Eg.

    new SVGO({
        plugins: [
            myCustomPugin: {
                type: ‘perItem’,
                fn:   function(item) {
                    /* do stuff */
                }
            }
        ]
    });
  • Loading branch information
rlivsey committed Jun 2, 2015
1 parent bd1b8ad commit 1ec50c4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 28 deletions.
84 changes: 56 additions & 28 deletions lib/svgo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,31 @@ function preparePluginsArray(plugins) {
if (typeof item === 'object') {

key = Object.keys(item)[0];
plugin = EXTEND({}, require('../../plugins/' + key));

// name: {}
if (typeof item[key] === 'object') {
plugin.params = EXTEND({}, plugin.params || {}, item[key]);
plugin.active = true;
// custom
if (typeof item[key] === 'object' && item[key].fn && typeof item[key].fn === 'function') {
plugin = setupCustomPlugin(key, item[key]);

// name: false
} else if (item[key] === false) {
plugin.active = false;
} else {

// name: true
} else if (item[key] === true) {
plugin.active = true;
}
plugin = EXTEND({}, require('../../plugins/' + key));

// name: {}
if (typeof item[key] === 'object') {
plugin.params = EXTEND({}, plugin.params || {}, item[key]);
plugin.active = true;

// name: false
} else if (item[key] === false) {
plugin.active = false;

// name: true
} else if (item[key] === true) {
plugin.active = true;
}

plugin.name = key;
plugin.name = key;
}

// name
} else {
Expand Down Expand Up @@ -124,25 +132,30 @@ function extendConfig(defaults, config) {

key = Object.keys(item)[0];

defaults.plugins.forEach(function(plugin) {
// custom
if (typeof item[key] === 'object' && item[key].fn && typeof item[key].fn === 'function') {
defaults.plugins.push(setupCustomPlugin(key, item[key]));

if (plugin.name === key) {
// name: {}
if (typeof item[key] === 'object') {
plugin.params = EXTEND({}, plugin.params || {}, item[key]);
plugin.active = true;
} else {
defaults.plugins.forEach(function(plugin) {

// name: false
} else if (item[key] === false) {
plugin.active = false;
if (plugin.name === key) {
// name: {}
if (typeof item[key] === 'object') {
plugin.params = EXTEND({}, plugin.params || {}, item[key]);
plugin.active = true;

// name: true
} else if (item[key] === true) {
plugin.active = true;
}
}
// name: false
} else if (item[key] === false) {
plugin.active = false;

});
// name: true
} else if (item[key] === true) {
plugin.active = true;
}
}
});
}

}

Expand All @@ -164,6 +177,21 @@ function extendConfig(defaults, config) {

}

/**
* Setup and enable a custom plugin
*
* @param {String} plugin name
* @param {Object} custom plugin
* @return {Array} enabled plugin
*/
function setupCustomPlugin(name, plugin) {
plugin.active = true;
plugin.params = EXTEND({}, plugin.params || {});
plugin.name = name;

return plugin;
}

/**
* Try to group sequential elements of plugins array.
*
Expand Down
56 changes: 56 additions & 0 deletions test/config/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,62 @@ describe('config', function() {

});


describe('custom plugins', function() {

describe('extend config with custom plugin', function() {
var config = CONFIG({
plugins: [
{
aCustomPlugin: {
type: 'perItem',
fn: function() { }
}
}
]
}),
customPlugin = getPlugin('aCustomPlugin', config.plugins);

it('custom plugin should be enabled', function() {
return customPlugin.active.should.be.true;
});

it('custom plugin should have been given a name', function() {
return customPlugin.name.should.equal('aCustomPlugin');
});
});

describe('replace default config with custom plugin', function() {

var config = CONFIG({
full: true,
plugins: [
{
aCustomPlugin: {
type: 'perItem',
fn: function() { }
}
}
]
}),
customPlugin = getPlugin('aCustomPlugin', config.plugins);

it('config.plugins should have length 1', function() {
return config.plugins.should.have.length(1);
});

it('custom plugin should be enabled', function() {
return customPlugin.active.should.be.true;
});

it('custom plugin should have been given a name', function() {
return customPlugin.name.should.equal('aCustomPlugin');
});

});

});

});

function getPlugin(name, plugins) {
Expand Down

0 comments on commit 1ec50c4

Please sign in to comment.