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.

185 lines
5.5 KiB

4 years ago
  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(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; }
  8. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  9. 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; }
  10. const buildTerserOptions = ({
  11. ecma,
  12. warnings,
  13. parse = {},
  14. compress = {},
  15. mangle,
  16. module,
  17. output,
  18. toplevel,
  19. nameCache,
  20. ie8,
  21. /* eslint-disable camelcase */
  22. keep_classnames,
  23. keep_fnames,
  24. /* eslint-enable camelcase */
  25. safari10
  26. } = {}) => ({
  27. ecma,
  28. warnings,
  29. parse: _objectSpread({}, parse),
  30. compress: typeof compress === 'boolean' ? compress : _objectSpread({}, compress),
  31. // eslint-disable-next-line no-nested-ternary
  32. mangle: mangle == null ? true : typeof mangle === 'boolean' ? mangle : _objectSpread({}, mangle),
  33. output: _objectSpread({
  34. shebang: true,
  35. comments: false,
  36. beautify: false,
  37. semicolons: true
  38. }, output),
  39. module,
  40. // Ignoring sourceMap from options
  41. sourceMap: null,
  42. toplevel,
  43. nameCache,
  44. ie8,
  45. keep_classnames,
  46. keep_fnames,
  47. safari10
  48. });
  49. const buildComments = (options, terserOptions, extractedComments) => {
  50. const condition = {};
  51. const commentsOpts = terserOptions.output.comments; // Use /^\**!|@preserve|@license|@cc_on/i RegExp
  52. if (typeof options.extractComments === 'boolean') {
  53. condition.preserve = commentsOpts;
  54. condition.extract = /^\**!|@preserve|@license|@cc_on/i;
  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 = commentsOpts;
  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. } // Ensure that both conditions are functions
  71. ['preserve', 'extract'].forEach(key => {
  72. let regexStr;
  73. let regex;
  74. switch (typeof condition[key]) {
  75. case 'boolean':
  76. condition[key] = condition[key] ? () => true : () => false;
  77. break;
  78. case 'function':
  79. break;
  80. case 'string':
  81. if (condition[key] === 'all') {
  82. condition[key] = () => true;
  83. break;
  84. }
  85. if (condition[key] === 'some') {
  86. condition[key] = (astNode, comment) => {
  87. return comment.type === 'comment2' && /^\**!|@preserve|@license|@cc_on/i.test(comment.value);
  88. };
  89. break;
  90. }
  91. regexStr = condition[key];
  92. condition[key] = (astNode, comment) => {
  93. return new RegExp(regexStr).test(comment.value);
  94. };
  95. break;
  96. default:
  97. regex = condition[key];
  98. condition[key] = (astNode, comment) => regex.test(comment.value);
  99. }
  100. }); // Redefine the comments function to extract and preserve
  101. // comments according to the two conditions
  102. return (astNode, comment) => {
  103. if (condition.extract(astNode, comment)) {
  104. const commentText = comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
  105. if (!extractedComments.includes(commentText)) {
  106. extractedComments.push(commentText);
  107. }
  108. }
  109. return condition.preserve(astNode, comment);
  110. };
  111. };
  112. const minify = options => {
  113. const {
  114. file,
  115. input,
  116. inputSourceMap,
  117. extractComments,
  118. minify: minifyFn
  119. } = options;
  120. if (minifyFn) {
  121. return minifyFn({
  122. [file]: input
  123. }, inputSourceMap);
  124. } // Copy terser options
  125. const terserOptions = buildTerserOptions(options.terserOptions); // Let terser generate a SourceMap
  126. if (inputSourceMap) {
  127. terserOptions.sourceMap = true;
  128. }
  129. const extractedComments = [];
  130. if (extractComments) {
  131. terserOptions.output.comments = buildComments(options, terserOptions, extractedComments);
  132. }
  133. const {
  134. error,
  135. map,
  136. code,
  137. warnings
  138. } = (0, _terser.minify)({
  139. [file]: input
  140. }, terserOptions);
  141. return {
  142. error,
  143. map,
  144. code,
  145. warnings,
  146. extractedComments
  147. };
  148. };
  149. var _default = minify;
  150. exports.default = _default;