Skip to content

Commit f736ed0

Browse files
feat: emit warning on invalid url
1 parent 947e666 commit f736ed0

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

lib/plugins/postcss-url-parser.js

+24-16
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,55 @@ function walkUrls(parsed, callback) {
2020
node.after = '';
2121
/* eslint-enable */
2222

23-
if (url.trim().replace(/\\[\r\n]/g, '').length !== 0) {
24-
callback(node, url);
25-
}
23+
callback(node, url);
2624

2725
// Do not traverse inside url
2826
// eslint-disable-next-line consistent-return
2927
return false;
3028
});
3129
}
3230

33-
function filterUrls(parsed, filter) {
34-
const result = [];
31+
function filterUrls(parsed, result, decl, filter) {
32+
const urls = [];
3533

36-
walkUrls(parsed, (node, content) => {
37-
if (!filter(content)) {
34+
walkUrls(parsed, (node, url) => {
35+
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
36+
result.warn(`Unable to find uri in '${decl.toString()}'`, {
37+
node: decl,
38+
});
39+
40+
return;
41+
}
42+
43+
if (!filter(url)) {
3844
return;
3945
}
4046

41-
result.push(content);
47+
urls.push(url);
4248
});
4349

44-
return result;
50+
return urls;
4551
}
4652

47-
function walkDeclsWithUrl(css, filter) {
48-
const result = [];
53+
function walkDeclsWithUrl(css, result, filter) {
54+
const items = [];
4955

5056
css.walkDecls((decl) => {
5157
if (!/url\(/i.test(decl.value)) {
5258
return;
5359
}
5460

5561
const parsed = valueParser(decl.value);
56-
const values = filterUrls(parsed, filter);
62+
const values = filterUrls(parsed, result, decl, filter);
5763

5864
if (values.length === 0) {
5965
return;
6066
}
6167

62-
result.push({ decl, parsed, values });
68+
items.push({ decl, parsed, values });
6369
});
6470

65-
return result;
71+
return items;
6672
}
6773

6874
function flatten(array) {
@@ -92,9 +98,11 @@ function uniq(array) {
9298
module.exports = postcss.plugin(
9399
pluginName,
94100
(options) =>
95-
function process(css) {
101+
function process(css, result) {
96102
const urlItems = [];
97-
const traversed = walkDeclsWithUrl(css, (value) => isUrlRequest(value));
103+
const traversed = walkDeclsWithUrl(css, result, (value) =>
104+
isUrlRequest(value)
105+
);
98106
const paths = uniq(flatten(traversed.map((item) => item.values)));
99107

100108
if (paths.length === 0) {

test/__snapshots__/url-option.test.js.snap

+25-1
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,28 @@ exports.push([module.id, \\".class {\\\\n background: url(\\" + escape(require(
421421
"
422422
`;
423423
424-
exports[`url option true: warnings 1`] = `Array []`;
424+
exports[`url option true: warnings 1`] = `
425+
Array [
426+
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
427+
Warning
428+
429+
(120:3) Unable to find uri in 'background: green url() xyz'],
430+
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
431+
Warning
432+
433+
(124:3) Unable to find uri in 'background: green url('') xyz'],
434+
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
435+
Warning
436+
437+
(128:3) Unable to find uri in 'background: green url("") xyz'],
438+
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
439+
Warning
440+
441+
(132:3) Unable to find uri in 'background: green url('') xyz'],
442+
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
443+
Warning
444+
445+
(136:3) Unable to find uri in 'background: green url(
446+
) xyz'],
447+
]
448+
`;

0 commit comments

Comments
 (0)