e7f45ef47e9ae68443cb314edc8083ac8a6442d7f22ce979276e998655855d6d77ea264284d03370b5cb8fe44b3ff3c32c97849af572a950ec20d13b993435 5.7 KB

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