Skip to content

Commit 53d40a9

Browse files
feat: added support webpackIgnore comment (#1264)
1 parent e18c5a9 commit 53d40a9

14 files changed

+3406
-24
lines changed

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,60 @@ module.exports = {
11321132

11331133
## Examples
11341134

1135+
### Disable url resolving using the `/* webpackIgnore: true */` comment
1136+
1137+
With the help of the `/* webpackIgnore: true */`comment, it is possible to disable sources handling for rules and for individual declarations.
1138+
1139+
```css
1140+
/* webpackIgnore: true */
1141+
@import url(./basic.css);
1142+
@import /* webpackIgnore: true */ url(./imported.css);
1143+
1144+
.class {
1145+
/* Disabled url handling for the all urls in the 'background' declaration */
1146+
color: red;
1147+
/* webpackIgnore: true */
1148+
background: url("./url/img.png"), url("./url/img.png");
1149+
}
1150+
1151+
.class {
1152+
/* Disabled url handling for the first url in the 'background' declaration */
1153+
color: red;
1154+
background:
1155+
/* webpackIgnore: true */ url("./url/img.png"), url("./url/img.png");
1156+
}
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 */
1166+
.class {
1167+
/* Disabled url handling for the second url in the 'background' declaration */
1168+
color: red;
1169+
background: url("./url/img.png"),
1170+
/* webpackIgnore: true */
1171+
url("./url/img.png");
1172+
}
1173+
1174+
/* prettier-ignore */
1175+
.class {
1176+
/* Disabled url handling for third and sixth urls in the 'background-image' declaration */
1177+
background-image: image-set(
1178+
url(./url/img.png) 2x,
1179+
url(./url/img.png) 3x,
1180+
/* webpackIgnore: true */ url(./url/img.png) 4x,
1181+
url(./url/img.png) 5x,
1182+
url(./url/img.png) 6x,
1183+
/* webpackIgnore: true */
1184+
url(./url/img.png) 7x
1185+
);
1186+
}
1187+
```
1188+
11351189
### Assets
11361190

11371191
The following `webpack.config.js` can load CSS files, embed small PNG/JPG/GIF/SVG images as well as fonts as [Data URLs](https://tools.ietf.org/html/rfc2397) and copy larger files to the output directory.

src/plugins/postcss-import-parser.js

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
resolveRequests,
66
isUrlRequestable,
77
requestify,
8+
webpackIgnoreCommentRegexp,
89
} from "../utils";
910

1011
function visitor(result, parsedResults, node, key) {
@@ -13,6 +14,27 @@ function visitor(result, parsedResults, node, key) {
1314
return;
1415
}
1516

17+
if (node.raws.afterName && node.raws.afterName.trim().length > 0) {
18+
const lastCommentIndex = node.raws.afterName.lastIndexOf("/*");
19+
const matched = node.raws.afterName
20+
.slice(lastCommentIndex)
21+
.match(webpackIgnoreCommentRegexp);
22+
23+
if (matched && matched[2] === "true") {
24+
return;
25+
}
26+
}
27+
28+
const prevNode = node.prev();
29+
30+
if (prevNode && prevNode.type === "comment") {
31+
const matched = prevNode.text.match(webpackIgnoreCommentRegexp);
32+
33+
if (matched && matched[2] === "true") {
34+
return;
35+
}
36+
}
37+
1638
// Nodes do not exists - `@import url('http://') :root {}`
1739
if (node.nodes) {
1840
result.warn(

src/plugins/postcss-url-parser.js

+115-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
requestify,
66
resolveRequests,
77
isUrlRequestable,
8+
webpackIgnoreCommentRegexp,
89
} from "../utils";
910

1011
const isUrlFunc = /url/i;
@@ -30,19 +31,94 @@ function shouldHandleRule(rule, node, result) {
3031
return true;
3132
}
3233

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+
3365
function visitor(result, parsedResults, node, key) {
3466
if (!needParseDeclaration.test(node[key])) {
3567
return;
3668
}
3769

38-
const parsed = valueParser(node[key]);
70+
const parsed = valueParser(
71+
typeof node.raws.value === "undefined" ? node[key] : node.raws.value.raw
72+
);
73+
74+
let inBetween;
75+
76+
if (typeof node.raws.between !== "undefined") {
77+
const lastCommentIndex = node.raws.between.lastIndexOf("/*");
78+
79+
const matched = node.raws.between
80+
.slice(lastCommentIndex)
81+
.match(webpackIgnoreCommentRegexp);
82+
83+
if (matched) {
84+
inBetween = matched[2] === "true";
85+
}
86+
}
87+
88+
let isIgnoreOnDeclaration = false;
3989

40-
parsed.walk((valueNode) => {
90+
const prevNode = node.prev();
91+
92+
if (prevNode && prevNode.type === "comment") {
93+
const matched = prevNode.text.match(webpackIgnoreCommentRegexp);
94+
95+
if (matched) {
96+
isIgnoreOnDeclaration = matched[2] === "true";
97+
}
98+
}
99+
100+
let needIgnore;
101+
102+
parsed.walk((valueNode, index, valueNodes) => {
41103
if (valueNode.type !== "function") {
42104
return;
43105
}
44106

45107
if (isUrlFunc.test(valueNode.value)) {
108+
needIgnore = getWebpackIgnoreCommentValue(index, valueNodes, inBetween);
109+
110+
if (
111+
(isIgnoreOnDeclaration && typeof needIgnore === "undefined") ||
112+
needIgnore
113+
) {
114+
if (needIgnore) {
115+
// eslint-disable-next-line no-undefined
116+
needIgnore = undefined;
117+
}
118+
119+
return;
120+
}
121+
46122
const { nodes } = valueNode;
47123
const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
48124
const url = isStringValue ? nodes[0].value : valueParser.stringify(nodes);
@@ -62,10 +138,28 @@ function visitor(result, parsedResults, node, key) {
62138
// eslint-disable-next-line consistent-return
63139
return false;
64140
} else if (isImageSetFunc.test(valueNode.value)) {
65-
for (const nNode of valueNode.nodes) {
141+
for (const [innerIndex, nNode] of valueNode.nodes.entries()) {
66142
const { type, value } = nNode;
67143

68144
if (type === "function" && isUrlFunc.test(value)) {
145+
needIgnore = getWebpackIgnoreCommentValue(
146+
innerIndex,
147+
valueNode.nodes
148+
);
149+
150+
if (
151+
(isIgnoreOnDeclaration && typeof needIgnore === "undefined") ||
152+
needIgnore
153+
) {
154+
if (needIgnore) {
155+
// eslint-disable-next-line no-undefined
156+
needIgnore = undefined;
157+
}
158+
159+
// eslint-disable-next-line no-continue
160+
continue;
161+
}
162+
69163
const { nodes } = nNode;
70164
const isStringValue =
71165
nodes.length !== 0 && nodes[0].type === "string";
@@ -88,6 +182,24 @@ function visitor(result, parsedResults, node, key) {
88182
});
89183
}
90184
} else if (type === "string") {
185+
needIgnore = getWebpackIgnoreCommentValue(
186+
innerIndex,
187+
valueNode.nodes
188+
);
189+
190+
if (
191+
(isIgnoreOnDeclaration && typeof needIgnore === "undefined") ||
192+
needIgnore
193+
) {
194+
if (needIgnore) {
195+
// eslint-disable-next-line no-undefined
196+
needIgnore = undefined;
197+
}
198+
199+
// eslint-disable-next-line no-continue
200+
continue;
201+
}
202+
91203
const rule = {
92204
node: nNode,
93205
url: value,
@@ -104,7 +216,6 @@ function visitor(result, parsedResults, node, key) {
104216
}
105217
}
106218
}
107-
108219
// Do not traverse inside `image-set`
109220
// eslint-disable-next-line consistent-return
110221
return false;

src/utils.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const unescapeRegExp = new RegExp(
1919
"ig"
2020
);
2121
const matchNativeWin32Path = /^[A-Z]:[/\\]|^\\\\/i;
22+
const webpackIgnoreCommentRegexp = /webpackIgnore:(\s+)?(true|false)/;
2223

2324
function unescape(str) {
2425
return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
@@ -721,4 +722,5 @@ export {
721722
resolveRequests,
722723
isUrlRequestable,
723724
sort,
725+
webpackIgnoreCommentRegexp,
724726
};

0 commit comments

Comments
 (0)