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.

61 lines
1.7 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. var path = require('path');
  12. grunt.registerMultiTask('cssmin', 'Minify CSS files', function() {
  13. var options = this.options({
  14. report: false
  15. });
  16. this.files.forEach(function(f) {
  17. var valid = f.src.filter(function(filepath) {
  18. // Warn on and remove invalid source files (if nonull was set).
  19. if (!grunt.file.exists(filepath)) {
  20. grunt.log.warn('Source file "' + filepath + '" not found.');
  21. return false;
  22. } else {
  23. return true;
  24. }
  25. });
  26. var max = valid
  27. .map(grunt.file.read)
  28. .join(grunt.util.normalizelf(grunt.util.linefeed));
  29. var min = valid.map(function(f) {
  30. options.relativeTo = path.dirname(f);
  31. return minifyCSS(grunt.file.read(f), options);
  32. })
  33. .join('');
  34. if (min.length < 1) {
  35. grunt.log.warn('Destination not written because minified CSS was empty.');
  36. } else {
  37. if ( options.banner ) {
  38. min = options.banner + grunt.util.linefeed + min;
  39. }
  40. grunt.file.write(f.dest, min);
  41. grunt.log.writeln('File ' + f.dest + ' created.');
  42. if(options.report) {
  43. helper.minMaxInfo(min, max, options.report);
  44. }
  45. }
  46. });
  47. });
  48. var minifyCSS = function(source, options) {
  49. try {
  50. return require('clean-css').process(source, options);
  51. } catch (e) {
  52. grunt.log.error(e);
  53. grunt.fail.warn('css minification failed.');
  54. }
  55. };
  56. };