postcss-discard-comments 7.0.0 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-discard-comments",
3
- "version": "7.0.0",
3
+ "version": "7.0.1",
4
4
  "description": "Discard comments in your CSS files with PostCSS.",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -23,6 +23,9 @@
23
23
  "url": "http://beneb.info"
24
24
  },
25
25
  "repository": "cssnano/cssnano",
26
+ "dependencies": {
27
+ "postcss-selector-parser": "^6.1.0"
28
+ },
26
29
  "bugs": {
27
30
  "url": "https://github.com/cssnano/cssnano/issues"
28
31
  },
package/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
  const CommentRemover = require('./lib/commentRemover');
3
3
  const commentParser = require('./lib/commentParser');
4
+ const selectorParser = require('postcss-selector-parser');
4
5
 
5
6
  /** @typedef {object} Options
6
7
  * @property {boolean=} removeAll
@@ -65,6 +66,45 @@ function pluginCreator(opts = {}) {
65
66
  return result;
66
67
  }
67
68
 
69
+ /**
70
+ * @param {string} source
71
+ * @param {(s: string) => string[]} space
72
+ * @return {string}
73
+ */
74
+ function replaceCommentsInSelector(source, space) {
75
+ const key = source + '@|@';
76
+
77
+ if (replacerCache.has(key)) {
78
+ return replacerCache.get(key);
79
+ }
80
+ const processed = selectorParser((ast) => {
81
+ ast.walk((node) => {
82
+ if (node.type === 'comment') {
83
+ const contents = node.value.slice(2, -2);
84
+ if (remover.canRemove(contents)) {
85
+ node.remove();
86
+ }
87
+ }
88
+ const rawSpaceAfter = replaceComments(node.rawSpaceAfter, space, '');
89
+ const rawSpaceBefore = replaceComments(node.rawSpaceBefore, space, '');
90
+ // If comments are not removed, the result of trim will be returned,
91
+ // so if we compare and there are no changes, skip it.
92
+ if (rawSpaceAfter !== node.rawSpaceAfter.trim()) {
93
+ node.rawSpaceAfter = rawSpaceAfter;
94
+ }
95
+ if (rawSpaceBefore !== node.rawSpaceBefore.trim()) {
96
+ node.rawSpaceBefore = rawSpaceBefore;
97
+ }
98
+ });
99
+ }).processSync(source);
100
+
101
+ const result = space(processed).join(' ');
102
+
103
+ replacerCache.set(key, result);
104
+
105
+ return result;
106
+ }
107
+
68
108
  return {
69
109
  postcssPlugin: 'postcss-discard-comments',
70
110
 
@@ -109,16 +149,18 @@ function pluginCreator(opts = {}) {
109
149
  return;
110
150
  }
111
151
 
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
- );
152
+ if (node.type === 'rule') {
153
+ if (node.raws.selector && node.raws.selector.raw) {
154
+ node.raws.selector.raw = replaceCommentsInSelector(
155
+ node.raws.selector.raw,
156
+ list.space
157
+ );
158
+ } else if (node.selector && node.selector.includes('/*')) {
159
+ node.selector = replaceCommentsInSelector(
160
+ node.selector,
161
+ list.space
162
+ );
163
+ }
122
164
 
123
165
  return;
124
166
  }
@@ -142,6 +184,8 @@ function pluginCreator(opts = {}) {
142
184
  node.raws.params.raw,
143
185
  list.space
144
186
  );
187
+ } else if (node.params && node.params.includes('/*')) {
188
+ node.params = replaceComments(node.params, list.space);
145
189
  }
146
190
  }
147
191
  });
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AAIA;;;;GAIG;AACH;;;;GAIG;AACH,sCAHW,OAAO,GACN,OAAO,SAAS,EAAE,MAAM,CAyInC;;;;;gBAhJc,OAAO;wBACP,OAAO;cACP,CAAC,CAAC,EAAE,MAAM,KAAK,OAAO"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AAKA;;;;GAIG;AACH;;;;GAIG;AACH,sCAHW,OAAO,GACN,OAAO,SAAS,EAAE,MAAM,CAoLnC;;;;;gBA3Lc,OAAO;wBACP,OAAO;cACP,CAAC,CAAC,EAAE,MAAM,KAAK,OAAO"}