source: frontend/node_modules/postcss-discard-comments/src/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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