mage-minify.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. module.exports = function (grunt) {
  6. 'use strict';
  7. var compressor = require('node-minify'),
  8. _ = require('underscore');
  9. /**
  10. * Helper function used to create config object for compressor.
  11. *
  12. * @param {Object} options - Options object for a current task.
  13. * @param {Object} file - File object with 'sorce' and 'destination' properties.
  14. * @return {Object} Config object for compressor.
  15. */
  16. function getConfig(options, file) {
  17. return _.extend({
  18. input: file.src,
  19. output: file.dest
  20. }, options);
  21. }
  22. grunt.registerMultiTask('mage-minify', 'Minify files with a various compressor engines', function () {
  23. var done = this.async(),
  24. files = this.files,
  25. total = files.length,
  26. options = this.options();
  27. this.files.forEach(function (file, i) {
  28. var config = getConfig(options, file);
  29. /**
  30. * Callback function.
  31. */
  32. config.callback = function (err) {
  33. if (err) {
  34. console.log(err);
  35. done(false);
  36. } else if (i === total - 1) {
  37. done();
  38. }
  39. };
  40. compressor.minify(config);
  41. });
  42. });
  43. };