postcss-discard-comments 2.0.0 → 2.0.4

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,22 @@
1
+ # 2.0.4
2
+
3
+ * Now compiled with Babel 6.
4
+
5
+ # 2.0.3
6
+
7
+ * Fixes an issue where comments that were removed from selectors were replaced
8
+ by a single space.
9
+
10
+ # 2.0.2
11
+
12
+ * Fixes an integration issue where comments inside values transformed by other
13
+ processors had their values reset to their original state before the
14
+ comments were removed.
15
+
16
+ # 2.0.1
17
+
18
+ * Replaces a dependency on node-balanced with internal comments parser.
19
+
1
20
  # 2.0.0
2
21
 
3
22
  * Upgraded to PostCSS 5 (thanks to @avanes).
package/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  > Discard comments in your CSS files with PostCSS.
4
4
 
5
+
5
6
  ## Install
6
7
 
7
8
  With [npm](https://npmjs.org/package/postcss-discard-comments) do:
@@ -10,6 +11,7 @@ With [npm](https://npmjs.org/package/postcss-discard-comments) do:
10
11
  npm install postcss-discard-comments --save
11
12
  ```
12
13
 
14
+
13
15
  ## Example
14
16
 
15
17
  ### Input
@@ -30,7 +32,14 @@ h1 {
30
32
 
31
33
  This module discards comments from your CSS files; by default, it will remove
32
34
  all regular comments (`/* comment */`) and preserve comments marked as important
33
- (`/*! important */`) or that refer to a source mapping URL (`/*# sourcemap */`).
35
+ (`/*! important */`).
36
+
37
+ Note that this module does not handle source map comments because they are not
38
+ available to it; PostCSS handles this internally, so if they are removed then
39
+ you will have to [configure source maps in PostCSS][maps].
40
+
41
+ [maps]: https://github.com/postcss/postcss/blob/master/docs/source-maps.md
42
+
34
43
 
35
44
  ## API
36
45
 
@@ -85,20 +94,24 @@ console.log(postcss(comments({removeAllButFirst: true})).process(css).css);
85
94
  //=> /*! heading */h1{margin:0 auto}h2{color:red}
86
95
  ```
87
96
 
97
+
88
98
  ## Usage
89
99
 
90
100
  See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for
91
101
  examples for your environment.
92
102
 
103
+
93
104
  ## Contributing
94
105
 
95
106
  Pull requests are welcome. If you add functionality, then please add unit tests
96
107
  to cover it.
97
108
 
109
+
98
110
  ## License
99
111
 
100
112
  MIT © Ben Briggs
101
113
 
114
+
102
115
  [ci]: https://travis-ci.org/ben-eb/postcss-discard-comments
103
116
  [deps]: https://gemnasium.com/ben-eb/postcss-discard-comments
104
117
  [npm]: http://badge.fury.io/js/postcss-discard-comments
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
1
+ 'use strict';
2
+
3
+ exports.__esModule = true;
4
+
5
+ var _commentRemover = require('./lib/commentRemover');
6
+
7
+ var _commentRemover2 = _interopRequireDefault(_commentRemover);
8
+
9
+ var _commentParser = require('./lib/commentParser');
10
+
11
+ var _commentParser2 = _interopRequireDefault(_commentParser);
12
+
13
+ var _postcss = require('postcss');
14
+
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+
17
+ var space = _postcss.list.space;
18
+
19
+ exports.default = (0, _postcss.plugin)('postcss-discard-comments', function () {
20
+ var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
21
+
22
+ var remover = new _commentRemover2.default(opts);
23
+
24
+ function matchesComments(source) {
25
+ return (0, _commentParser2.default)(source).filter(function (node) {
26
+ return node.type === 'comment';
27
+ });
28
+ }
29
+
30
+ function replaceComments(source) {
31
+ var separator = arguments.length <= 1 || arguments[1] === undefined ? ' ' : arguments[1];
32
+
33
+ if (!source) {
34
+ return source;
35
+ }
36
+ var parsed = (0, _commentParser2.default)(source).reduce(function (value, node) {
37
+ if (node.type !== 'comment') {
38
+ return value + node.value;
39
+ }
40
+ if (remover.canRemove(node.value)) {
41
+ return value + separator;
42
+ }
43
+ return value + '/*' + node.value + '*/';
44
+ }, '');
45
+
46
+ return space(parsed).join(' ');
47
+ }
48
+
49
+ return function (css) {
50
+ css.walk(function (node) {
51
+ if (node.type === 'comment' && remover.canRemove(node.text)) {
52
+ node.remove();
53
+ return;
54
+ }
55
+
56
+ if (node.raws.between) {
57
+ node.raws.between = replaceComments(node.raws.between);
58
+ }
59
+
60
+ if (node.type === 'decl') {
61
+ if (node.raws.value && node.raws.value.raw) {
62
+ if (node.raws.value.value === node.value) {
63
+ node.value = replaceComments(node.raws.value.raw);
64
+ } else {
65
+ node.value = replaceComments(node.value);
66
+ }
67
+ node.raws.value = null;
68
+ }
69
+ if (node.raws.important) {
70
+ node.raws.important = replaceComments(node.raws.important);
71
+ var b = matchesComments(node.raws.important);
72
+ node.raws.important = b.length ? node.raws.important : '!important';
73
+ }
74
+ return;
75
+ }
76
+
77
+ if (node.type === 'rule' && node.raws.selector && node.raws.selector.raw) {
78
+ node.raws.selector.raw = replaceComments(node.raws.selector.raw, '');
79
+ return;
80
+ }
81
+
82
+ if (node.type === 'atrule') {
83
+ if (node.raws.afterName) {
84
+ var commentsReplaced = replaceComments(node.raws.afterName);
85
+ if (!commentsReplaced.length) {
86
+ node.raws.afterName = commentsReplaced + ' ';
87
+ } else {
88
+ node.raws.afterName = ' ' + commentsReplaced + ' ';
89
+ }
90
+ }
91
+ if (node.raws.params && node.raws.params.raw) {
92
+ node.raws.params.raw = replaceComments(node.raws.params.raw);
93
+ }
94
+ }
95
+ });
96
+ };
97
+ });
98
+ module.exports = exports['default'];
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ exports.__esModule = true;
4
+ exports.default = commentParser;
5
+ function commentParser(input) {
6
+ var tokens = [];
7
+ var length = input.length;
8
+ var pos = 0;
9
+ var next = undefined;
10
+
11
+ while (pos < length) {
12
+ next = input.indexOf('/*', pos);
13
+
14
+ if (~next) {
15
+ tokens.push({
16
+ type: 'other',
17
+ value: input.slice(pos, next)
18
+ });
19
+ pos = next;
20
+
21
+ next = input.indexOf('*/', pos + 2);
22
+ if (! ~next) {
23
+ throw new Error('postcss-discard-comments: Unclosed */');
24
+ }
25
+ tokens.push({
26
+ type: 'comment',
27
+ value: input.slice(pos + 2, next)
28
+ });
29
+ pos = next + 2;
30
+ } else {
31
+ tokens.push({
32
+ type: 'other',
33
+ value: input.slice(pos)
34
+ });
35
+ pos = length;
36
+ }
37
+ }
38
+
39
+ return tokens;
40
+ };
41
+ module.exports = exports['default'];
@@ -1,9 +1,7 @@
1
1
  'use strict';
2
2
 
3
- function CommentRemover (options) {
4
- if (!(this instanceof CommentRemover)) {
5
- return new CommentRemover(options);
6
- }
3
+ exports.__esModule = true;
4
+ function CommentRemover(options) {
7
5
  this.options = options;
8
6
  }
9
7
 
@@ -26,4 +24,5 @@ CommentRemover.prototype.canRemove = function (comment) {
26
24
  }
27
25
  };
28
26
 
29
- module.exports = CommentRemover;
27
+ exports.default = CommentRemover;
28
+ module.exports = exports['default'];
package/package.json CHANGED
@@ -1,10 +1,16 @@
1
1
  {
2
2
  "name": "postcss-discard-comments",
3
- "version": "2.0.0",
3
+ "version": "2.0.4",
4
4
  "description": "Discard comments in your CSS files with PostCSS.",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist",
8
+ "LICENSE-MIT"
9
+ ],
6
10
  "scripts": {
7
- "test": "tape test.js | tap-spec"
11
+ "pretest": "eslint src",
12
+ "prepublish": "del-cli dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/",
13
+ "test": "ava src/__tests__"
8
14
  },
9
15
  "keywords": [
10
16
  "css",
@@ -14,8 +20,18 @@
14
20
  ],
15
21
  "license": "MIT",
16
22
  "devDependencies": {
17
- "tap-spec": "^4.1.0",
18
- "tape": "^4.2.0"
23
+ "ava": "^0.11.0",
24
+ "babel-cli": "^6.5.1",
25
+ "babel-core": "^6.5.1",
26
+ "babel-plugin-add-module-exports": "^0.1.2",
27
+ "babel-preset-es2015": "^6.5.0",
28
+ "babel-preset-es2015-loose": "^7.0.0",
29
+ "babel-preset-stage-0": "^6.5.0",
30
+ "del-cli": "^0.2.0",
31
+ "eslint": "^1.10.3",
32
+ "eslint-config-cssnano": "^1.0.0",
33
+ "postcss-scss": "^0.1.3",
34
+ "postcss-simple-vars": "^1.2.0"
19
35
  },
20
36
  "homepage": "https://github.com/ben-eb/postcss-discard-comments",
21
37
  "author": {
@@ -23,12 +39,14 @@
23
39
  "email": "beneb.info@gmail.com",
24
40
  "url": "http://beneb.info"
25
41
  },
26
- "repository": {
27
- "type": "git",
28
- "url": "git://github.com/ben-eb/postcss-discard-comments.git"
29
- },
42
+ "repository": "ben-eb/postcss-discard-comments",
30
43
  "dependencies": {
31
- "node-balanced": "0.0.14",
32
- "postcss": "^5.0.4"
44
+ "postcss": "^5.0.14"
45
+ },
46
+ "ava": {
47
+ "require": "babel-core/register"
48
+ },
49
+ "eslintConfig": {
50
+ "extends": "cssnano"
33
51
  }
34
52
  }
package/.npmignore DELETED
@@ -1,5 +0,0 @@
1
- .editorconfig
2
- .gitignore
3
- .jshintrc
4
- .travis.yml
5
- test.js
package/index.js DELETED
@@ -1,76 +0,0 @@
1
- 'use strict';
2
-
3
- var balanced = require('node-balanced');
4
- var CommentRemover = require('./lib/commentRemover');
5
- var postcss = require('postcss');
6
- var space = postcss.list.space;
7
-
8
- module.exports = postcss.plugin('postcss-discard-comments', function (options) {
9
- return function (css) {
10
- var remover = new CommentRemover(options || {});
11
-
12
- function replaceComments (source) {
13
- if (!source) {
14
- return;
15
- }
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
- }
29
-
30
- css.walk(function (node) {
31
- if (node.type === 'comment' && remover.canRemove(node.text)) {
32
- return node.remove();
33
- }
34
-
35
- if (node.raws.between) {
36
- node.raws.between = replaceComments(node.raws.between);
37
- }
38
-
39
- if (node.type === 'decl') {
40
- if (node.raws.value && node.raws.value.raw) {
41
- var replaced = replaceComments(node.raws.value.raw);
42
- node.raws.value.raw = node.raws.value.value = node.value = replaced;
43
- }
44
- if (node.raws.important) {
45
- node.raws.important = replaceComments(node.raws.important);
46
- var b = balanced.matches({
47
- source: node.raws.important,
48
- open: '/*',
49
- close: '*/'
50
- });
51
- node.raws.important = b.length ? node.raws.important : '!important';
52
- }
53
- return;
54
- }
55
-
56
- if (node.type === 'rule' && node.raws.selector && node.raws.selector.raw) {
57
- node.raws.selector.raw = replaceComments(node.raws.selector.raw);
58
- return;
59
- }
60
-
61
- if (node.type === 'atrule') {
62
- if (node.raws.afterName) {
63
- var commentsReplaced = replaceComments(node.raws.afterName);
64
- if (!commentsReplaced.length) {
65
- node.raws.afterName = commentsReplaced + ' ';
66
- } else {
67
- node.raws.afterName = ' ' + commentsReplaced + ' ';
68
- }
69
- }
70
- if (node.raws.params && node.raws.params.raw) {
71
- node.raws.params.raw = replaceComments(node.raws.params.raw);
72
- }
73
- }
74
- });
75
- };
76
- });