Skip to content

Commit b4a620d

Browse files
committed
added support for nested items
1 parent 1298d2b commit b4a620d

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

lib/processCss.js

+46-21
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,55 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
7777
exports[exportName] = replaceImportsInString(exports[exportName]);
7878
});
7979

80+
function processNode(item, isValue) {
81+
switch (item.type) {
82+
case "value":
83+
item.nodes.forEach(function(node){
84+
processNode(node, true);
85+
});
86+
break;
87+
case "nested-item":
88+
item.nodes.forEach(function(node){
89+
processNode(node);
90+
});
91+
break;
92+
case "item":
93+
var importIndex = imports["$" + item.name];
94+
if (typeof importIndex === "number") {
95+
item.name = "___CSS_LOADER_IMPORT___" + importIndex + "___";
96+
}
97+
break;
98+
case "url":
99+
if (!/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
100+
item.stringType = "";
101+
delete item.innerSpacingBefore;
102+
delete item.innerSpacingAfter;
103+
var url;
104+
105+
if(isValue) {
106+
if(loaderUtils.isUrlRequest(url, options.root) && options.mode === "global") {
107+
url = loaderUtils.urlToRequest(item.url, options.root);
108+
} else {
109+
url = item.url;
110+
}
111+
} else {
112+
url = item.url;
113+
}
114+
115+
item.url = "___CSS_LOADER_URL___" + urlItems.length + "___";
116+
urlItems.push({
117+
url: url
118+
});
119+
}
120+
break;
121+
}
122+
}
123+
80124
css.walkDecls(function(decl) {
81125
var values = Tokenizer.parseValues(decl.value);
82126
values.nodes.forEach(function(value) {
83-
value.nodes.forEach(function(item) {
84-
switch(item.type) {
85-
case "item":
86-
var importIndex = imports["$" + item.name];
87-
if(typeof importIndex === "number") {
88-
item.name = "___CSS_LOADER_IMPORT___" + importIndex + "___";
89-
}
90-
break;
91-
case "url":
92-
if(!/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
93-
item.stringType = "";
94-
delete item.innerSpacingBefore;
95-
delete item.innerSpacingAfter;
96-
var url = item.url;
97-
item.url = "___CSS_LOADER_URL___" + urlItems.length + "___";
98-
urlItems.push({
99-
url: url
100-
});
101-
}
102-
break;
103-
}
127+
value.nodes.forEach(function(node){
128+
processNode(node);
104129
});
105130
});
106131
decl.value = Tokenizer.stringifyValues(values);

test/urlTest.js

+3
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,7 @@ describe("url", function() {
5050
test("keyframe background img", "@keyframes anim { background: green url('img.png') xyz }", [
5151
[1, "@keyframes anim { background: green url({./img.png}) xyz }", ""]
5252
]);
53+
test("-webkit-image-set", "background-image: -webkit-image-set(url('url1x.png') 1x, url('url2x.png') 2x)", [
54+
[1, "background-image: -webkit-image-set(url({./url1x.png}) 1x, url({./url2x.png}) 2x)", ""]
55+
]);
5356
});

0 commit comments

Comments
 (0)