index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require("postcss");
  6. var _commentRemover = require("./lib/commentRemover");
  7. var _commentRemover2 = _interopRequireDefault(_commentRemover);
  8. var _commentParser = require("./lib/commentParser");
  9. var _commentParser2 = _interopRequireDefault(_commentParser);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. const { space } = _postcss.list;
  12. exports.default = (0, _postcss.plugin)("postcss-discard-comments", (opts = {}) => {
  13. const remover = new _commentRemover2.default(opts);
  14. const matcherCache = {};
  15. const replacerCache = {};
  16. function matchesComments(source) {
  17. if (matcherCache[source]) {
  18. return matcherCache[source];
  19. }
  20. const result = (0, _commentParser2.default)(source).filter(([type]) => type);
  21. matcherCache[source] = result;
  22. return result;
  23. }
  24. function replaceComments(source, separator = " ") {
  25. const key = source + "@|@" + separator;
  26. if (replacerCache[key]) {
  27. return replacerCache[key];
  28. }
  29. const parsed = (0, _commentParser2.default)(source).reduce((value, [type, start, end]) => {
  30. const contents = source.slice(start, end);
  31. if (!type) {
  32. return value + contents;
  33. }
  34. if (remover.canRemove(contents)) {
  35. return value + separator;
  36. }
  37. return `${value}/*${contents}*/`;
  38. }, "");
  39. const result = space(parsed).join(" ");
  40. replacerCache[key] = result;
  41. return result;
  42. }
  43. return css => {
  44. css.walk(node => {
  45. if (node.type === "comment" && remover.canRemove(node.text)) {
  46. node.remove();
  47. return;
  48. }
  49. if (node.raws.between) {
  50. node.raws.between = replaceComments(node.raws.between);
  51. }
  52. if (node.type === "decl") {
  53. if (node.raws.value && node.raws.value.raw) {
  54. if (node.raws.value.value === node.value) {
  55. node.value = replaceComments(node.raws.value.raw);
  56. } else {
  57. node.value = replaceComments(node.value);
  58. }
  59. node.raws.value = null;
  60. }
  61. if (node.raws.important) {
  62. node.raws.important = replaceComments(node.raws.important);
  63. const b = matchesComments(node.raws.important);
  64. node.raws.important = b.length ? node.raws.important : "!important";
  65. }
  66. return;
  67. }
  68. if (node.type === "rule" && node.raws.selector && node.raws.selector.raw) {
  69. node.raws.selector.raw = replaceComments(node.raws.selector.raw, "");
  70. return;
  71. }
  72. if (node.type === "atrule") {
  73. if (node.raws.afterName) {
  74. const commentsReplaced = replaceComments(node.raws.afterName);
  75. if (!commentsReplaced.length) {
  76. node.raws.afterName = commentsReplaced + " ";
  77. } else {
  78. node.raws.afterName = " " + commentsReplaced + " ";
  79. }
  80. }
  81. if (node.raws.params && node.raws.params.raw) {
  82. node.raws.params.raw = replaceComments(node.raws.params.raw);
  83. }
  84. }
  85. });
  86. };
  87. });
  88. module.exports = exports["default"];