minify.js 5.2 KB

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