Skip to content

Commit f503815

Browse files
test: more
1 parent 8a0ce26 commit f503815

File tree

4 files changed

+1030
-52
lines changed

4 files changed

+1030
-52
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,40 +1137,50 @@ module.exports = {
11371137
With the help of the `/* webpackIgnore: true */`comment, it is possible to disable sources handling for rules and for individual declarations.
11381138

11391139
```css
1140-
/* webpackIgnore: true*/
1140+
/* webpackIgnore: true */
11411141
@import url(./basic.css);
11421142
@import /* webpackIgnore: true */ url(./imported.css);
11431143

11441144
.class {
1145-
/* Disabled url handling for the all urls in 'background' declaration */
1145+
/* Disabled url handling for the all urls in the 'background' declaration */
11461146
color: red;
1147-
/** webpackIgnore: true */
1147+
/* webpackIgnore: true */
11481148
background: url("./url/img.png"), url("./url/img.png");
11491149
}
11501150

11511151
.class {
1152-
/* Disabled url handling for the first url in 'background' declaration */
1152+
/* Disabled url handling for the first url in the 'background' declaration */
11531153
color: red;
11541154
background:
1155-
/** webpackIgnore: true */ url("./url/img.png"), url("./url/img.png");
1155+
/* webpackIgnore: true */ url("./url/img.png"), url("./url/img.png");
11561156
}
1157+
1158+
.class {
1159+
/* Disabled url handling for the second url in the 'background' declaration */
1160+
color: red;
1161+
background: url("./url/img.png"),
1162+
/* webpackIgnore: true */ url("./url/img.png");
1163+
}
1164+
1165+
/* prettier-ignore */
11571166
.class {
1158-
/* Disabled url handling for the second url in 'background' declaration */
1167+
/* Disabled url handling for the second url in the 'background' declaration */
11591168
color: red;
11601169
background: url("./url/img.png"),
1161-
/** webpackIgnore: true */ url("./url/img.png");
1170+
/* webpackIgnore: true */
1171+
url("./url/img.png");
11621172
}
11631173

11641174
/* prettier-ignore */
11651175
.class {
1166-
/* Disabled url handling for the 3 and 6 urls in 'background-image' declaration */
1176+
/* Disabled url handling for third and sixth urls in the 'background-image' declaration */
11671177
background-image: image-set(
11681178
url(./url/img.png) 2x,
11691179
url(./url/img.png) 3x,
1170-
/*webpackIgnore: true*/ url(./url/img.png) 4x,
1180+
/* webpackIgnore: true */ url(./url/img.png) 4x,
11711181
url(./url/img.png) 5x,
11721182
url(./url/img.png) 6x,
1173-
/*webpackIgnore: true*/
1183+
/* webpackIgnore: true */
11741184
url(./url/img.png) 7x
11751185
);
11761186
}

src/plugins/postcss-url-parser.js

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ function shouldHandleRule(rule, node, result) {
3131
return true;
3232
}
3333

34+
function getWebpackIgnoreCommentValue(index, nodes, inBetween) {
35+
if (index === 0 && typeof inBetween !== "undefined") {
36+
return inBetween;
37+
}
38+
39+
let prevValueNode = nodes[index - 1];
40+
41+
if (!prevValueNode) {
42+
// eslint-disable-next-line consistent-return
43+
return;
44+
}
45+
46+
if (prevValueNode.type === "space") {
47+
if (!nodes[index - 2]) {
48+
// eslint-disable-next-line consistent-return
49+
return;
50+
}
51+
52+
prevValueNode = nodes[index - 2];
53+
}
54+
55+
if (prevValueNode.type !== "comment") {
56+
// eslint-disable-next-line consistent-return
57+
return;
58+
}
59+
60+
const matched = prevValueNode.value.match(webpackIgnoreCommentRegexp);
61+
62+
return matched && matched[2] === "true";
63+
}
64+
3465
function visitor(result, parsedResults, node, key) {
3566
if (!needParseDeclaration.test(node[key])) {
3667
return;
@@ -40,7 +71,7 @@ function visitor(result, parsedResults, node, key) {
4071
typeof node.raws.value === "undefined" ? node[key] : node.raws.value.raw
4172
);
4273

43-
let needIgnore;
74+
let inBetween;
4475

4576
if (typeof node.raws.between !== "undefined") {
4677
const lastCommentIndex = node.raws.between.lastIndexOf("/*");
@@ -50,7 +81,7 @@ function visitor(result, parsedResults, node, key) {
5081
.match(webpackIgnoreCommentRegexp);
5182

5283
if (matched) {
53-
needIgnore = matched[2] === "true";
84+
inBetween = matched[2] === "true";
5485
}
5586
}
5687

@@ -66,30 +97,26 @@ function visitor(result, parsedResults, node, key) {
6697
}
6798
}
6899

69-
parsed.walk((valueNode) => {
70-
if (valueNode.type === "comment") {
71-
const matched = valueNode.value.match(webpackIgnoreCommentRegexp);
72-
73-
needIgnore = matched && matched[2] === "true";
74-
75-
return;
76-
}
100+
let needIgnore;
77101

102+
parsed.walk((valueNode, index, valueNodes) => {
78103
if (valueNode.type !== "function") {
79104
return;
80105
}
81106

82-
if (isIgnoreOnDeclaration && needIgnore !== false) {
83-
return;
84-
}
107+
if (isUrlFunc.test(valueNode.value)) {
108+
needIgnore = getWebpackIgnoreCommentValue(index, valueNodes, inBetween);
85109

86-
if (needIgnore) {
87-
// eslint-disable-next-line no-undefined
88-
needIgnore = undefined;
89-
return;
90-
}
110+
if (isIgnoreOnDeclaration && needIgnore !== false) {
111+
return;
112+
}
113+
114+
if (needIgnore) {
115+
// eslint-disable-next-line no-undefined
116+
needIgnore = undefined;
117+
return;
118+
}
91119

92-
if (isUrlFunc.test(valueNode.value)) {
93120
const { nodes } = valueNode;
94121
const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
95122
const url = isStringValue ? nodes[0].value : valueParser.stringify(nodes);
@@ -109,25 +136,23 @@ function visitor(result, parsedResults, node, key) {
109136
// eslint-disable-next-line consistent-return
110137
return false;
111138
} else if (isImageSetFunc.test(valueNode.value)) {
112-
let imageSetWebpackIgnore = false;
113-
114-
for (const nNode of valueNode.nodes) {
139+
for (const [innerIndex, nNode] of valueNode.nodes.entries()) {
115140
const { type, value } = nNode;
116141

117-
if (type === "comment") {
118-
const matched = value.match(webpackIgnoreCommentRegexp);
142+
if (type === "function" && isUrlFunc.test(value)) {
143+
needIgnore = getWebpackIgnoreCommentValue(
144+
innerIndex,
145+
valueNode.nodes
146+
);
119147

120-
if (matched) {
121-
imageSetWebpackIgnore = matched[2] === "true";
148+
if (isIgnoreOnDeclaration && needIgnore !== false) {
149+
// eslint-disable-next-line no-continue
150+
continue;
122151
}
123152

124-
// eslint-disable-next-line no-continue
125-
continue;
126-
}
127-
128-
if (type === "function" && isUrlFunc.test(value)) {
129-
if (imageSetWebpackIgnore) {
130-
imageSetWebpackIgnore = false;
153+
if (needIgnore) {
154+
// eslint-disable-next-line no-undefined
155+
needIgnore = undefined;
131156
// eslint-disable-next-line no-continue
132157
continue;
133158
}
@@ -154,8 +179,19 @@ function visitor(result, parsedResults, node, key) {
154179
});
155180
}
156181
} else if (type === "string") {
157-
if (imageSetWebpackIgnore) {
158-
imageSetWebpackIgnore = false;
182+
needIgnore = getWebpackIgnoreCommentValue(
183+
innerIndex,
184+
valueNode.nodes
185+
);
186+
187+
if (isIgnoreOnDeclaration && needIgnore !== false) {
188+
// eslint-disable-next-line no-continue
189+
continue;
190+
}
191+
192+
if (needIgnore) {
193+
// eslint-disable-next-line no-undefined
194+
needIgnore = undefined;
159195
// eslint-disable-next-line no-continue
160196
continue;
161197
}

0 commit comments

Comments
 (0)