CleanCSS.prototype.minify = function(data, callback) {
var options = this.options;
+ var self = this;
if (Buffer.isBuffer(data))
data = data.toString();
if (options.processImport || data.indexOf('@shallow') > 0) {
// inline all imports
- var self = this;
var runner = callback ?
process.nextTick :
- function(callback) { return callback(); };
+ function (callback) { return callback(); };
- return runner(function() {
+ return runner(function () {
return new ImportInliner(self.context, options.inliner).process(data, {
localOnly: !callback,
root: options.root || process.cwd(),
relativeTo: options.relativeTo,
- whenDone: function(data) {
- return minify.call(self, data, callback);
- }
+ whenDone: runMinifier(callback, self)
});
});
} else {
- return minify.call(this, data, callback);
+ return runMinifier(callback, self)(data);
}
};
+function runMinifier(callback, self) {
+ return function (data) {
+ data = self.options.debug ?
+ minifyWithDebug(self, data) :
+ minify.call(self, data);
+
+ return callback ?
+ callback.call(self, self.context.errors.length > 0 ? self.context.errors : null, data) :
+ data;
+ };
+}
+
+function minifyWithDebug(self, data) {
+ var startedAt = process.hrtime();
+ self.stats.originalSize = data.length;
+
+ data = minify.call(self, data);
+
+ var elapsed = process.hrtime(startedAt);
+ self.stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6);
+ self.stats.efficiency = 1 - data.length / self.stats.originalSize;
+ self.stats.minifiedSize = data.length;
+
+ return data;
+}
+
function benchmark(runner) {
return function (processor, action) {
var name = processor.constructor.name + '#' + action;
};
}
-function minify(data, callback) {
- var startedAt;
- var stats = this.stats;
+function minify(data) {
var options = this.options;
var context = this.context;
if (options.benchmark)
run = benchmark(run);
- if (options.debug) {
- startedAt = process.hrtime();
- stats.originalSize = data.length;
- }
-
run(commentsProcessor, 'escape');
run(expressionsProcessor, 'escape');
run(urlsProcessor, 'escape');
run(expressionsProcessor, 'restore');
run(commentsProcessor, 'restore');
- if (options.debug) {
- var elapsed = process.hrtime(startedAt);
- stats.timeSpent = ~~(elapsed[0] * 1e3 + elapsed[1] / 1e6);
- stats.efficiency = 1 - data.length / stats.originalSize;
- stats.minifiedSize = data.length;
- }
-
- return callback ?
- callback.call(this, this.context.errors.length > 0 ? this.context.errors : null, data) :
- data;
-};
+ return data;
+}