Ingen beskrivning

htmlmin.js 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * grunt-contrib-htmlmin
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 Sindre Sorhus, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. var chalk = require('chalk');
  10. var prettyBytes = require('pretty-bytes');
  11. var minify = require('html-minifier').minify;
  12. module.exports = function (grunt) {
  13. grunt.registerMultiTask('htmlmin', 'Minify HTML', function () {
  14. var options = this.options();
  15. this.files.forEach(function (file) {
  16. var min;
  17. var src = file.src[0];
  18. if (!grunt.file.exists(src || ' ')) {
  19. return grunt.log.warn('Source file "' + chalk.cyan(src) + '" not found.');
  20. }
  21. var max = grunt.file.read(src);
  22. try {
  23. min = minify(max, options);
  24. } catch (err) {
  25. return grunt.warn(file.src + '\n' + err);
  26. }
  27. if (min.length < 1) {
  28. grunt.log.warn('Destination not written because minified HTML was empty.');
  29. } else {
  30. grunt.file.write(file.dest, min);
  31. grunt.log.writeln('Minified ' + chalk.cyan(file.dest) + ' ' + prettyBytes(max.length) + ' → ' + prettyBytes(min.length));
  32. }
  33. });
  34. });
  35. };