cssmin.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. var path = require('path');
  3. var util = require('util');
  4. var CleanCSS = require('clean-css');
  5. var chalk = require('chalk');
  6. var maxmin = require('maxmin');
  7. module.exports = function (grunt) {
  8. var getAvailableFiles = function (filesArray) {
  9. return filesArray.filter(function (filepath) {
  10. if (!grunt.file.exists(filepath)) {
  11. grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found');
  12. return false;
  13. }
  14. return true;
  15. });
  16. };
  17. grunt.registerMultiTask('cssmin', 'Minify CSS', function () {
  18. var created = {
  19. maps: 0,
  20. files: 0
  21. };
  22. var size = {
  23. before: 0,
  24. after: 0
  25. };
  26. this.files.forEach(function (file) {
  27. var options = this.options({
  28. rebase: false,
  29. report: 'min',
  30. sourceMap: false
  31. });
  32. var availableFiles = getAvailableFiles(file.src);
  33. var compiled = '';
  34. options.target = file.dest;
  35. options.relativeTo = path.dirname(availableFiles[0]);
  36. try {
  37. compiled = new CleanCSS(options).minify(availableFiles);
  38. if (compiled.errors.length) {
  39. grunt.warn(compiled.errors.toString());
  40. return;
  41. }
  42. if (compiled.warnings.length) {
  43. grunt.log.error(compiled.warnings.toString());
  44. }
  45. if (options.debug) {
  46. grunt.log.writeln(util.format(compiled.stats));
  47. }
  48. } catch (err) {
  49. grunt.log.error(err);
  50. grunt.warn('CSS minification failed at ' + availableFiles + '.');
  51. }
  52. var compiledCssString = compiled.styles;
  53. var unCompiledCssString = availableFiles.map(function (file) {
  54. return grunt.file.read(file);
  55. }).join('');
  56. size.before += unCompiledCssString.length;
  57. if (options.sourceMap) {
  58. compiledCssString += '\n' + '/*# sourceMappingURL=' + path.basename(file.dest) + '.map */';
  59. grunt.file.write(file.dest + '.map', compiled.sourceMap.toString());
  60. created.maps++;
  61. grunt.verbose.writeln('File ' + chalk.cyan(file.dest + '.map') + ' created');
  62. }
  63. grunt.file.write(file.dest, compiledCssString);
  64. created.files++;
  65. size.after += compiledCssString.length;
  66. grunt.verbose.writeln('File ' + chalk.cyan(file.dest) + ' created ' + chalk.dim(maxmin(unCompiledCssString, compiledCssString, options.report === 'gzip')));
  67. }, this);
  68. if (created.maps > 0) {
  69. grunt.log.ok(created.maps + ' source' + grunt.util.pluralize(this.files.length, 'map/maps') + ' created.');
  70. }
  71. if (created.files > 0) {
  72. grunt.log.ok(created.files + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created. ' + chalk.dim(maxmin(size.before, size.after)));
  73. } else {
  74. grunt.log.warn('No files created.');
  75. }
  76. });
  77. };