minify.js 5.6 KB

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