postcss-discard-comments 8.0.1 → 8.0.3

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/LICENSE-MIT CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)
1
+ Copyright (c) Ben Briggs <beneb.info@gmail.com> (https://beneb.info)
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person
4
4
  obtaining a copy of this software and associated documentation
package/README.md CHANGED
@@ -108,7 +108,7 @@ See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTOR
108
108
 
109
109
  ## License
110
110
 
111
- MIT © [Ben Briggs](http://beneb.info)
111
+ MIT © [Ben Briggs](https://beneb.info)
112
112
 
113
113
 
114
114
  [postcss]: https://github.com/postcss/postcss
package/package.json CHANGED
@@ -1,9 +1,19 @@
1
1
  {
2
2
  "name": "postcss-discard-comments",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
4
4
  "description": "Discard comments in your CSS files with PostCSS.",
5
- "main": "src/index.js",
6
- "types": "types/index.d.ts",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./types/index.d.ts",
8
+ "default": "./src/index.js"
9
+ },
10
+ "./src": "./src/index.js",
11
+ "./src/index": "./src/index.js",
12
+ "./src/index.js": "./src/index.js",
13
+ "./src/lib/*": "./src/lib/*.js",
14
+ "./src/lib/*.js": "./src/lib/*.js",
15
+ "./package.json": "./package.json"
16
+ },
7
17
  "files": [
8
18
  "src",
9
19
  "LICENSE-MIT",
@@ -20,11 +30,14 @@
20
30
  "author": {
21
31
  "name": "Ben Briggs",
22
32
  "email": "beneb.info@gmail.com",
23
- "url": "http://beneb.info"
33
+ "url": "https://beneb.info"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "cssnano/cssnano"
24
38
  },
25
- "repository": "cssnano/cssnano",
26
39
  "dependencies": {
27
- "postcss-selector-parser": "^7.1.2"
40
+ "postcss-selector-parser": "^7.1.5"
28
41
  },
29
42
  "bugs": {
30
43
  "url": "https://github.com/cssnano/cssnano/issues"
@@ -33,11 +46,14 @@
33
46
  "node": "^22.11.0 || ^24.11.0 || >=26.0"
34
47
  },
35
48
  "devDependencies": {
36
- "postcss": "^8.5.15",
49
+ "postcss": "^8.5.26",
37
50
  "postcss-scss": "^4.0.9",
38
51
  "postcss-simple-vars": "^7.0.1"
39
52
  },
40
53
  "peerDependencies": {
41
- "postcss": "^8.5.15"
54
+ "postcss": "^8.5.26"
55
+ },
56
+ "scripts": {
57
+ "test": "node --test"
42
58
  }
43
59
  }
package/src/index.js CHANGED
@@ -9,7 +9,6 @@ const selectorParser = require('postcss-selector-parser');
9
9
  * @property {(s: string) => boolean=} remove
10
10
  */
11
11
  /**
12
- * @type {import('postcss').PluginCreator<Options>}
13
12
  * @param {Options} opts
14
13
  * @return {import('postcss').Plugin}
15
14
  */
@@ -65,7 +64,7 @@ function pluginCreator(opts = {}) {
65
64
  return replacerCache.get(key);
66
65
  }
67
66
 
68
- if (source.indexOf('/*') === -1) {
67
+ if (!source.includes('/*')) {
69
68
  const normalized = space(source).join(' ');
70
69
 
71
70
  replacerCache.set(key, normalized);
@@ -112,7 +111,7 @@ function pluginCreator(opts = {}) {
112
111
  if (replacerCache.has(key)) {
113
112
  return replacerCache.get(key);
114
113
  }
115
- if (source.indexOf('/*') === -1) {
114
+ if (!source.includes('/*')) {
116
115
  const normalized = space(source).join(' ');
117
116
 
118
117
  replacerCache.set(key, normalized);
@@ -147,93 +146,107 @@ function pluginCreator(opts = {}) {
147
146
  return result;
148
147
  }
149
148
 
150
- return {
151
- postcssPlugin: 'postcss-discard-comments',
149
+ /**
150
+ * @param {import('postcss').Declaration} node
151
+ * @param {(s: string) => string[]} space
152
+ */
153
+ function processDeclaration(node, space) {
154
+ if (node.raws.value && node.raws.value.raw) {
155
+ if (node.raws.value.value === node.value) {
156
+ node.value = replaceComments(node.raws.value.raw, space);
157
+ } else {
158
+ node.value = replaceComments(node.value, space);
159
+ }
152
160
 
153
- OnceExit(css, { list }) {
154
- css.walk((node) => {
155
- if (node.type === 'comment' && remover.canRemove(node.text)) {
156
- node.remove();
161
+ /** @type {null | {value: string, raw: string}} */ (node.raws.value) =
162
+ null;
163
+ }
157
164
 
158
- return;
159
- }
165
+ if (node.raws.important) {
166
+ node.raws.important = replaceComments(node.raws.important, space);
160
167
 
161
- if (typeof node.raws.between === 'string') {
162
- node.raws.between = replaceComments(node.raws.between, list.space);
163
- }
168
+ const b = matchesComments(node.raws.important);
164
169
 
165
- if (node.type === 'decl') {
166
- if (node.raws.value && node.raws.value.raw) {
167
- if (node.raws.value.value === node.value) {
168
- node.value = replaceComments(node.raws.value.raw, list.space);
169
- } else {
170
- node.value = replaceComments(node.value, list.space);
171
- }
172
-
173
- /** @type {null | {value: string, raw: string}} */ (
174
- node.raws.value
175
- ) = null;
176
- }
170
+ node.raws.important = b.length ? node.raws.important : '!important';
171
+ } else {
172
+ node.value = replaceComments(node.value, space);
173
+ }
174
+ }
177
175
 
178
- if (node.raws.important) {
179
- node.raws.important = replaceComments(
180
- node.raws.important,
181
- list.space
182
- );
176
+ /**
177
+ * @param {import('postcss').Rule} node
178
+ * @param {(s: string) => string[]} space
179
+ */
180
+ function processRule(node, space) {
181
+ if (node.raws.selector && node.raws.selector.raw) {
182
+ node.raws.selector.raw = replaceCommentsInSelector(
183
+ node.raws.selector.raw,
184
+ space
185
+ );
186
+ } else if (node.selector && node.selector.includes('/*')) {
187
+ node.selector = replaceCommentsInSelector(node.selector, space);
188
+ }
189
+ }
183
190
 
184
- const b = matchesComments(node.raws.important);
191
+ /**
192
+ * @param {import('postcss').AtRule} node
193
+ * @param {(s: string) => string[]} space
194
+ */
195
+ function processAtRule(node, space) {
196
+ if (node.raws.afterName) {
197
+ const commentsReplaced = replaceComments(node.raws.afterName, space);
198
+
199
+ if (!commentsReplaced.length) {
200
+ node.raws.afterName = commentsReplaced + ' ';
201
+ } else {
202
+ node.raws.afterName = ' ' + commentsReplaced + ' ';
203
+ }
204
+ }
185
205
 
186
- node.raws.important = b.length ? node.raws.important : '!important';
187
- } else {
188
- node.value = replaceComments(node.value, list.space);
189
- }
206
+ if (node.raws.params && node.raws.params.raw) {
207
+ node.raws.params.raw = replaceComments(node.raws.params.raw, space);
208
+ } else if (node.params && node.params.includes('/*')) {
209
+ node.params = replaceComments(node.params, space);
210
+ }
211
+ }
190
212
 
191
- return;
192
- }
213
+ /**
214
+ * @param {import('postcss').ChildNode} node
215
+ * @param {(s: string) => string[]} space
216
+ */
217
+ function processNode(node, space) {
218
+ if (node.type === 'comment' && remover.canRemove(node.text)) {
219
+ node.remove();
193
220
 
194
- if (node.type === 'rule') {
195
- if (node.raws.selector && node.raws.selector.raw) {
196
- node.raws.selector.raw = replaceCommentsInSelector(
197
- node.raws.selector.raw,
198
- list.space
199
- );
200
- } else if (node.selector && node.selector.includes('/*')) {
201
- node.selector = replaceCommentsInSelector(
202
- node.selector,
203
- list.space
204
- );
205
- }
221
+ return;
222
+ }
206
223
 
207
- return;
208
- }
224
+ if (typeof node.raws.between === 'string') {
225
+ node.raws.between = replaceComments(node.raws.between, space);
226
+ }
209
227
 
210
- if (node.type === 'atrule') {
211
- if (node.raws.afterName) {
212
- const commentsReplaced = replaceComments(
213
- node.raws.afterName,
214
- list.space
215
- );
216
-
217
- if (!commentsReplaced.length) {
218
- node.raws.afterName = commentsReplaced + ' ';
219
- } else {
220
- node.raws.afterName = ' ' + commentsReplaced + ' ';
221
- }
222
- }
228
+ if (node.type === 'decl') {
229
+ processDeclaration(node, space);
230
+ } else if (node.type === 'rule') {
231
+ processRule(node, space);
232
+ } else if (node.type === 'atrule') {
233
+ processAtRule(node, space);
234
+ }
235
+ }
223
236
 
224
- if (node.raws.params && node.raws.params.raw) {
225
- node.raws.params.raw = replaceComments(
226
- node.raws.params.raw,
227
- list.space
228
- );
229
- } else if (node.params && node.params.includes('/*')) {
230
- node.params = replaceComments(node.params, list.space);
231
- }
232
- }
233
- });
237
+ return {
238
+ postcssPlugin: 'postcss-discard-comments',
239
+ /**
240
+ * @param {import('postcss').Root} css
241
+ * @param {import('postcss').Helpers} helpers
242
+ */
243
+ OnceExit(css, { list }) {
244
+ css.walk((node) => processNode(node, list.space));
234
245
  },
235
246
  };
236
247
  }
237
248
 
238
249
  pluginCreator.postcss = true;
239
- module.exports = pluginCreator;
250
+ module.exports = /** @type {import('postcss').PluginCreator<Options>}*/ (
251
+ pluginCreator
252
+ );
@@ -8,6 +8,85 @@ const STATES = {
8
8
  IN_COMMENT: 3,
9
9
  };
10
10
 
11
+ /**
12
+ * @typedef {object} ParserContext
13
+ * @property {string} input
14
+ * @property {[number, number, number][]} tokens
15
+ * @property {number} length
16
+ * @property {number} pos
17
+ * @property {number} state
18
+ * @property {number} tokenStart
19
+ * @property {number} commentStart
20
+ */
21
+
22
+ /**
23
+ * @param {ParserContext} context
24
+ * @param {string} nextChar
25
+ * @return {boolean}
26
+ */
27
+ function handleNormalState(context, nextChar) {
28
+ const { input, pos } = context;
29
+ const char = input[pos];
30
+
31
+ if (char === '/' && nextChar === '*') {
32
+ if (pos > context.tokenStart) {
33
+ context.tokens.push([0, context.tokenStart, pos]);
34
+ }
35
+ context.commentStart = pos;
36
+ context.state = STATES.IN_COMMENT;
37
+ context.pos += 2;
38
+ return true;
39
+ }
40
+
41
+ if (char === '"') {
42
+ context.state = STATES.IN_DOUBLE_QUOTE;
43
+ } else if (char === "'") {
44
+ context.state = STATES.IN_SINGLE_QUOTE;
45
+ }
46
+
47
+ return false;
48
+ }
49
+
50
+ /**
51
+ * @param {ParserContext} context
52
+ * @param {string} nextChar
53
+ * @param {string} quote
54
+ * @return {boolean}
55
+ */
56
+ function handleQuoteState(context, nextChar, quote) {
57
+ const char = context.input[context.pos];
58
+
59
+ if (char === '\\' && nextChar) {
60
+ context.pos += 2;
61
+ return true;
62
+ }
63
+
64
+ if (char === quote) {
65
+ context.state = STATES.NORMAL;
66
+ }
67
+
68
+ return false;
69
+ }
70
+
71
+ /**
72
+ * @param {ParserContext} context
73
+ * @param {string} nextChar
74
+ * @return {boolean}
75
+ */
76
+ function handleCommentState(context, nextChar) {
77
+ const { input, pos } = context;
78
+
79
+ if (input[pos] === '*' && nextChar === '/') {
80
+ context.tokens.push([1, context.commentStart + 2, pos]);
81
+ context.tokenStart = pos + 2;
82
+ context.state = STATES.NORMAL;
83
+ context.pos += 2;
84
+ return true;
85
+ }
86
+
87
+ return false;
88
+ }
89
+
11
90
  /**
12
91
  * CSS Comment Parser with context awareness
13
92
  * Properly handles comments inside strings, URLs, and escaped characters
@@ -16,80 +95,55 @@ const STATES = {
16
95
  * @return {[number, number, number][]}
17
96
  */
18
97
  module.exports = function commentParser(input) {
19
- /** @type {[number, number, number][]} */
20
- const tokens = [];
21
- const length = input.length;
22
- let pos = 0;
98
+ /** @type {ParserContext} */
99
+ const context = {
100
+ input,
101
+ tokens: [],
102
+ length: input.length,
103
+ pos: 0,
104
+ state: STATES.NORMAL,
105
+ tokenStart: 0,
106
+ commentStart: 0,
107
+ };
23
108
 
24
- let state = STATES.NORMAL;
25
- let tokenStart = 0;
26
- let commentStart = 0;
109
+ while (context.pos < context.length) {
110
+ const nextChar =
111
+ context.pos + 1 < context.length ? context.input[context.pos + 1] : '';
112
+ let handled;
27
113
 
28
- while (pos < length) {
29
- const char = input[pos];
30
- const nextChar = pos + 1 < length ? input[pos + 1] : '';
31
-
32
- switch (state) {
114
+ switch (context.state) {
33
115
  case STATES.NORMAL:
34
- if (char === '/' && nextChar === '*') {
35
- // Found comment start - add non-comment token if needed
36
- if (pos > tokenStart) {
37
- tokens.push([0, tokenStart, pos]);
38
- }
39
- commentStart = pos;
40
- state = STATES.IN_COMMENT;
41
- pos += 2; // Skip /*
42
- continue;
43
- } else if (char === '"') {
44
- state = STATES.IN_DOUBLE_QUOTE;
45
- } else if (char === "'") {
46
- state = STATES.IN_SINGLE_QUOTE;
47
- }
116
+ handled = handleNormalState(context, nextChar);
48
117
  break;
49
118
 
50
119
  case STATES.IN_SINGLE_QUOTE:
51
- if (char === '\\' && nextChar) {
52
- // Skip escaped character
53
- pos += 2;
54
- continue;
55
- } else if (char === "'") {
56
- state = STATES.NORMAL;
57
- }
120
+ handled = handleQuoteState(context, nextChar, "'");
58
121
  break;
59
122
 
60
123
  case STATES.IN_DOUBLE_QUOTE:
61
- if (char === '\\' && nextChar) {
62
- // Skip escaped character
63
- pos += 2;
64
- continue;
65
- } else if (char === '"') {
66
- state = STATES.NORMAL;
67
- }
124
+ handled = handleQuoteState(context, nextChar, '"');
68
125
  break;
69
126
 
70
127
  case STATES.IN_COMMENT:
71
- if (char === '*' && nextChar === '/') {
72
- // Found comment end
73
- tokens.push([1, commentStart + 2, pos]);
74
- tokenStart = pos + 2;
75
- state = STATES.NORMAL;
76
- pos += 2; // Skip */
77
- continue;
78
- }
128
+ handled = handleCommentState(context, nextChar);
79
129
  break;
80
130
  }
81
131
 
82
- pos++;
132
+ if (handled) {
133
+ continue;
134
+ }
135
+
136
+ context.pos++;
83
137
  }
84
138
 
85
139
  // Handle remaining content
86
- if (state === STATES.IN_COMMENT) {
140
+ if (context.state === STATES.IN_COMMENT) {
87
141
  // Unclosed comment - treat as comment to end
88
- tokens.push([1, commentStart + 2, length]);
89
- } else if (tokenStart < length) {
142
+ context.tokens.push([1, context.commentStart + 2, context.length]);
143
+ } else if (context.tokenStart < context.length) {
90
144
  // Add final non-comment token
91
- tokens.push([0, tokenStart, length]);
145
+ context.tokens.push([0, context.tokenStart, context.length]);
92
146
  }
93
147
 
94
- return tokens;
148
+ return context.tokens;
95
149
  };
@@ -1,32 +1,35 @@
1
1
  'use strict';
2
2
 
3
- /** @param {import('../index.js').Options} options */
4
- function CommentRemover(options) {
5
- this.options = options;
6
- }
7
- /**
8
- * @param {string} comment
9
- * @return {boolean | undefined}
10
- */
11
- CommentRemover.prototype.canRemove = function (comment) {
12
- const remove = this.options.remove;
3
+ class CommentRemover {
4
+ /** @param {import('../index.js').Options} options */
5
+ constructor(options) {
6
+ this.options = options;
7
+ }
8
+ /**
9
+ * @param {string} comment
10
+ * @return {boolean | undefined}
11
+ */
12
+ canRemove(comment) {
13
+ const remove = this.options.remove;
13
14
 
14
- if (remove) {
15
- return remove(comment);
16
- } else {
17
- const isImportant = comment.indexOf('!') === 0;
15
+ if (remove) {
16
+ return remove(comment);
17
+ } else {
18
+ const isImportant = comment.indexOf('!') === 0;
18
19
 
19
- if (!isImportant) {
20
- return true;
21
- }
20
+ if (!isImportant) {
21
+ return true;
22
+ }
22
23
 
23
- if (this.options.removeAll || this._hasFirst) {
24
- return true;
25
- } else if (this.options.removeAllButFirst && !this._hasFirst) {
26
- this._hasFirst = true;
27
- return false;
24
+ if (this.options.removeAll || this._hasFirst) {
25
+ return true;
26
+ } else if (this.options.removeAllButFirst && !this._hasFirst) {
27
+ this._hasFirst = true;
28
+ return false;
29
+ }
28
30
  }
31
+ return false;
29
32
  }
30
- };
33
+ }
31
34
 
32
35
  module.exports = CommentRemover;
package/types/index.d.ts CHANGED
@@ -1,20 +1,6 @@
1
- export = pluginCreator;
2
- /** @typedef {object} Options
3
- * @property {boolean=} removeAll
4
- * @property {boolean=} removeAllButFirst
5
- * @property {(s: string) => boolean=} remove
6
- */
7
- /**
8
- * @type {import('postcss').PluginCreator<Options>}
9
- * @param {Options} opts
10
- * @return {import('postcss').Plugin}
11
- */
12
- declare function pluginCreator(opts?: Options): import("postcss").Plugin;
13
- declare namespace pluginCreator {
14
- export { postcss, Options };
15
- }
16
- declare var postcss: true;
17
- type Options = {
1
+ declare const _exports: import("postcss").PluginCreator<Options>;
2
+ export = _exports;
3
+ export type Options = {
18
4
  removeAll?: boolean | undefined;
19
5
  removeAllButFirst?: boolean | undefined;
20
6
  remove?: ((s: string) => boolean) | undefined;
@@ -1 +1 @@
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,CA8NnC;;;;;;gBArOc,OAAO,YAAC;wBACR,OAAO,YAAC;aACR,CAAA,CAAC,CAAC,EAAE,MAAM,KAAK,OAAO,aAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;AAKI,YAAkB,OAAO,GACzB;IAAqB,SAAS,AAA9B,CACA,EADW,OAAO,YAAC,CACnB;IAAqB,iBAAiB,AAAtC,CACA,EADW,OAAO,YAAC,CACnB;IAAoC,MAAM,AAA1C,CACH,EADc,CAAA,CAAC,CAAC,EAAE,MAAM,KAAK,OAAO,aAAC,CACrC;CAAA,CAAA"}
@@ -1,3 +1,19 @@
1
- declare function _exports(input: string): [number, number, number][];
2
- export = _exports;
1
+ export = commentParser;
2
+ /**
3
+ * CSS Comment Parser with context awareness
4
+ * Properly handles comments inside strings, URLs, and escaped characters
5
+ *
6
+ * @param {string} input
7
+ * @return {[number, number, number][]}
8
+ */
9
+ declare function commentParser(input: string): [number, number, number][];
10
+ export type ParserContext = {
11
+ input: string;
12
+ tokens: [number, number, number][];
13
+ length: number;
14
+ pos: number;
15
+ state: number;
16
+ tokenStart: number;
17
+ commentStart: number;
18
+ };
3
19
  //# sourceMappingURL=commentParser.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commentParser.d.ts","sourceRoot":"","sources":["../../src/lib/commentParser.js"],"names":[],"mappings":"AAiBiB,iCAHN,MAAM,GACL,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,CA+ErC"}
1
+ {"version":3,"file":"commentParser.d.ts","sourceRoot":"","sources":["../../src/lib/commentParser.js"],"names":[],"mappings":";AAyFA;;;;;;GAMG;+BACqC,KAAK,EAHlC,MAGkC,GAFjC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE;AAnFnC,YAAkB,aAAa,GAC/B;IAAmB,KAAK,EAAb,MAAM,CACjB;IAAuC,MAAM,EAAlC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,CACrC;IAAmB,MAAM,EAAd,MAAM,CACjB;IAAmB,GAAG,EAAX,MAAM,CACjB;IAAmB,KAAK,EAAb,MAAM,CACjB;IAAmB,UAAU,EAAlB,MAAM,CACjB;IAAmB,YAAY,EAApB,MAAM,CACnB;CAAA,CAAA"}
@@ -1,15 +1,13 @@
1
1
  export = CommentRemover;
2
- /** @param {import('../index.js').Options} options */
3
- declare function CommentRemover(options: import("../index.js").Options): void;
4
2
  declare class CommentRemover {
5
- /** @param {import('../index.js').Options} options */
6
- constructor(options: import("../index.js").Options);
7
3
  options: import("../index.js").Options;
4
+ _hasFirst: boolean | undefined;
5
+ /** @param {import('../index.js').Options} options */
6
+ constructor(options: import('../index.js').Options);
8
7
  /**
9
8
  * @param {string} comment
10
9
  * @return {boolean | undefined}
11
10
  */
12
11
  canRemove(comment: string): boolean | undefined;
13
- _hasFirst: boolean | undefined;
14
12
  }
15
13
  //# sourceMappingURL=commentRemover.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"commentRemover.d.ts","sourceRoot":"","sources":["../../src/lib/commentRemover.js"],"names":[],"mappings":";AAEA,qDAAqD;AACrD,yCADY,OAAO,aAAa,EAAE,OAAO,QAGxC;;IAHD,qDAAqD;IACrD,qBADY,OAAO,aAAa,EAAE,OAAO,EAGxC;IADC,uCAAsB;IAExB;;;OAGG;IACH,mBAHW,MAAM,GACL,OAAO,GAAG,SAAS,CAqB9B;IAJK,+BAAqB"}
1
+ {"version":3,"file":"commentRemover.d.ts","sourceRoot":"","sources":["../../src/lib/commentRemover.js"],"names":[],"mappings":"SAkCiB,cAAc;AAhC/B,cAAM,cAAc;IAGX,OAAO;IAqBH,SAAS;IAvBpB,qDAAqD;IACrD,YAAY,OAAO,EADP,OAAO,aAAa,EAAE,OACf,EAElB;IACD;;;OAGG;IACH,SAAS,CAAC,OAAO,EAHN,MAGM,GAFL,OAAO,GAAG,SAAS,CAsB9B;CACF"}