You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.7 KiB

  1. /*
  2. * grunt-css
  3. * https://github.com/jzaefferer/grunt-css
  4. *
  5. * Copyright (c) 2012 Jörn Zaefferer
  6. * Licensed under the MIT license.
  7. */
  8. module.exports = function(grunt) {
  9. "use strict";
  10. function min_max(min, max) {
  11. var gzip = require('gzip-js');
  12. var gzipSize = String(gzip.zip(min, {}).length);
  13. grunt.log.writeln('Uncompressed size: ' + String(max.length).green + ' bytes.');
  14. grunt.log.writeln('Compressed size: ' + gzipSize.green + ' bytes gzipped (' + String(min.length).green + ' bytes minified).');
  15. }
  16. function expandFiles( files ) {
  17. return grunt.util._.pluck( grunt.file.expandMapping( files ), "src" );
  18. }
  19. grunt.registerMultiTask( "csslint", "Lint CSS files with csslint", function() {
  20. var csslint = require( "csslint" ).CSSLint;
  21. var files = expandFiles( this.filesSrc );
  22. var ruleset = {};
  23. var verbose = grunt.verbose;
  24. csslint.getRules().forEach(function( rule ) {
  25. ruleset[ rule.id ] = 1;
  26. });
  27. for ( var rule in this.data.rules ) {
  28. if ( !this.data.rules[ rule ] ) {
  29. delete ruleset[rule];
  30. } else {
  31. ruleset[ rule ] = this.data.rules[ rule ];
  32. }
  33. }
  34. var hadErrors = 0;
  35. files.forEach(function( filepath ) {
  36. var file = grunt.file.read( filepath ),
  37. message = "Linting " + filepath + "...",
  38. result;
  39. // skip empty files
  40. if (file.length) {
  41. result = csslint.verify( file, ruleset );
  42. verbose.write( message );
  43. if (result.messages.length) {
  44. verbose.or.write( message );
  45. grunt.log.error();
  46. } else {
  47. verbose.ok();
  48. }
  49. result.messages.forEach(function( message ) {
  50. grunt.log.writeln( "[".red + (typeof message.line !== "undefined" ? ( "L" + message.line ).yellow + ":".red + ( "C" + message.col ).yellow : "GENERAL".yellow) + "]".red );
  51. grunt.log[ message.type === "error" ? "error" : "writeln" ]( message.message + " " + message.rule.desc + " (" + message.rule.id + ")" );
  52. });
  53. if ( result.messages.length ) {
  54. hadErrors += 1;
  55. }
  56. } else {
  57. grunt.log.writeln( "Skipping empty file " + filepath);
  58. }
  59. });
  60. if ( hadErrors ) {
  61. return false;
  62. }
  63. grunt.log.ok( files.length + " files lint free." );
  64. });
  65. grunt.registerMultiTask( "cssmin", "Minify CSS files with clean-css.", function() {
  66. var options = this.options({
  67. banner: ''
  68. });
  69. var src = grunt.file.read( this.filesSrc );
  70. var min = require( "clean-css" ).process( src, options );
  71. if ( options.banner ) {
  72. min = options.banner + grunt.util.linefeed + min;
  73. }
  74. grunt.file.write( this.files[0].dest, min );
  75. grunt.log.writeln( "File '" + this.files[0].dest + "' written." );
  76. min_max( min, src );
  77. });
  78. };