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.

110 lines
3.0 KiB

  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 contrib = require('grunt-lib-contrib').init(grunt);
  12. var uglify = require('./lib/uglify').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. report: false
  23. });
  24. // Process banner.
  25. var banner = grunt.template.process(options.banner);
  26. var mapNameGenerator, mappingURLGenerator;
  27. // Iterate over all src-dest file pairs.
  28. this.files.forEach(function(f) {
  29. var src = f.src.filter(function(filepath) {
  30. // Warn on and remove invalid source files (if nonull was set).
  31. if (!grunt.file.exists(filepath)) {
  32. grunt.log.warn('Source file "' + filepath + '" not found.');
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. });
  38. // function to get the name of the sourceMap
  39. if (typeof options.sourceMap === "function") {
  40. mapNameGenerator = options.sourceMap;
  41. }
  42. // function to get the sourceMappingURL
  43. if (typeof options.sourceMappingURL === "function") {
  44. mappingURLGenerator = options.sourceMappingURL;
  45. }
  46. if (mapNameGenerator) {
  47. try {
  48. options.sourceMap = mapNameGenerator(f.dest);
  49. } catch (e) {
  50. var err = new Error('SourceMapName failed.');
  51. err.origError = e;
  52. grunt.fail.warn(err);
  53. }
  54. }
  55. if (mappingURLGenerator) {
  56. try {
  57. options.sourceMappingURL = mappingURLGenerator(f.dest);
  58. } catch (e) {
  59. var err = new Error('SourceMapName failed.');
  60. err.origError = e;
  61. grunt.fail.warn(err);
  62. }
  63. }
  64. // Minify files, warn and fail on error.
  65. var result;
  66. try {
  67. result = uglify.minify(src, f.dest, options);
  68. } catch (e) {
  69. var err = new Error('Uglification failed.');
  70. if (e.msg) {
  71. err.message += ', ' + e.msg + '.';
  72. }
  73. err.origError = e;
  74. grunt.fail.warn(err);
  75. }
  76. // Concat banner + minified source.
  77. var output = banner + result.min;
  78. // Write the destination file.
  79. grunt.file.write(f.dest, output);
  80. // Write source map
  81. if (options.sourceMap) {
  82. grunt.file.write(options.sourceMap, result.sourceMap);
  83. grunt.log.writeln('Source Map "' + options.sourceMap + '" created.');
  84. }
  85. // Print a success message.
  86. grunt.log.writeln('File "' + f.dest + '" created.');
  87. // ...and report some size information.
  88. if (options.report) {
  89. contrib.minMaxInfo(result.min, result.max, options.report);
  90. }
  91. });
  92. });
  93. };