Skip to content

Commit

Permalink
add alias feature to rewrite urls
Browse files Browse the repository at this point in the history
  • Loading branch information
bbtfr committed Mar 18, 2017
1 parent 04dabca commit e2d54ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ module.exports = function(content, map) {
var idx = +match[1];
var urlItem = result.urlItems[idx];
var url = urlItem.url;
var loaderOptions = this.options.cssLoader;
if (loaderOptions && loaderOptions.alias) {
var alias = loaderOptions.alias;
Object.keys(alias).forEach(function(aliasName) {
var aliasValue = alias[aliasName];
var onlyModule = /\$$/.test(aliasName);
if (onlyModule) aliasName = aliasName.substr(0, aliasName.length - 1);
if ((!onlyModule && url.indexOf(aliasName + "/") === 0) || url === aliasName) {
url = aliasValue + url.substr(aliasName.length);
}
});
}
idx = url.indexOf("?#");
if(idx < 0) idx = url.indexOf("#");
var urlRequest;
Expand Down
30 changes: 30 additions & 0 deletions test/aliasTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*globals describe */

var test = require("./helpers").test;

describe("alias", function() {
var css = ".className { background: url(./path/to/file.png); }";
var exports = {
without: [
[1, ".className { background: url({./path/to/file.png}); }", ""]
],
onlyModule: [
[1, ".className { background: url({module/file.png}); }", ""]
],
exactMatch: [
[1, ".className { background: url({module/file.png}); }", ""]
],
notExactMatch: [
[1, ".className { background: url({./path/to/file.png}); }", ""]
]
};

function aliasOptions(alias) {
return { options: { context: "", cssLoader: { alias: alias }}}
}

test("without", css, exports.without);
test("onlyModule", css, exports.onlyModule, aliasOptions({ "./path/to": "module" }));
test("exactMatch", css, exports.exactMatch, aliasOptions({ "./path/to/file.png$": "module/file.png" }));
test("notExactMatch", css, exports.notExactMatch, aliasOptions({ "./path/to/file.jpg$": "module/file.jpg" }));
});

0 comments on commit e2d54ef

Please sign in to comment.