postcss-discard-comments 5.0.3 → 5.1.2

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,11 +1,13 @@
1
1
  {
2
2
  "name": "postcss-discard-comments",
3
- "version": "5.0.3",
3
+ "version": "5.1.2",
4
4
  "description": "Discard comments in your CSS files with PostCSS.",
5
5
  "main": "src/index.js",
6
+ "types": "types/index.d.ts",
6
7
  "files": [
7
8
  "src",
8
- "LICENSE-MIT"
9
+ "LICENSE-MIT",
10
+ "types"
9
11
  ],
10
12
  "keywords": [
11
13
  "css",
@@ -32,6 +34,5 @@
32
34
  },
33
35
  "peerDependencies": {
34
36
  "postcss": "^8.2.15"
35
- },
36
- "readme": "# [postcss][postcss]-discard-comments\n\n> Discard comments in your CSS files with PostCSS.\n\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-discard-comments) do:\n\n```\nnpm install postcss-discard-comments --save\n```\n\n\n## Example\n\n### Input\n\n```css\nh1/* heading */{\n margin: 0 auto\n}\n```\n\n### Output\n\n```css\nh1 {\n margin: 0 auto\n}\n```\n\nThis module discards comments from your CSS files; by default, it will remove\nall regular comments (`/* comment */`) and preserve comments marked as important\n(`/*! important */`).\n\nNote that this module does not handle source map comments because they are not\navailable to it; PostCSS handles this internally, so if they are removed then\nyou will have to [configure source maps in PostCSS][maps].\n\n[maps]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md\n\n\n## API\n\n### comments([options])\n\n#### options\n\n##### remove(function)\n\nType: `function`\nReturn: `boolean`\nVariable: `comment` contains a comment without `/**/`\n\nFor each comment, return true to remove, or false to keep the comment.\n\n```js\nfunction(comment) {}\n```\n\n```js\nvar css = '/* headings *//*@ h1 */h1{margin:0 auto}/*@ h2 */h2{color:red}';\nconsole.log(postcss(comments({\n remove: function(comment) { return comment[0] == \"@\"; }\n})).process(css).css);\n//=> /* headings */h1{margin:0 auto}h2{color:red}\n```\n**NOTE:** If you use the `remove` function other options will not be available.\n\n##### removeAll\n\nType: `boolean`\nDefault: `false`\n\nRemove all comments marked as important.\n\n```js\nvar css = '/*! heading */h1{margin:0 auto}/*! heading 2 */h2{color:red}';\nconsole.log(postcss(comments({removeAll: true})).process(css).css);\n//=> h1{margin:0 auto}h2{color:red}\n```\n\n##### removeAllButFirst\n\nType: `boolean`\nDefault: `false`\n\nRemove all comments marked as important, but the first one.\n\n```js\nvar css = '/*! heading */h1{margin:0 auto}/*! heading 2 */h2{color:red}';\nconsole.log(postcss(comments({removeAllButFirst: true})).process(css).css);\n//=> /*! heading */h1{margin:0 auto}h2{color:red}\n```\n\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n\n[postcss]: https://github.com/postcss/postcss\n"
37
+ }
37
38
  }
package/src/index.js CHANGED
@@ -2,11 +2,25 @@
2
2
  const CommentRemover = require('./lib/commentRemover');
3
3
  const commentParser = require('./lib/commentParser');
4
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
+ */
5
15
  function pluginCreator(opts = {}) {
6
16
  const remover = new CommentRemover(opts);
7
17
  const matcherCache = new Map();
8
18
  const replacerCache = new Map();
9
19
 
20
+ /**
21
+ * @param {string} source
22
+ * @return {[number, number, number][]}
23
+ */
10
24
  function matchesComments(source) {
11
25
  if (matcherCache.has(source)) {
12
26
  return matcherCache.get(source);
@@ -19,13 +33,17 @@ function pluginCreator(opts = {}) {
19
33
  return result;
20
34
  }
21
35
 
36
+ /**
37
+ * @param {string} source
38
+ * @param {(s: string) => string[]} space
39
+ * @return {string}
40
+ */
22
41
  function replaceComments(source, space, separator = ' ') {
23
42
  const key = source + '@|@' + separator;
24
43
 
25
44
  if (replacerCache.has(key)) {
26
45
  return replacerCache.get(key);
27
46
  }
28
-
29
47
  const parsed = commentParser(source).reduce((value, [type, start, end]) => {
30
48
  const contents = source.slice(start, end);
31
49
 
@@ -58,7 +76,7 @@ function pluginCreator(opts = {}) {
58
76
  return;
59
77
  }
60
78
 
61
- if (node.raws.between) {
79
+ if (typeof node.raws.between === 'string') {
62
80
  node.raws.between = replaceComments(node.raws.between, list.space);
63
81
  }
64
82
 
@@ -70,7 +88,9 @@ function pluginCreator(opts = {}) {
70
88
  node.value = replaceComments(node.value, list.space);
71
89
  }
72
90
 
73
- node.raws.value = null;
91
+ /** @type {null | {value: string, raw: string}} */ (
92
+ node.raws.value
93
+ ) = null;
74
94
  }
75
95
 
76
96
  if (node.raws.important) {
@@ -82,6 +102,8 @@ function pluginCreator(opts = {}) {
82
102
  const b = matchesComments(node.raws.important);
83
103
 
84
104
  node.raws.important = b.length ? node.raws.important : '!important';
105
+ } else {
106
+ node.value = replaceComments(node.value, list.space);
85
107
  }
86
108
 
87
109
  return;
@@ -1,5 +1,11 @@
1
1
  'use strict';
2
+
3
+ /**
4
+ * @param {string} input
5
+ * @return {[number, number, number][]}
6
+ */
2
7
  module.exports = function commentParser(input) {
8
+ /** @type [number, number, number][] */
3
9
  const tokens = [];
4
10
  const length = input.length;
5
11
  let pos = 0;
@@ -1,8 +1,13 @@
1
1
  'use strict';
2
+
3
+ /** @param {import('../index.js').Options} options */
2
4
  function CommentRemover(options) {
3
5
  this.options = options;
4
6
  }
5
-
7
+ /**
8
+ * @param {string} comment
9
+ * @return {boolean | undefined}
10
+ */
6
11
  CommentRemover.prototype.canRemove = function (comment) {
7
12
  const remove = this.options.remove;
8
13
 
@@ -0,0 +1,21 @@
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
+ type Options = {
17
+ removeAll?: boolean | undefined;
18
+ removeAllButFirst?: boolean | undefined;
19
+ remove?: ((s: string) => boolean) | undefined;
20
+ };
21
+ declare var postcss: true;
@@ -0,0 +1,2 @@
1
+ declare function _exports(input: string): [number, number, number][];
2
+ export = _exports;
@@ -0,0 +1,14 @@
1
+ export = CommentRemover;
2
+ /** @param {import('../index.js').Options} options */
3
+ declare function CommentRemover(options: import('../index.js').Options): void;
4
+ declare class CommentRemover {
5
+ /** @param {import('../index.js').Options} options */
6
+ constructor(options: import('../index.js').Options);
7
+ options: import("../index.js").Options;
8
+ /**
9
+ * @param {string} comment
10
+ * @return {boolean | undefined}
11
+ */
12
+ canRemove(comment: string): boolean | undefined;
13
+ _hasFirst: boolean | undefined;
14
+ }