postcss-discard-comments 4.0.1 → 4.0.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/dist/index.js +50 -18
- package/dist/lib/commentRemover.js +2 -0
- package/package.json +1 -1
- package/CHANGELOG.md +0 -75
package/dist/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
|
|
7
|
-
var _postcss = require(
|
|
7
|
+
var _postcss = require("postcss");
|
|
8
8
|
|
|
9
|
-
var _commentRemover = require(
|
|
9
|
+
var _commentRemover = require("./lib/commentRemover");
|
|
10
10
|
|
|
11
11
|
var _commentRemover2 = _interopRequireDefault(_commentRemover);
|
|
12
12
|
|
|
13
|
-
var _commentParser = require(
|
|
13
|
+
var _commentParser = require("./lib/commentParser");
|
|
14
14
|
|
|
15
15
|
var _commentParser2 = _interopRequireDefault(_commentParser);
|
|
16
16
|
|
|
@@ -18,32 +18,56 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
18
18
|
|
|
19
19
|
const { space } = _postcss.list;
|
|
20
20
|
|
|
21
|
-
exports.default = (0, _postcss.plugin)(
|
|
21
|
+
exports.default = (0, _postcss.plugin)("postcss-discard-comments", (opts = {}) => {
|
|
22
22
|
const remover = new _commentRemover2.default(opts);
|
|
23
|
+
const matcherCache = {};
|
|
24
|
+
const replacerCache = {};
|
|
23
25
|
|
|
24
26
|
function matchesComments(source) {
|
|
25
|
-
|
|
27
|
+
if (matcherCache[source]) {
|
|
28
|
+
return matcherCache[source];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const result = (0, _commentParser2.default)(source).filter(([type]) => type);
|
|
32
|
+
|
|
33
|
+
matcherCache[source] = result;
|
|
34
|
+
|
|
35
|
+
return result;
|
|
26
36
|
}
|
|
27
37
|
|
|
28
|
-
function replaceComments(source, separator =
|
|
38
|
+
function replaceComments(source, separator = " ") {
|
|
39
|
+
const key = source + "@|@" + separator;
|
|
40
|
+
|
|
41
|
+
if (replacerCache[key]) {
|
|
42
|
+
return replacerCache[key];
|
|
43
|
+
}
|
|
44
|
+
|
|
29
45
|
const parsed = (0, _commentParser2.default)(source).reduce((value, [type, start, end]) => {
|
|
30
46
|
const contents = source.slice(start, end);
|
|
47
|
+
|
|
31
48
|
if (!type) {
|
|
32
49
|
return value + contents;
|
|
33
50
|
}
|
|
51
|
+
|
|
34
52
|
if (remover.canRemove(contents)) {
|
|
35
53
|
return value + separator;
|
|
36
54
|
}
|
|
55
|
+
|
|
37
56
|
return `${value}/*${contents}*/`;
|
|
38
|
-
},
|
|
57
|
+
}, "");
|
|
58
|
+
|
|
59
|
+
const result = space(parsed).join(" ");
|
|
60
|
+
|
|
61
|
+
replacerCache[key] = result;
|
|
39
62
|
|
|
40
|
-
return
|
|
63
|
+
return result;
|
|
41
64
|
}
|
|
42
65
|
|
|
43
66
|
return css => {
|
|
44
67
|
css.walk(node => {
|
|
45
|
-
if (node.type ===
|
|
68
|
+
if (node.type === "comment" && remover.canRemove(node.text)) {
|
|
46
69
|
node.remove();
|
|
70
|
+
|
|
47
71
|
return;
|
|
48
72
|
}
|
|
49
73
|
|
|
@@ -51,37 +75,45 @@ exports.default = (0, _postcss.plugin)('postcss-discard-comments', (opts = {}) =
|
|
|
51
75
|
node.raws.between = replaceComments(node.raws.between);
|
|
52
76
|
}
|
|
53
77
|
|
|
54
|
-
if (node.type ===
|
|
78
|
+
if (node.type === "decl") {
|
|
55
79
|
if (node.raws.value && node.raws.value.raw) {
|
|
56
80
|
if (node.raws.value.value === node.value) {
|
|
57
81
|
node.value = replaceComments(node.raws.value.raw);
|
|
58
82
|
} else {
|
|
59
83
|
node.value = replaceComments(node.value);
|
|
60
84
|
}
|
|
85
|
+
|
|
61
86
|
node.raws.value = null;
|
|
62
87
|
}
|
|
88
|
+
|
|
63
89
|
if (node.raws.important) {
|
|
64
90
|
node.raws.important = replaceComments(node.raws.important);
|
|
91
|
+
|
|
65
92
|
const b = matchesComments(node.raws.important);
|
|
66
|
-
|
|
93
|
+
|
|
94
|
+
node.raws.important = b.length ? node.raws.important : "!important";
|
|
67
95
|
}
|
|
96
|
+
|
|
68
97
|
return;
|
|
69
98
|
}
|
|
70
99
|
|
|
71
|
-
if (node.type ===
|
|
72
|
-
node.raws.selector.raw = replaceComments(node.raws.selector.raw,
|
|
100
|
+
if (node.type === "rule" && node.raws.selector && node.raws.selector.raw) {
|
|
101
|
+
node.raws.selector.raw = replaceComments(node.raws.selector.raw, "");
|
|
102
|
+
|
|
73
103
|
return;
|
|
74
104
|
}
|
|
75
105
|
|
|
76
|
-
if (node.type ===
|
|
106
|
+
if (node.type === "atrule") {
|
|
77
107
|
if (node.raws.afterName) {
|
|
78
108
|
const commentsReplaced = replaceComments(node.raws.afterName);
|
|
109
|
+
|
|
79
110
|
if (!commentsReplaced.length) {
|
|
80
|
-
node.raws.afterName = commentsReplaced +
|
|
111
|
+
node.raws.afterName = commentsReplaced + " ";
|
|
81
112
|
} else {
|
|
82
|
-
node.raws.afterName =
|
|
113
|
+
node.raws.afterName = " " + commentsReplaced + " ";
|
|
83
114
|
}
|
|
84
115
|
}
|
|
116
|
+
|
|
85
117
|
if (node.raws.params && node.raws.params.raw) {
|
|
86
118
|
node.raws.params.raw = replaceComments(node.raws.params.raw);
|
|
87
119
|
}
|
|
@@ -89,4 +121,4 @@ exports.default = (0, _postcss.plugin)('postcss-discard-comments', (opts = {}) =
|
|
|
89
121
|
});
|
|
90
122
|
};
|
|
91
123
|
});
|
|
92
|
-
module.exports = exports[
|
|
124
|
+
module.exports = exports["default"];
|
|
@@ -9,10 +9,12 @@ function CommentRemover(options) {
|
|
|
9
9
|
|
|
10
10
|
CommentRemover.prototype.canRemove = function (comment) {
|
|
11
11
|
const remove = this.options.remove;
|
|
12
|
+
|
|
12
13
|
if (remove) {
|
|
13
14
|
return remove(comment);
|
|
14
15
|
} else {
|
|
15
16
|
const isImportant = comment.indexOf('!') === 0;
|
|
17
|
+
|
|
16
18
|
if (!isImportant) {
|
|
17
19
|
return true;
|
|
18
20
|
}
|
package/package.json
CHANGED
package/CHANGELOG.md
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
# 4.0.0-rc.0
|
|
2
|
-
|
|
3
|
-
* Breaking: Drops support for Node 0.12, we now require at least Node 4.
|
|
4
|
-
* Breaking: Update PostCSS to 6.0.0.
|
|
5
|
-
|
|
6
|
-
# 2.0.4
|
|
7
|
-
|
|
8
|
-
* Now compiled with Babel 6.
|
|
9
|
-
|
|
10
|
-
# 2.0.3
|
|
11
|
-
|
|
12
|
-
* Fixes an issue where comments that were removed from selectors were replaced
|
|
13
|
-
by a single space.
|
|
14
|
-
|
|
15
|
-
# 2.0.2
|
|
16
|
-
|
|
17
|
-
* Fixes an integration issue where comments inside values transformed by other
|
|
18
|
-
processors had their values reset to their original state before the
|
|
19
|
-
comments were removed.
|
|
20
|
-
|
|
21
|
-
# 2.0.1
|
|
22
|
-
|
|
23
|
-
* Replaces a dependency on node-balanced with internal comments parser.
|
|
24
|
-
|
|
25
|
-
# 2.0.0
|
|
26
|
-
|
|
27
|
-
* Upgraded to PostCSS 5 (thanks to @avanes).
|
|
28
|
-
|
|
29
|
-
# 1.2.1
|
|
30
|
-
|
|
31
|
-
* Improved performance by iterating the AST in a single pass.
|
|
32
|
-
|
|
33
|
-
# 1.2.0
|
|
34
|
-
|
|
35
|
-
* Adds support for user-directed removal of comments, with the `remove`
|
|
36
|
-
option (thanks to @dmitrykiselyov).
|
|
37
|
-
* `removeAllButFirst` now operates on each CSS tree, rather than the first one
|
|
38
|
-
passed to the plugin.
|
|
39
|
-
* Fixes to pass the PostCSS plugin guidelines.
|
|
40
|
-
|
|
41
|
-
# 1.1.3
|
|
42
|
-
|
|
43
|
-
* As PostCSS handles the source map content, there is no need to check for
|
|
44
|
-
the existence of a '#' at position 0 of the comment. This patch fixes this
|
|
45
|
-
behaviour.
|
|
46
|
-
|
|
47
|
-
# 1.1.2
|
|
48
|
-
|
|
49
|
-
* Fixes an issue where comment separated values were being incorrectly
|
|
50
|
-
transformed to not have spaces separating them instead, in `decl.value`.
|
|
51
|
-
e.g. `10px/*test*/20px` became `10px20px` in `decl.value` but not
|
|
52
|
-
`decl._value.raw`.
|
|
53
|
-
|
|
54
|
-
# 1.1.1
|
|
55
|
-
|
|
56
|
-
* Fixes a bug where non-special comments, with an exclamation mark in any part
|
|
57
|
-
of the text, were not being removed.
|
|
58
|
-
|
|
59
|
-
# 1.1.0
|
|
60
|
-
|
|
61
|
-
* Now uses the PostCSS `4.1` plugin API.
|
|
62
|
-
* Adds support for transforming comments inside `!important` values.
|
|
63
|
-
|
|
64
|
-
# 1.0.2
|
|
65
|
-
|
|
66
|
-
* Adds a JSHint config, tidy up unnecessary lines of code.
|
|
67
|
-
|
|
68
|
-
# 1.0.1
|
|
69
|
-
|
|
70
|
-
* Fixed a bug which affected initializing the plugin with no options.
|
|
71
|
-
* Stopped the plugin from trying to match comments in empty strings.
|
|
72
|
-
|
|
73
|
-
# 1.0.0
|
|
74
|
-
|
|
75
|
-
* Initial release.
|