uglify.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * grunt-contrib-uglify
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2012 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. module.exports = function(grunt) {
  10. // Internal lib.
  11. var uglify = require('./lib/uglify').init(grunt);
  12. var minlib = require('./lib/min').init(grunt);
  13. grunt.registerMultiTask('uglify', 'Minify files with UglifyJS.', function() {
  14. // Merge task-specific and/or target-specific options with these defaults.
  15. var options = this.options({
  16. banner: '',
  17. compress: {
  18. warnings: false
  19. },
  20. mangle: {},
  21. beautify: false
  22. });
  23. // Process banner.
  24. var banner = grunt.template.process(options.banner);
  25. // Iterate over all src-dest file pairs.
  26. this.files.forEach(function(f) {
  27. var src = f.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 "' + filepath + '" not found.');
  31. return false;
  32. } else {
  33. return true;
  34. }
  35. });
  36. // Minify files, warn and fail on error.
  37. var result;
  38. try {
  39. result = uglify.minify(src, f.dest, options);
  40. } catch (e) {
  41. var err = new Error('Uglification failed.');
  42. err.origError = e;
  43. grunt.fail.warn(err);
  44. }
  45. // Concat banner + minified source.
  46. var output = banner + result.min;
  47. // Write the destination file.
  48. grunt.file.write(f.dest, output);
  49. // Write source map
  50. if (options.sourceMap) {
  51. grunt.file.write(options.sourceMap, result.sourceMap);
  52. grunt.log.writeln('Source Map "' + options.sourceMap + '" created.');
  53. }
  54. // Print a success message.
  55. grunt.log.writeln('File "' + f.dest + '" created.');
  56. // ...and report some size information.
  57. minlib.info(result.min, result.max);
  58. });
  59. });
  60. };