Skip to content

Commit e2d54ef

Browse files
committed
add alias feature to rewrite urls
1 parent 04dabca commit e2d54ef

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/loader.js

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

test/aliasTest.js

Lines changed: 30 additions & 0 deletions
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)