Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit 701fc71

Browse files
devpeerapongjonathantneal
authored andcommitted
feat: update to postcss@8
1 parent 1e373b8 commit 701fc71

File tree

4 files changed

+65
-58
lines changed

4 files changed

+65
-58
lines changed

INSTALL.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ instructions for:
1111
Add [PostCSS Browser Comments] to your project:
1212

1313
```bash
14-
npm install postcss-browser-comments --save-dev
14+
npm install postcss postcss-browser-comments --save-dev
1515
```
1616

1717
Use [PostCSS Browser Comments] to process your CSS:

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ then the entire rule and comment are also removed.
6363
Add [PostCSS Browser Comments] to your project:
6464

6565
```bash
66-
npm install postcss-browser-comments --save-dev
66+
npm install postcss postcss-browser-comments --save-dev
6767
```
6868

6969
Use [PostCSS Browser Comments] to process your CSS:

index.js

+59-51
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,77 @@
11
import browserslist from 'browserslist';
2-
import postcss from 'postcss';
3-
4-
export default postcss.plugin('postcss-browser-comments', opts => root => {
5-
// client browserslist
6-
const clientBrowserList = browserslist(
7-
Object(opts).browsers || null,
8-
{ path: root.source && root.source.input && root.source.input.file }
9-
);
10-
11-
// root children references
12-
const references = root.nodes.slice(0);
13-
14-
// for each child node of the root children references
15-
for (let node of references) {
16-
// if the node is a comment browser comment node
17-
if (isBrowserCommentNode(node)) {
18-
// rule following the browser comment
19-
const rule = node.next();
20-
21-
// browser data
22-
const browserdata = getBrowserData(node.text);
23-
24-
if (browserdata.isNumbered) {
25-
rule.nodes.filter(isBrowserReferenceCommentNode).map(
26-
comment => {
27-
const browserdataIndex = parseFloat(comment.text) - 1;
28-
const browserslistPart = browserdata.browserslist[browserdataIndex];
292

3+
const plugin = opts => {
4+
return {
5+
postcssPlugin: 'postcss-browser-comments',
6+
Once(root) {
7+
// client browserslist
8+
const clientBrowserList = browserslist(
9+
Object(opts).browsers || null,
10+
{ path: root.source && root.source.input && root.source.input.file }
11+
);
12+
13+
// root children references
14+
const references = root.nodes.slice(0);
15+
16+
// for each child node of the root children references
17+
for (let node of references) {
18+
// if the node is a comment browser comment node
19+
if (isBrowserCommentNode(node)) {
20+
// rule following the browser comment
21+
const rule = node.next();
22+
23+
// browser data
24+
const browserdata = getBrowserData(node.text);
25+
26+
if (browserdata.isNumbered) {
27+
rule.nodes.filter(isBrowserReferenceCommentNode).map(
28+
comment => {
29+
const browserdataIndex = parseFloat(comment.text) - 1;
30+
const browserslistPart = browserdata.browserslist[browserdataIndex];
31+
32+
// whether to remove the rule if the comment browserslist does not match the client browserslist
33+
const removeRule = !clientBrowserList.some(
34+
clientBrowser => browserslist(browserslistPart).some(
35+
commentBrowser => commentBrowser === clientBrowser
36+
)
37+
);
38+
39+
// conditionally remove the declaration and reference comment
40+
if (removeRule) {
41+
comment.prev().remove();
42+
comment.remove();
43+
}
44+
}
45+
);
46+
47+
// conditionally remove the empty rule and comment
48+
if (!rule.nodes.length) {
49+
rule.remove();
50+
node.remove();
51+
}
52+
} else {
3053
// whether to remove the rule if the comment browserslist does not match the client browserslist
3154
const removeRule = !clientBrowserList.some(
32-
clientBrowser => browserslist(browserslistPart).some(
55+
clientBrowser => browserslist(browserdata.browserslist).some(
3356
commentBrowser => commentBrowser === clientBrowser
3457
)
3558
);
3659

37-
// conditionally remove the declaration and reference comment
60+
// conditionally remove the rule and comment
3861
if (removeRule) {
39-
comment.prev().remove();
40-
comment.remove();
62+
rule.remove();
63+
node.remove();
4164
}
4265
}
43-
);
44-
45-
// conditionally remove the empty rule and comment
46-
if (!rule.nodes.length) {
47-
rule.remove();
48-
node.remove();
49-
}
50-
} else {
51-
// whether to remove the rule if the comment browserslist does not match the client browserslist
52-
const removeRule = !clientBrowserList.some(
53-
clientBrowser => browserslist(browserdata.browserslist).some(
54-
commentBrowser => commentBrowser === clientBrowser
55-
)
56-
);
57-
58-
// conditionally remove the rule and comment
59-
if (removeRule) {
60-
rule.remove();
61-
node.remove();
6266
}
6367
}
6468
}
6569
}
66-
});
70+
};
71+
72+
plugin.postcss = true;
73+
74+
export default plugin;
6775

6876
// returns whether a node is a browser comment
6977
const isBrowserCommentNode = node => node.type === 'comment' && isBrowserCommentNodeRegExp.test(node.text) && node.next().type === 'rule';

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,18 @@
2626
"engines": {
2727
"node": ">=8.0.0"
2828
},
29-
"dependencies": {
30-
"postcss": "^7"
31-
},
3229
"peerDependencies": {
33-
"browserslist": "^4"
30+
"browserslist": "^4",
31+
"postcss": "^8.2.8"
3432
},
3533
"devDependencies": {
3634
"@babel/core": "^7.4.5",
3735
"@babel/preset-env": "^7.4.5",
3836
"babel-eslint": "^10.0.1",
3937
"browserslist": "^4.1.1",
4038
"eslint": "^5.16.0",
41-
"postcss-tape": "^5.0.0",
39+
"postcss": "^8.2.8",
40+
"postcss-tape": "^6.0.1",
4241
"pre-commit": "^1.2.2",
4342
"rollup": "^1.12.3",
4443
"rollup-plugin-babel": "^4.3.2"

0 commit comments

Comments
 (0)