From 3e7750a60d1526607b4530c95998f23794eb36f8 Mon Sep 17 00:00:00 2001 From: Mathijs van Velde Date: Tue, 5 Jul 2016 15:05:30 +0200 Subject: [PATCH] Update cssmin.js Added callback to CleanCSS.minify function --- tasks/cssmin.js | 101 ++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/tasks/cssmin.js b/tasks/cssmin.js index e0c07c3..5ac1e36 100644 --- a/tasks/cssmin.js +++ b/tasks/cssmin.js @@ -18,6 +18,8 @@ module.exports = function (grunt) { }; grunt.registerMultiTask('cssmin', 'Minify CSS', function () { + var done = this.async(); + var created = { maps: 0, files: 0 @@ -27,7 +29,9 @@ module.exports = function (grunt) { after: 0 }; - this.files.forEach(function (file) { + var files = this.files; + + files.forEach(function (file) { var options = this.options({ rebase: false, report: 'min', @@ -41,55 +45,60 @@ module.exports = function (grunt) { options.relativeTo = path.dirname(availableFiles[0]); try { - compiled = new CleanCSS(options).minify(availableFiles); - - if (compiled.errors.length) { - grunt.warn(compiled.errors.toString()); - return; - } - - if (compiled.warnings.length) { - grunt.log.error(compiled.warnings.toString()); - } - - if (options.debug) { - grunt.log.writeln(util.format(compiled.stats)); - } + new CleanCSS(options).minify(availableFiles, function (error, minified) { + compiled = minified; + if (compiled.errors.length) { + grunt.warn(compiled.errors.toString()); + return; + } + + if (compiled.warnings.length) { + grunt.log.error(compiled.warnings.toString()); + } + + if (options.debug) { + grunt.log.writeln(util.format(compiled.stats)); + } + + var compiledCssString = compiled.styles; + + var unCompiledCssString = availableFiles.map(function (file) { + return grunt.file.read(file); + }).join(''); + + size.before += unCompiledCssString.length; + + if (options.sourceMap) { + compiledCssString += '\n' + '/*# sourceMappingURL=' + path.basename(file.dest) + '.map */'; + grunt.file.write(file.dest + '.map', compiled.sourceMap.toString()); + created.maps++; + grunt.verbose.writeln('File ' + chalk.cyan(file.dest + '.map') + ' created'); + } + + grunt.file.write(file.dest, compiledCssString); + created.files++; + size.after += compiledCssString.length; + grunt.verbose.writeln('File ' + chalk.cyan(file.dest) + ' created ' + chalk.dim(maxmin( + unCompiledCssString, compiledCssString, options.report === 'gzip'))); + + if (created.maps > 0) { + grunt.log.ok(created.maps + ' source' + grunt.util.pluralize(files.length, 'map/maps') + + ' created.'); + } + + if (created.files > 0) { + grunt.log.ok(created.files + ' ' + grunt.util.pluralize(files.length, 'file/files') + + ' created. ' + chalk.dim(maxmin(size.before, size.after))); + } else { + grunt.log.warn('No files created.'); + } + + done(); + }); } catch (err) { grunt.log.error(err); grunt.warn('CSS minification failed at ' + availableFiles + '.'); } - - var compiledCssString = compiled.styles; - - var unCompiledCssString = availableFiles.map(function (file) { - return grunt.file.read(file); - }).join(''); - - size.before += unCompiledCssString.length; - - if (options.sourceMap) { - compiledCssString += '\n' + '/*# sourceMappingURL=' + path.basename(file.dest) + '.map */'; - grunt.file.write(file.dest + '.map', compiled.sourceMap.toString()); - created.maps++; - grunt.verbose.writeln('File ' + chalk.cyan(file.dest + '.map') + ' created'); - } - - grunt.file.write(file.dest, compiledCssString); - created.files++; - size.after += compiledCssString.length; - grunt.verbose.writeln('File ' + chalk.cyan(file.dest) + ' created ' + chalk.dim(maxmin(unCompiledCssString, compiledCssString, options.report === 'gzip'))); - }, this); - - if (created.maps > 0) { - grunt.log.ok(created.maps + ' source' + grunt.util.pluralize(this.files.length, 'map/maps') + ' created.'); - } - - if (created.files > 0) { - grunt.log.ok(created.files + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created. ' + chalk.dim(maxmin(size.before, size.after))); - } else { - grunt.log.warn('No files created.'); - } }); };