postcss-discard-comments 1.1.1 → 1.2.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/CHANGELOG.md CHANGED
@@ -1,3 +1,28 @@
1
+ # 1.2.1
2
+
3
+ * Improved performance by iterating the AST in a single pass.
4
+
5
+ # 1.2.0
6
+
7
+ * Adds support for user-directed removal of comments, with the `remove`
8
+ option (thanks to @dmitrykiselyov).
9
+ * `removeAllButFirst` now operates on each CSS tree, rather than the first one
10
+ passed to the plugin.
11
+ * Fixes to pass the PostCSS plugin guidelines.
12
+
13
+ # 1.1.3
14
+
15
+ * As PostCSS handles the source map content, there is no need to check for
16
+ the existence of a '#' at position 0 of the comment. This patch fixes this
17
+ behaviour.
18
+
19
+ # 1.1.2
20
+
21
+ * Fixes an issue where comment separated values were being incorrectly
22
+ transformed to not have spaces separating them instead, in `decl.value`.
23
+ e.g. `10px/*test*/20px` became `10px20px` in `decl.value` but not
24
+ `decl._value.raw`.
25
+
1
26
  # 1.1.1
2
27
 
3
28
  * Fixes a bug where non-special comments, with an exclamation mark in any part
package/LICENSE-MIT CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015 Ben Briggs
1
+ Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://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
@@ -2,7 +2,9 @@
2
2
 
3
3
  > Discard comments in your CSS files with PostCSS.
4
4
 
