Aucune description

uglify.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * grunt-contrib-uglify
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2013 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. var path = require('path');
  10. var chalk = require('chalk');
  11. var maxmin = require('maxmin');
  12. // Return the relative path from file1 => file2
  13. function relativePath(file1, file2) {
  14. var file1Dirname = path.dirname(file1);
  15. var file2Dirname = path.dirname(file2);
  16. if (file1Dirname !== file2Dirname) {
  17. return path.relative(file1Dirname, file2Dirname) + path.sep;
  18. } else {
  19. return "";
  20. }
  21. }
  22. // Converts \r\n to \n
  23. function normalizeLf( string ) {
  24. return string.replace(/\r\n/g, '\n');
  25. }
  26. module.exports = function(grunt) {
  27. // Internal lib.
  28. var uglify = require('./lib/uglify').init(grunt);
  29. grunt.registerMultiTask('uglify', 'Minify files with UglifyJS.', function() {
  30. // Merge task-specific and/or target-specific options with these defaults.
  31. var options = this.options({
  32. banner: '',
  33. footer: '',
  34. compress: {
  35. warnings: false
  36. },
  37. mangle: {},
  38. beautify: false,
  39. report: 'min',
  40. expression: false,
  41. maxLineLen: 32000,
  42. ASCIIOnly: false
  43. });
  44. // Process banner.
  45. var banner = normalizeLf(options.banner);
  46. var footer = normalizeLf(options.footer);
  47. var mapNameGenerator, mapInNameGenerator;
  48. var createdFiles = 0;
  49. var createdMaps = 0;
  50. // Iterate over all src-dest file pairs.
  51. this.files.forEach(function (f) {
  52. var src = f.src.filter(function (filepath) {
  53. // Warn on and remove invalid source files (if nonull was set).
  54. if (!grunt.file.exists(filepath)) {
  55. grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found.');
  56. return false;
  57. } else {
  58. return true;
  59. }
  60. });
  61. if (src.length === 0) {
  62. grunt.log.warn('Destination ' + chalk.cyan(f.dest) + ' not written because src files were empty.');
  63. return;
  64. }
  65. // Warn on incompatible options
  66. if (options.expression && (options.compress || options.mangle)) {
  67. grunt.log.warn('Option ' + chalk.cyan('expression') + ' not compatible with ' + chalk.cyan('compress and mangle'));
  68. options.compress = false;
  69. options.mangle = false;
  70. }
  71. // function to get the name of the sourceMap
  72. if (typeof options.sourceMapName === "function") {
  73. mapNameGenerator = options.sourceMapName;
  74. }
  75. // function to get the name of the sourceMapIn file
  76. if (typeof options.sourceMapIn === "function") {
  77. if (src.length !== 1) {
  78. grunt.fail.warn('Cannot generate `sourceMapIn` for multiple source files.');
  79. }
  80. mapInNameGenerator = options.sourceMapIn;
  81. }
  82. // dynamically create destination sourcemap name
  83. if (mapNameGenerator) {
  84. try {
  85. options.generatedSourceMapName = mapNameGenerator(f.dest);
  86. } catch (e) {
  87. var err = new Error('SourceMap failed.');
  88. err.origError = e;
  89. grunt.fail.warn(err);
  90. }
  91. }
  92. // If no name is passed append .map to the filename
  93. else if (!options.sourceMapName) {
  94. options.generatedSourceMapName = f.dest + '.map';
  95. } else {
  96. options.generatedSourceMapName = options.sourceMapName;
  97. }
  98. // Dynamically create incoming sourcemap names
  99. if (mapInNameGenerator) {
  100. try {
  101. options.sourceMapIn = mapInNameGenerator(src[0]);
  102. } catch (e) {
  103. var err = new Error('SourceMapInName failed.');
  104. err.origError = e;
  105. grunt.fail.warn(err);
  106. }
  107. }
  108. // Calculate the path from the dest file to the sourcemap for the
  109. // sourceMappingURL reference
  110. if (options.sourceMap) {
  111. var destToSourceMapPath = relativePath(f.dest, options.generatedSourceMapName);
  112. var sourceMapBasename = path.basename(options.generatedSourceMapName);
  113. options.destToSourceMap = destToSourceMapPath + sourceMapBasename;
  114. }
  115. // Minify files, warn and fail on error.
  116. var result;
  117. try {
  118. result = uglify.minify(src, f.dest, options);
  119. } catch (e) {
  120. console.log(e);
  121. var err = new Error('Uglification failed.');
  122. if (e.message) {
  123. err.message += '\n' + e.message + '. \n';
  124. if (e.line) {
  125. err.message += 'Line ' + e.line + ' in ' + src + '\n';
  126. }
  127. }
  128. err.origError = e;
  129. grunt.log.warn('Uglifying source ' + chalk.cyan(src) + ' failed.');
  130. grunt.fail.warn(err);
  131. }
  132. // Concat minified source + footer
  133. var output = result.min + footer;
  134. // Only prepend banner if uglify hasn't taken care of it as part of the preamble
  135. if (!options.sourceMap) {
  136. output = banner + output;
  137. }
  138. // Write the destination file.
  139. grunt.file.write(f.dest, output);
  140. // Write source map
  141. if (options.sourceMap) {
  142. grunt.file.write(options.generatedSourceMapName, result.sourceMap);
  143. grunt.verbose.writeln('File ' + chalk.cyan(options.generatedSourceMapName) + ' created (source map).');
  144. createdMaps++;
  145. }
  146. grunt.verbose.writeln('File ' + chalk.cyan(f.dest) + ' created: ' +
  147. maxmin(result.max, output, options.report === 'gzip'));
  148. createdFiles++;
  149. });
  150. if (createdMaps > 0) {
  151. grunt.log.ok(createdMaps + ' source' + grunt.util.pluralize(this.files.length, 'map/maps') + ' created.');
  152. }
  153. if (createdFiles > 0) {
  154. grunt.log.ok(createdFiles + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created.');
  155. } else {
  156. grunt.log.warn('No files created.');
  157. }
  158. });
  159. };