提交学习笔记专用
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.

153 lines
3.8 KiB

  1. 'use strict';
  2. const CommentRemover = require('./lib/commentRemover');
  3. const commentParser = require('./lib/commentParser');
  4. /** @typedef {object} Options
  5. * @property {boolean=} removeAll
  6. * @property {boolean=} removeAllButFirst
  7. * @property {(s: string) => boolean=} remove
  8. */
  9. /**
  10. * @type {import('postcss').PluginCreator<Options>}
  11. * @param {Options} opts
  12. * @return {import('postcss').Plugin}
  13. */
  14. function pluginCreator(opts = {}) {
  15. const remover = new CommentRemover(opts);
  16. const matcherCache = new Map();
  17. const replacerCache = new Map();
  18. /**
  19. * @param {string} source
  20. * @return {[number, number, number][]}
  21. */
  22. function matchesComments(source) {
  23. if (matcherCache.has(source)) {
  24. return matcherCache.get(source);
  25. }
  26. const result = commentParser(source).filter(([type]) => type);
  27. matcherCache.set(source, result);
  28. return result;
  29. }
  30. /**
  31. * @param {string} source
  32. * @param {(s: string) => string[]} space
  33. * @return {string}
  34. */
  35. function replaceComments(source, space, separator = ' ') {
  36. const key = source + '@|@' + separator;
  37. if (replacerCache.has(key)) {
  38. return replacerCache.get(key);
  39. }
  40. const parsed = commentParser(source).reduce((value, [type, start, end]) => {
  41. const contents = source.slice(start, end);
  42. if (!type) {
  43. return value + contents;
  44. }
  45. if (remover.canRemove(contents)) {
  46. return value + separator;
  47. }
  48. return `${value}/*${contents}*/`;
  49. }, '');
  50. const result = space(parsed).join(' ');
  51. replacerCache.set(key, result);
  52. return result;
  53. }
  54. return {
  55. postcssPlugin: 'postcss-discard-comments',
  56. OnceExit(css, { list }) {
  57. css.walk((node) => {
  58. if (node.type === 'comment' && remover.canRemove(node.text)) {
  59. node.remove();
  60. return;
  61. }
  62. if (typeof node.raws.between === 'string') {
  63. node.raws.between = replaceComments(node.raws.between, list.space);
  64. }
  65. if (node.type === 'decl') {
  66. if (node.raws.value && node.raws.value.raw) {
  67. if (node.raws.value.value === node.value) {
  68. node.value = replaceComments(node.raws.value.raw, list.space);
  69. } else {
  70. node.value = replaceComments(node.value, list.space);
  71. }
  72. /** @type {null | {value: string, raw: string}} */ (
  73. node.raws.value
  74. ) = null;
  75. }
  76. if (node.raws.important) {
  77. node.raws.important = replaceComments(
  78. node.raws.important,
  79. list.space
  80. );
  81. const b = matchesComments(node.raws.important);
  82. node.raws.important = b.length ? node.raws.important : '!important';
  83. } else {
  84. node.value = replaceComments(node.value, list.space);
  85. }
  86. return;
  87. }
  88. if (
  89. node.type === 'rule' &&
  90. node.raws.selector &&
  91. node.raws.selector.raw
  92. ) {
  93. node.raws.selector.raw = replaceComments(
  94. node.raws.selector.raw,
  95. list.space,
  96. ''
  97. );
  98. return;
  99. }
  100. if (node.type === 'atrule') {
  101. if (node.raws.afterName) {
  102. const commentsReplaced = replaceComments(
  103. node.raws.afterName,
  104. list.space
  105. );
  106. if (!commentsReplaced.length) {
  107. node.raws.afterName = commentsReplaced + ' ';
  108. } else {
  109. node.raws.afterName = ' ' + commentsReplaced + ' ';
  110. }
  111. }
  112. if (node.raws.params && node.raws.params.raw) {
  113. node.raws.params.raw = replaceComments(
  114. node.raws.params.raw,
  115. list.space
  116. );
  117. }
  118. }
  119. });
  120. },
  121. };
  122. }
  123. pluginCreator.postcss = true;
  124. module.exports = pluginCreator;