5
- Install via [npm](https://npmjs.org/package/postcss-discard-comments):
5
+ ## Install
6
+
7
+ With [npm](https://npmjs.org/package/postcss-discard-comments) do:
6
8
 
7
9
  ```
8
10
  npm install postcss-discard-comments --save
@@ -10,14 +12,20 @@ npm install postcss-discard-comments --save
10
12
 
11
13
  ## Example
12
14
 
13
- ```js
14
- var postcss = require('postcss')
15
- var comments = require('postcss-discard-comments');
15
+ ### Input
16
+
17
+ ```css
18
+ h1/* heading */{
19
+ margin: 0 auto
20
+ }
21
+ ```
16
22
 
17
- var css = 'h1/* heading */{margin:0 auto}';
18
- console.log(postcss(comments()).process(css).css);
23
+ ### Output
19
24
 
20
- // => 'h1{margin:0 auto}'
25
+ ```css
26
+ h1 {
27
+ margin: 0 auto
28
+ }
21
29
  ```
22
30
 
23
31
  This module discards comments from your CSS files; by default, it will remove
@@ -30,6 +38,27 @@ all regular comments (`/* comment */`) and preserve comments marked as important
30
38
 
31
39
  #### options
32
40
 
41
+ ##### remove(function)
42
+
43
+ Type: `function`
44
+ Return: `boolean`
45
+ Variable: `comment` contains a comment without `/**/`
46
+
47
+ For each comment, return true to remove, or false to keep the comment.
48
+
49
+ ```js
50
+ function(comment) {}
51
+ ```
52
+
53
+ ```js
54
+ var css = '/* headings *//*@ h1 */h1{margin:0 auto}/*@ h2 */h2{color:red}';
55
+ console.log(postcss(comments({
56
+ remove: function(comment) { return comment[0] == "@"; }
57
+ })).process(css).css);
58
+ //=> /* headings */h1{margin:0 auto}h2{color:red}
59
+ ```
60
+ **NOTE:** If you use the `remove` function other options will not be available.
61
+
33
62
  ##### removeAll
34
63
 
35
64
  Type: `boolean`
@@ -56,6 +85,11 @@ console.log(postcss(comments({removeAllButFirst: true})).process(css).css);
56
85
  //=> /*! heading */h1{margin:0 auto}h2{color:red}
57
86
  ```
58
87
 
88
+ ## Usage
89
+
90
+ See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for
91
+ examples for your environment.
92
+
59
93
  ## Contributing
60
94
 
61
95
  Pull requests are welcome. If you add functionality, then please add unit tests
package/index.js CHANGED
@@ -6,72 +6,70 @@ var postcss = require('postcss');
6
6
  var space = postcss.list.space;
7
7
 
8
8
  module.exports = postcss.plugin('postcss-discard-comments', function (options) {
9
- var remover = new CommentRemover(options || {});
9
+ return function (css) {
10
+ var remover = new CommentRemover(options || {});
10
11
 
11
- function replaceComments (source) {
12
- if (!source) {
13
- return;
14
- }
15
- var b = balanced.replacements({
16
- source: source,
17
- open: '/*',
18
- close: '*/',
19
- replace: function (comment, head, tail) {
20
- if (remover.canRemove(comment)) {
21
- return ' ';
22
- }
23
- return head + comment + tail;
12
+ function replaceComments (source) {
13
+ if (!source) {
14
+ return;
24
15
  }
25
- });
26
- return space(b).join(' ');
27
- }
16
+ var b = balanced.replacements({
17
+ source: source,
18
+ open: '/*',
19
+ close: '*/',
20
+ replace: function (comment, head, tail) {
21
+ if (remover.canRemove(comment)) {
22
+ return ' ';
23
+ }
24
+ return head + comment + tail;
25
+ }
26
+ });
27
+ return space(b).join(' ');
28
+ }
28
29
 
29
- return function (css) {
30
- css.eachComment(function (comment) {
31
- if (remover.canRemove(comment.text)) {
32
- comment.removeSelf();
30
+ css.eachInside(function (node) {
31
+ if (node.type === 'comment' && remover.canRemove(node.text)) {
32
+ return node.removeSelf();
33
33
  }
34
- });
35
34
 
36
- css.eachDecl(function (decl) {
37
- decl.between = replaceComments(decl.between);
38
- if (decl._value && decl._value.raw) {
39
- decl._value.raw = replaceComments(decl._value.raw);
40
- }
41
- if (decl._important) {
42
- decl._important = replaceComments(decl._important);
43
- var b = balanced.matches({
44
- source: decl._important,
45
- open: '/*',
46
- close: '*/'
47
- });
48
- decl._important = b.length ? decl._important : '!important';
35
+ if (node.between) {
36
+ node.between = replaceComments(node.between);
49
37
  }
50
- });
51
38
 
52
- css.eachRule(function (rule) {
53
- if (rule.between) {
54
- rule.between = replaceComments(rule.between);
39
+ if (node.type === 'decl') {
40
+ if (node._value && node._value.raw) {
41
+ var replaced = replaceComments(node._value.raw);
42
+ node._value.raw = node._value.value = node.value = replaced;
43
+ }
44
+ if (node._important) {
45
+ node._important = replaceComments(node._important);
46
+ var b = balanced.matches({
47
+ source: node._important,
48
+ open: '/*',
49
+ close: '*/'
50
+ });
51
+ node._important = b.length ? node._important : '!important';
52
+ }
53
+ return;
55
54
  }
56
- if (rule._selector && rule._selector.raw) {
57
- rule._selector.raw = replaceComments(rule._selector.raw);
55
+
56
+ if (node.type === 'rule' && node._selector && node._selector.raw) {
57
+ node._selector.raw = replaceComments(node._selector.raw);
58
+ return;
58
59
  }
59
- });
60
60
 
61
- css.eachAtRule(function (rule) {
62
- if (rule.afterName) {
63
- var commentsReplaced = replaceComments(rule.afterName);
64
- if (!commentsReplaced.length) {
65
- rule.afterName = commentsReplaced + ' ';
66
- } else {
67
- rule.afterName = ' ' + commentsReplaced + ' ';
61
+ if (node.type === 'atrule') {
62
+ if (node.afterName) {
63
+ var commentsReplaced = replaceComments(node.afterName);
64
+ if (!commentsReplaced.length) {
65
+ node.afterName = commentsReplaced + ' ';
66
+ } else {
67
+ node.afterName = ' ' + commentsReplaced + ' ';
68
+ }
69
+ }
70
+ if (node._params && node._params.raw) {
71
+ node._params.raw = replaceComments(node._params.raw);
68
72
  }
69
- }
70
- if (rule._params && rule._params.raw) {
71
- rule._params.raw = replaceComments(rule._params.raw);
72
- }
73
- if (rule.between) {
74
- rule.between = replaceComments(rule.between);
75
73
  }
76
74
  });
77
75
  };
@@ -8,15 +8,20 @@ function CommentRemover (options) {
8
8
  }
9
9
 
10
10
  CommentRemover.prototype.canRemove = function (comment) {
11
- var isImportant = comment.indexOf('!') === 0;
12
- if (!isImportant && comment.indexOf('#') !== 0) {
13
- return true;
14
- } else if (isImportant) {
15
- if (this.options.removeAll || this._hasFirst) {
11
+ var remove = this.options.remove;
12
+ if (remove) {
13
+ return remove(comment);
14
+ } else {
15
+ var isImportant = comment.indexOf('!') === 0;
16
+ if (!isImportant) {
16
17
  return true;
17
- } else if (this.options.removeAllButFirst && !this._hasFirst) {
18
- this._hasFirst = true;
19
- return false;
18
+ } else if (isImportant) {
19
+ if (this.options.removeAll || this._hasFirst) {
20
+ return true;
21
+ } else if (this.options.removeAllButFirst && !this._hasFirst) {
22
+ this._hasFirst = true;
23
+ return false;
24
+ }
20
25
  }
21
26
  }
22
27
  };
package/package.json CHANGED
@@ -1,24 +1,21 @@
1
1
  {
2
2
  "name": "postcss-discard-comments",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "Discard comments in your CSS files with PostCSS.",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "lint": "jshint index.js lib/commentRemover.js --reporter node_modules/jshint-stylish/stylish.js",
8
7
  "test": "tape test.js | tap-spec"
9
8
  },
10
9
  "keywords": [
11
10
  "css",
12
11
  "comments",
13
12
  "postcss",
14
- "postcss-plugins"
13
+ "postcss-plugin"
15
14
  ],
16
15
  "license": "MIT",
17
16
  "devDependencies": {
18
- "jshint": "^2.6.3",
19
- "jshint-stylish": "^1.0.1",
20
- "tap-spec": "^2.2.2",
21
- "tape": "^3.5.0"
17
+ "tap-spec": "^4.0.2",
18
+ "tape": "^4.1.0"
22
19
  },
23
20
  "homepage": "https://github.com/ben-eb/postcss-discard-comments",
24
21
  "author": {
@@ -32,6 +29,6 @@
32
29
  },
33
30
  "dependencies": {
34
31
  "node-balanced": "0.0.14",
35
- "postcss": "^4.1.2"
32
+ "postcss": "^4.1.16"
36
33
  }
37
34
  }