-
-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add alias feature to rewrite URLs (#274)
- Loading branch information
1 parent
04dabca
commit c8db489
Showing
4 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module.exports = function createResolver(alias) { | ||
if(typeof alias !== "object" || Array.isArray(alias)) { | ||
return function(url) { | ||
return url | ||
}; | ||
} | ||
|
||
alias = Object.keys(alias).map(function(key) { | ||
var onlyModule = false; | ||
var obj = alias[key]; | ||
if(/\$$/.test(key)) { | ||
onlyModule = true; | ||
key = key.substr(0, key.length - 1); | ||
} | ||
if(typeof obj === "string") { | ||
obj = { | ||
alias: obj | ||
}; | ||
} | ||
obj = Object.assign({ | ||
name: key, | ||
onlyModule: onlyModule | ||
}, obj); | ||
return obj; | ||
}); | ||
|
||
return function(url) { | ||
alias.forEach(function(obj) { | ||
var name = obj.name; | ||
if(url === name || (!obj.onlyModule && url.startsWith(name + "/"))) { | ||
url = obj.alias + url.substr(name.length); | ||
} | ||
}); | ||
return url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { query: { 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" })); | ||
}); |