Skip to content

Commit 08c551c

Browse files
refactor: better warning on invalid url resolution (#852)
1 parent b0aa159 commit 08c551c

File tree

1 file changed

+37
-46
lines changed

1 file changed

+37
-46
lines changed

src/plugins/postcss-url-parser.js

+37-46
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,31 @@ import valueParser from 'postcss-value-parser';
33

44
const pluginName = 'postcss-url-parser';
55

6+
function getArg(nodes) {
7+
return nodes.length !== 0 && nodes[0].type === 'string'
8+
? nodes[0].value
9+
: valueParser.stringify(nodes);
10+
}
11+
612
function walkUrls(parsed, callback) {
713
parsed.walk((node) => {
814
if (node.type !== 'function' || node.value.toLowerCase() !== 'url') {
915
return;
1016
}
1117

12-
const url =
13-
node.nodes.length !== 0 && node.nodes[0].type === 'string'
14-
? node.nodes[0].value
15-
: valueParser.stringify(node.nodes);
16-
1718
/* eslint-disable */
1819
node.before = '';
1920
node.after = '';
2021
/* eslint-enable */
2122

22-
callback(node, url);
23+
callback(node, getArg(node.nodes));
2324

2425
// Do not traverse inside url
2526
// eslint-disable-next-line consistent-return
2627
return false;
2728
});
2829
}
2930

30-
function filterUrls(parsed, result, decl, filter) {
31-
const urls = [];
32-
33-
walkUrls(parsed, (node, url) => {
34-
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
35-
result.warn(`Unable to find uri in '${decl.toString()}'`, {
36-
node: decl,
37-
});
38-
39-
return;
40-
}
41-
42-
if (filter && !filter(url)) {
43-
return;
44-
}
45-
46-
urls.push(url);
47-
});
48-
49-
return urls;
50-
}
51-
5231
function walkDeclsWithUrl(css, result, filter) {
5332
const items = [];
5433

@@ -58,13 +37,29 @@ function walkDeclsWithUrl(css, result, filter) {
5837
}
5938

6039
const parsed = valueParser(decl.value);
61-
const values = filterUrls(parsed, result, decl, filter);
40+
const urls = [];
41+
42+
walkUrls(parsed, (node, url) => {
43+
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
44+
result.warn(`Unable to find uri in '${decl.toString()}'`, {
45+
node: decl,
46+
});
6247

63-
if (values.length === 0) {
48+
return;
49+
}
50+
51+
if (filter && !filter(url)) {
52+
return;
53+
}
54+
55+
urls.push(url);
56+
});
57+
58+
if (urls.length === 0) {
6459
return;
6560
}
6661

67-
items.push({ decl, parsed, values });
62+
items.push({ decl, parsed, urls });
6863
});
6964

7065
return items;
@@ -74,19 +69,6 @@ function flatten(array) {
7469
return array.reduce((acc, d) => [...acc, ...d], []);
7570
}
7671

77-
function mapUrls(parsed, map) {
78-
walkUrls(parsed, (node, content) => {
79-
const placeholder = map(content);
80-
81-
if (!placeholder) {
82-
return;
83-
}
84-
85-
// eslint-disable-next-line no-param-reassign
86-
node.nodes = [{ type: 'word', value: map(content) }];
87-
});
88-
}
89-
9072
function uniq(array) {
9173
return array.reduce(
9274
(acc, d) => (acc.indexOf(d) === -1 ? [...acc, d] : acc),
@@ -99,7 +81,7 @@ export default postcss.plugin(
9981
(options = {}) =>
10082
function process(css, result) {
10183
const traversed = walkDeclsWithUrl(css, result, options.filter);
102-
const paths = uniq(flatten(traversed.map((item) => item.values)));
84+
const paths = uniq(flatten(traversed.map((item) => item.urls)));
10385

10486
if (paths.length === 0) {
10587
return;
@@ -120,7 +102,16 @@ export default postcss.plugin(
120102
});
121103

122104
traversed.forEach((item) => {
123-
mapUrls(item.parsed, (value) => urls[value]);
105+
walkUrls(item.parsed, (node, url) => {
106+
const value = urls[url];
107+
108+
if (!value) {
109+
return;
110+
}
111+
112+
// eslint-disable-next-line no-param-reassign
113+
node.nodes = [{ type: 'word', value }];
114+
});
124115

125116
// eslint-disable-next-line no-param-reassign
126117
item.decl.value = item.parsed.toString();

0 commit comments

Comments
 (0)