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