Skip to content

Commit 014944c

Browse files
committed
add alias feature to rewrite urls
1 parent b6acfec commit 014944c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/loader.js

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ module.exports = function(content, map) {
7171
var idx = +match[1];
7272
var urlItem = result.urlItems[idx];
7373
var url = urlItem.url;
74+
var loaderOptions = this.options.cssLoader;
75+
if (loaderOptions && loaderOptions.alias) {
76+
var alias = loaderOptions.alias;
77+
Object.keys(alias).forEach(function(aliasName) {
78+
var aliasValue = alias[aliasName];
79+
var onlyModule = /\$$/.test(aliasName);
80+
if (onlyModule) aliasName = aliasName.substr(0, aliasName.length - 1);
81+
if ((!onlyModule && url.indexOf(aliasName + "/") === 0) || url === aliasName) {
82+
url = aliasValue + url.substr(aliasName.length);
83+
}
84+
});
85+
}
7486
idx = url.indexOf("?#");
7587
if(idx < 0) idx = url.indexOf("#");
7688
var urlRequest;

test/aliasTest.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*globals describe */
2+
3+
var test = require("./helpers").test;
4+
5+
describe("alias", function() {
6+
var css = ".className { background: url(./path/to/file.png); }";
7+
var exports = {
8+
without: [
9+
[1, ".className { background: url({./path/to/file.png}); }", ""]
10+
],
11+
onlyModule: [
12+
[1, ".className { background: url({module/file.png}); }", ""]
13+
],
14+
exactMatch: [
15+
[1, ".className { background: url({module/file.png}); }", ""]
16+
],
17+
notExactMatch: [
18+
[1, ".className { background: url({./path/to/file.png}); }", ""]
19+
]
20+
};
21+
22+
function aliasOptions(alias) {
23+
return { options: { context: "", cssLoader: { alias: alias }}}
24+
}
25+
26+
test("without", css, exports.without);
27+
test("onlyModule", css, exports.onlyModule, aliasOptions({ "./path/to": "module" }));
28+
test("exactMatch", css, exports.exactMatch, aliasOptions({ "./path/to/file.png$": "module/file.png" }));
29+
test("notExactMatch", css, exports.notExactMatch, aliasOptions({ "./path/to/file.jpg$": "module/file.jpg" }));
30+
});

0 commit comments

Comments
 (0)