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.

145 lines
3.9 KiB

  1. /*
  2. * grunt-contrib-uglify
  3. * https://gruntjs.com/
  4. *
  5. * Copyright (c) 2012 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. // External libs.
  10. var UglifyJS = require('uglify-js');
  11. var fs = require('fs');
  12. exports.init = function(grunt) {
  13. var exports = {};
  14. // Minify with UglifyJS.
  15. // From https://github.com/mishoo/UglifyJS2
  16. // API docs at http://lisperator.net/uglifyjs/
  17. exports.minify = function(files, dest, options) {
  18. options = options || {};
  19. grunt.verbose.write('Minifying with UglifyJS...');
  20. var topLevel = null;
  21. var totalCode = '';
  22. var outputOptions = getOutputOptions(options, dest);
  23. var output = UglifyJS.OutputStream(outputOptions);
  24. // Grab and parse all source files
  25. files.forEach(function(file){
  26. var code = grunt.file.read(file);
  27. if (typeof options.sourceMapPrefix !== 'undefined') {
  28. file = file.replace(/^\/+/, "").split(/\/+/).slice(options.sourceMapPrefix).join("/");
  29. }
  30. totalCode += code;
  31. topLevel = UglifyJS.parse(code, {
  32. filename: file,
  33. toplevel: topLevel
  34. });
  35. });
  36. // Wrap code in a common js wrapper.
  37. if (options.wrap) {
  38. topLevel = topLevel.wrap_commonjs(options.wrap, options.exportAll);
  39. }
  40. // Need to call this before we mangle or compress,
  41. // and call after any compression or ast altering
  42. topLevel.figure_out_scope();
  43. if (options.compress !== false) {
  44. if (options.compress.warnings !== true) {
  45. options.compress.warnings = false;
  46. }
  47. var compressor = UglifyJS.Compressor(options.compress);
  48. topLevel = topLevel.transform(compressor);
  49. // Need to figure out scope again after source being altered
  50. topLevel.figure_out_scope();
  51. }
  52. if (options.mangle !== false) {
  53. // compute_char_frequency optimizes names for compression
  54. topLevel.compute_char_frequency(options.mangle);
  55. // Requires previous call to figure_out_scope
  56. // and should always be called after compressor transform
  57. topLevel.mangle_names(options.mangle);
  58. }
  59. // Print the ast to OutputStream
  60. topLevel.print(output);
  61. var min = output.get();
  62. if (options.sourceMappingURL || options.sourceMap) {
  63. min += '\n//@ sourceMappingURL=' + (options.sourceMappingURL || options.sourceMap);
  64. }
  65. var result = {
  66. max: totalCode,
  67. min: min,
  68. sourceMap: outputOptions.source_map
  69. };
  70. grunt.verbose.ok();
  71. return result;
  72. };
  73. var getOutputOptions = function(options, dest) {
  74. var outputOptions = {
  75. beautify: false,
  76. source_map: null
  77. };
  78. if (options.preserveComments) {
  79. if (options.preserveComments === 'all' || options.preserveComments === true) {
  80. // preserve all the comments we can
  81. outputOptions.comments = true;
  82. } else if (options.preserveComments === 'some') {
  83. // preserve comments with directives or that start with a bang (!)
  84. outputOptions.comments = function(node, comment) {
  85. return (/^!|@preserve|@license|@cc_on/i).test(comment.value);
  86. };
  87. } else if (grunt.util._.isFunction(options.preserveComments)) {
  88. // support custom functions passed in
  89. outputOptions.comments = options.preserveComments;
  90. }
  91. }
  92. if (options.beautify) {
  93. if (grunt.util._.isObject(options.beautify)) {
  94. // beautify options sent as an object are merged
  95. // with outputOptions and passed to the OutputStream
  96. grunt.util._.extend(outputOptions, options.beautify);
  97. } else {
  98. outputOptions.beautify = true;
  99. }
  100. }
  101. if (options.sourceMap) {
  102. var sourceMapIn;
  103. if (options.sourceMapIn) {
  104. sourceMapIn = grunt.file.readJSON(options.sourceMapIn);
  105. }
  106. outputOptions.source_map = UglifyJS.SourceMap({
  107. file: dest,
  108. root: options.sourceMapRoot,
  109. orig: sourceMapIn
  110. });
  111. }
  112. return outputOptions;
  113. };
  114. return exports;
  115. };