postcss-discard-comments 5.0.2 → 5.1.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,11 +1,13 @@
1
1
  {
2
2
  "name": "postcss-discard-comments",
3
- "version": "5.0.2",
3
+ "version": "5.1.1",
4
4
  "description": "Discard comments in your CSS files with PostCSS.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
+ "types": "types/index.d.ts",
6
7
  "files": [
7
- "dist",
8
- "LICENSE-MIT"
8
+ "src",
9
+ "LICENSE-MIT",
10
+ "types"
9
11
  ],
10
12
  "keywords": [
11
13
  "css",
@@ -33,9 +35,5 @@
33
35
  "peerDependencies": {
34
36
  "postcss": "^8.2.15"
35
37
  },
36
- "scripts": {
37
- "prebuild": "rimraf dist",
38
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
39
- },
40
38
  "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"
41
39
  }
@@ -1,39 +1,50 @@
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
-
1
+ 'use strict';
2
+ const CommentRemover = require('./lib/commentRemover');
3
+ const 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
+ */
14
15
  function pluginCreator(opts = {}) {
15
- const remover = new _commentRemover.default(opts);
16
+ const remover = new CommentRemover(opts);
16
17
  const matcherCache = new Map();
17
18
  const replacerCache = new Map();
18
19
 
20
+ /**
21
+ * @param {string} source
22
+ * @return {[number, number, number][]}
23
+ */
19
24
  function matchesComments(source) {
20
25
  if (matcherCache.has(source)) {
21
26
  return matcherCache.get(source);
22
27
  }
23
28
 
24
- const result = (0, _commentParser.default)(source).filter(([type]) => type);
29
+ const result = commentParser(source).filter(([type]) => type);
30
+
25
31
  matcherCache.set(source, result);
32
+
26
33
  return result;
27
34
  }
28
35
 
36
+ /**
37
+ * @param {string} source
38
+ * @param {(s: string) => string[]} space
39
+ * @return {string}
40
+ */
29
41
  function replaceComments(source, space, separator = ' ') {
30
42
  const key = source + '@|@' + separator;
31
43
 
32
44
  if (replacerCache.has(key)) {
33
45
  return replacerCache.get(key);
34
46
  }
35
-
36
- const parsed = (0, _commentParser.default)(source).reduce((value, [type, start, end]) => {
47
+ const parsed = commentParser(source).reduce((value, [type, start, end]) => {
37
48
  const contents = source.slice(start, end);
38
49
 
39
50
  if (!type) {
@@ -46,25 +57,30 @@ function pluginCreator(opts = {}) {
46
57
 
47
58
  return `${value}/*${contents}*/`;
48
59
  }, '');
60
+
49
61
  const result = space(parsed).join(' ');
62
+
50
63
  replacerCache.set(key, result);
64
+
51
65
  return result;
52
66
  }
53
67
 
54
68
  return {
55
69
  postcssPlugin: 'postcss-discard-comments',
56
70
 
57
- OnceExit(css, {
58
- list
59
- }) {
60
- css.walk(node => {
71
+ OnceExit(css, { list }) {
72
+ css.walk((node) => {
61
73
  if (node.type === 'comment' && remover.canRemove(node.text)) {
62
74
  node.remove();
75
+
63
76
  return;
64
77
  }
65
78
 
66
79
  if (node.raws.between) {
67
- node.raws.between = replaceComments(node.raws.between, list.space);
80
+ node.raws.between = replaceComments(
81
+ /** @type {string} */ (node.raws.between),
82
+ list.space
83
+ );
68
84
  }
69
85
 
70
86
  if (node.type === 'decl') {
@@ -75,26 +91,47 @@ function pluginCreator(opts = {}) {
75
91
  node.value = replaceComments(node.value, list.space);
76
92
  }
77
93
 
78
- node.raws.value = null;
94
+ /** @type {null | {value: string, raw: string}} */ (
95
+ node.raws.value
96
+ ) = null;
79
97
  }
80
98
 
81
99
  if (node.raws.important) {
82
- node.raws.important = replaceComments(node.raws.important, list.space);
100
+ node.raws.important = replaceComments(
101
+ node.raws.important,
102
+ list.space
103
+ );
104
+
83
105
  const b = matchesComments(node.raws.important);
106
+
84
107
  node.raws.important = b.length ? node.raws.important : '!important';
108
+ } else {
109
+ node.value = replaceComments(node.value, list.space);
85
110
  }
86
111
 
87
112
  return;
88
113
  }
89
114
 
90
- if (node.type === 'rule' && node.raws.selector && node.raws.selector.raw) {
91
- node.raws.selector.raw = replaceComments(node.raws.selector.raw, list.space, '');
115
+ if (
116
+ node.type === 'rule' &&
117
+ node.raws.selector &&
118
+ node.raws.selector.raw
119
+ ) {
120
+ node.raws.selector.raw = replaceComments(
121
+ node.raws.selector.raw,
122
+ list.space,
123
+ ''
124
+ );
125
+
92
126
  return;
93
127
  }
94
128
 
95
129
  if (node.type === 'atrule') {
96
130
  if (node.raws.afterName) {
97
- const commentsReplaced = replaceComments(node.raws.afterName, list.space);
131
+ const commentsReplaced = replaceComments(
132
+ node.raws.afterName,
133
+ list.space
134
+ );
98
135
 
99
136
  if (!commentsReplaced.length) {
100
137
  node.raws.afterName = commentsReplaced + ' ';
@@ -104,16 +141,16 @@ function pluginCreator(opts = {}) {
104
141
  }
105
142
 
106
143
  if (node.raws.params && node.raws.params.raw) {
107
- node.raws.params.raw = replaceComments(node.raws.params.raw, list.space);
144
+ node.raws.params.raw = replaceComments(
145
+ node.raws.params.raw,
146
+ list.space
147
+ );
108
148
  }
109
149
  }
110
150
  });
111
- }
112
-
151
+ },
113
152
  };
114
153
  }
115
154
 
116
155
  pluginCreator.postcss = true;
117
- var _default = pluginCreator;
118
- exports.default = _default;
119
- module.exports = exports.default;
156
+ module.exports = pluginCreator;
@@ -1,11 +1,11 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = commentParser;
7
-
8
- function commentParser(input) {
3
+ /**
4
+ * @param {string} input
5
+ * @return {[number, number, number][]}
6
+ */
7
+ module.exports = function commentParser(input) {
8
+ /** @type [number, number, number][] */
9
9
  const tokens = [];
10
10
  const length = input.length;
11
11
  let pos = 0;
@@ -17,6 +17,7 @@ function commentParser(input) {
17
17
  if (~next) {
18
18
  tokens.push([0, pos, next]);
19
19
  pos = next;
20
+
20
21
  next = input.indexOf('*/', pos + 2);
21
22
  tokens.push([1, pos + 2, next]);
22
23
  pos = next + 2;
@@ -27,6 +28,4 @@ function commentParser(input) {
27
28
  }
28
29
 
29
30
  return tokens;
30
- }
31
-
32
- module.exports = exports.default;
31
+ };
@@ -1,14 +1,13 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
1
+ 'use strict';
7
2
 
3
+ /** @param {import('../index.js').Options} options */
8
4
  function CommentRemover(options) {
9
5
  this.options = options;
10
6
  }
11
-
7
+ /**
8
+ * @param {string} comment
9
+ * @return {boolean | undefined}
10
+ */
12
11
  CommentRemover.prototype.canRemove = function (comment) {
13
12
  const remove = this.options.remove;
14
13
 
@@ -30,6 +29,4 @@ CommentRemover.prototype.canRemove = function (comment) {
30
29
  }
31
30
  };
32
31
 
33
- var _default = CommentRemover;
34
- exports.default = _default;
35
- module.exports = exports.default;
32
+ module.exports = CommentRemover;
@@ -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
+ }