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.

55 lines
1.5 KiB

  1. /*
  2. * grunt-contrib-cssmin
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2012 Tim Branyen, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. module.exports = function(grunt) {
  10. var helper = require('grunt-lib-contrib').init(grunt);
  11. grunt.registerMultiTask('cssmin', 'Minify CSS files', function() {
  12. var options = this.options({
  13. report: false
  14. });
  15. this.files.forEach(function(f) {
  16. var max = f.src.filter(function(filepath) {
  17. // Warn on and remove invalid source files (if nonull was set).
  18. if (!grunt.file.exists(filepath)) {
  19. grunt.log.warn('Source file "' + filepath + '" not found.');
  20. return false;
  21. } else {
  22. return true;
  23. }
  24. })
  25. .map(grunt.file.read)
  26. .join(grunt.util.normalizelf(grunt.util.linefeed));
  27. var min = minifyCSS(max, options);
  28. if (min.length < 1) {
  29. grunt.log.warn('Destination not written because minified CSS was empty.');
  30. } else {
  31. if ( options.banner ) {
  32. min = options.banner + grunt.util.linefeed + min;
  33. }
  34. grunt.file.write(f.dest, min);
  35. grunt.log.writeln('File ' + f.dest + ' created.');
  36. if(options.report) {
  37. helper.minMaxInfo(min, max, options.report);
  38. }
  39. }
  40. });
  41. });
  42. var minifyCSS = function(source, options) {
  43. try {
  44. return require('clean-css').process(source, options);
  45. } catch (e) {
  46. grunt.log.error(e);
  47. grunt.fail.warn('css minification failed.');
  48. }
  49. };
  50. };