Skip to content

Commit 1a5f165

Browse files
committed
accept root query parameter for root-relative urls, fixes #11
1 parent 8ae2fe8 commit 1a5f165

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

index.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var loaderUtils = require("loader-utils");
88
module.exports = function(content) {
99
this.cacheable && this.cacheable();
1010
var result = [];
11+
var query = loaderUtils.parseQuery(this.query);
12+
var root = query.root;
1113
var tree = csso.parse(content, "stylesheet");
1214
if(tree && this && this.minimize) {
1315
tree = csso.compress(tree);
@@ -37,19 +39,19 @@ module.exports = function(content) {
3739
var uriRegExp = /%CSSURL\[%(.*?)%\]CSSURL%/g;
3840
css = css.replace(uriRegExp, function(str) {
3941
var match = /^%CSSURL\[%(.*?)%\]CSSURL%$/.exec(str);
40-
if(/^data:|^(https?:)?\/\//.test(match[1])) return match[1];
42+
if(!loaderUtils.isUrlRequest(match[1], root)) return match[1];
4143
var idx = match[1].indexOf("?");
4244
if(idx < 0) idx = match[1].indexOf("#");
4345
if(idx > 0) {
4446
// in cases like url('webfont.eot?#iefix')
4547
var url = JSON.parse("\"" + match[1].substr(0, idx) + "\"");
46-
return "\"+require(" + JSON.stringify(urlToRequire(url)) + ")+\"" + match[1].substr(idx);
48+
return "\"+require(" + JSON.stringify(loaderUtils.urlToRequest(url, root)) + ")+\"" + match[1].substr(idx);
4749
} else if(idx === 0) {
4850
// only hash
4951
return match[1];
5052
}
5153
var url = JSON.parse("\"" + match[1] + "\"");
52-
return "\"+require(" + JSON.stringify(urlToRequire(url)) + ")+\"";
54+
return "\"+require(" + JSON.stringify(loaderUtils.urlToRequest(url, root)) + ")+\"";
5355
});
5456
result.push(css);
5557
var cssRequest = loaderUtils.getRemainingRequest(this);
@@ -63,12 +65,6 @@ module.exports = function(content) {
6365
this.callback(null, stringWithMap.code, stringWithMap.map.toJSON());
6466
}
6567

66-
function urlToRequire(url) {
67-
if(/^~/.test(url))
68-
return url.substring(1);
69-
else
70-
return "./"+url;
71-
}
7268
function extractImports(tree) {
7369
var results = [];
7470
var removes = [];

0 commit comments

Comments
 (0)