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.

123 lines
3.6 KiB

4 years ago
  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"];