Add first draft post-processors, fix render function when using callback. remove regex compression (not compatible with sourcemaps)

This commit is contained in:
Luke Page
2014-09-12 14:54:30 +01:00
parent e357dae7bc
commit 4927bf1665
3 changed files with 36 additions and 9 deletions

View File

@@ -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;
};

View File

@@ -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;
};

View File

@@ -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;