Bez popisu

cssmin.js 1.6KB

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