diff --git a/lib/less/parse-tree.js b/lib/less/parse-tree.js index 1ae303ff..b7531b81 100644 --- a/lib/less/parse-tree.js +++ b/lib/less/parse-tree.js @@ -45,11 +45,15 @@ ParseTree.prototype.toCSS = function(options) { throw new LessError(e, this.imports); } - if (options.compress) { - return css.replace(/(^(\s)+)|((\s)+$)/g, ""); - } else { - return css; + if (options.plugins) { + var postProcessors = options.plugins.getPostProcessors(); + for(var i = 0; i < postProcessors.length; i++) { + // TODO - pass source maps + // TODO - async + css = postProcessors.process(css); + } } + return css; }; return ParseTree; }; diff --git a/lib/less/plugin-manager.js b/lib/less/plugin-manager.js index c4be752f..38bdb965 100644 --- a/lib/less/plugin-manager.js +++ b/lib/less/plugin-manager.js @@ -4,6 +4,7 @@ var PluginManager = function(less) { this.less = less; this.visitors = []; + this.postProcessors = []; }; PluginManager.prototype.addPlugin = function(plugin) { plugin.install(this.less, this); @@ -11,6 +12,27 @@ PluginManager.prototype.addPlugin = function(plugin) { PluginManager.prototype.addVisitor = function(visitor) { this.visitors.push(visitor); }; +/** + * Adds a post processor object + * @param {object} postProcessor + * @param {number} priority - guidelines 1 = before compression, 1000 = compression, 2000 = after compression + */ +PluginManager.prototype.addPostProcessor = function(postProcessor, priority) { + var indexToInsertAt; + for(indexToInsertAt = 0; indexToInsertAt < this.postProcessors.length; indexToInsertAt++) { + if (this.postProcessors[indexToInsertAt].priority >= priority) { + break; + } + } + this.postProcessors.splice(indexToInsertAt, 0, {postProcessor: postProcessor, priority: priority}); +}; +PluginManager.prototype.getPostProcessors = function() { + var postProcessors = []; + for(var i = 0; i < this.postProcessors.length; i++) { + postProcessors.push(this.postProcessors[i].postProcessor); + } + return postProcessors; +}; PluginManager.prototype.getVisitors = function() { return this.visitors; }; diff --git a/lib/less/render.js b/lib/less/render.js index 16912075..dde6eb84 100644 --- a/lib/less/render.js +++ b/lib/less/render.js @@ -3,9 +3,9 @@ var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : P getImportManager = require("./imports"), Parser = require('./parser/parser'); -var render = function(environment) { +module.exports = function(environment) { var ParseTree = require("./parse-tree")(environment); - return function (input, options, callback) { + var render = function (input, options, callback) { options = options || {}; if (typeof(options) === 'function') { @@ -14,7 +14,7 @@ var render = function(environment) { } if (callback) { - render(input.options) + render(input, options) .then(function(css) { callback(null, css); }, @@ -32,12 +32,13 @@ var render = function(environment) { if (e) { return reject(e); } try { var parseTree = new ParseTree(root, imports); - resolve(parseTree.toCSS(options)); + var css = parseTree.toCSS(options); + resolve(css); } catch (err) { reject( err); } }, options); }); } }; + return render; }; -module.exports = render;