minify.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _uglifyEs = require('uglify-es');
  6. var _uglifyEs2 = _interopRequireDefault(_uglifyEs);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var buildUglifyOptions = function buildUglifyOptions() {
  9. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  10. ecma = _ref.ecma,
  11. warnings = _ref.warnings,
  12. _ref$parse = _ref.parse,
  13. parse = _ref$parse === undefined ? {} : _ref$parse,
  14. _ref$compress = _ref.compress,
  15. compress = _ref$compress === undefined ? {} : _ref$compress,
  16. mangle = _ref.mangle,
  17. output = _ref.output,
  18. toplevel = _ref.toplevel,
  19. nameCache = _ref.nameCache,
  20. ie8 = _ref.ie8,
  21. keep_classnames = _ref.keep_classnames,
  22. keep_fnames = _ref.keep_fnames,
  23. safari10 = _ref.safari10;
  24. return {
  25. ecma,
  26. warnings,
  27. parse,
  28. compress,
  29. mangle: mangle == null ? true : mangle,
  30. output: Object.assign({
  31. shebang: true,
  32. comments: false,
  33. beautify: false,
  34. semicolons: true
  35. }, output),
  36. // Ignoring sourcemap from options
  37. sourceMap: null,
  38. toplevel,
  39. nameCache,
  40. ie8,
  41. keep_classnames,
  42. keep_fnames,
  43. safari10
  44. };
  45. }; /* eslint-disable
  46. arrow-body-style
  47. */
  48. var buildComments = function buildComments(options, uglifyOptions, extractedComments) {
  49. var condition = {};
  50. var commentsOpts = uglifyOptions.output.comments;
  51. // /^\**!|@preserve|@license|@cc_on/
  52. if (typeof options.extractComments === 'boolean') {
  53. condition.preserve = commentsOpts;
  54. condition.extract = /^\**!|@preserve|@license|@cc_on/;
  55. } else if (typeof options.extractComments === 'string' || options.extractComments instanceof RegExp) {
  56. // extractComments specifies the extract condition and commentsOpts specifies the preserve condition
  57. condition.preserve = commentsOpts;
  58. condition.extract = options.extractComments;
  59. } else if (typeof options.extractComments === 'function') {
  60. condition.preserve = false;
  61. condition.extract = options.extractComments;
  62. } else if (Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')) {
  63. // Extract condition is given in extractComments.condition
  64. condition.preserve = commentsOpts;
  65. condition.extract = options.extractComments.condition;
  66. } else {
  67. // No extract condition is given. Extract comments that match commentsOpts instead of preserving them
  68. condition.preserve = false;
  69. condition.extract = commentsOpts;
  70. }
  71. // Ensure that both conditions are functions
  72. ['preserve', 'extract'].forEach(function (key) {
  73. var regexStr = void 0;
  74. var regex = void 0;
  75. switch (typeof condition[key]) {
  76. case 'boolean':
  77. condition[key] = condition[key] ? function () {
  78. return true;
  79. } : function () {
  80. return false;
  81. };
  82. break;
  83. case 'function':
  84. break;
  85. case 'string':
  86. if (condition[key] === 'all') {
  87. condition[key] = function () {
  88. return true;
  89. };
  90. break;
  91. }
  92. if (condition[key] === 'some') {
  93. condition[key] = function (astNode, comment) {
  94. return comment.type === 'comment2' && /@preserve|@license|@cc_on/i.test(comment.value);
  95. };
  96. break;
  97. }
  98. regexStr = condition[key];
  99. condition[key] = function (astNode, comment) {
  100. return new RegExp(regexStr).test(comment.value);
  101. };
  102. break;
  103. default:
  104. regex = condition[key];
  105. condition[key] = function (astNode, comment) {
  106. return regex.test(comment.value);
  107. };
  108. }
  109. });
  110. // Redefine the comments function to extract and preserve
  111. // comments according to the two conditions
  112. return function (astNode, comment) {
  113. if (condition.extract(astNode, comment)) {
  114. extractedComments.push(comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`);
  115. }
  116. return condition.preserve(astNode, comment);
  117. };
  118. };
  119. var minify = function minify(options) {
  120. var file = options.file,
  121. input = options.input,
  122. inputSourceMap = options.inputSourceMap,
  123. extractComments = options.extractComments;
  124. // Copy uglify options
  125. var uglifyOptions = buildUglifyOptions(options.uglifyOptions);
  126. // Add source map data
  127. if (inputSourceMap) {
  128. uglifyOptions.sourceMap = {
  129. content: inputSourceMap
  130. };
  131. }
  132. var extractedComments = [];
  133. if (extractComments) {
  134. uglifyOptions.output.comments = buildComments(options, uglifyOptions, extractedComments);
  135. }
  136. var _uglify$minify = _uglifyEs2.default.minify({ [file]: input }, uglifyOptions),
  137. error = _uglify$minify.error,
  138. map = _uglify$minify.map,
  139. code = _uglify$minify.code,
  140. warnings = _uglify$minify.warnings;
  141. return { error, map, code, warnings, extractedComments };
  142. };
  143. exports.default = minify;