Skip to content

Commit c5fbbde

Browse files
committed
refactor resolve function
1 parent e2d54ef commit c5fbbde

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

lib/createResolver.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = function createResolver(alias) {
2+
if(typeof alias !== "object" || Array.isArray(alias)) {
3+
return function(url) {
4+
return url
5+
};
6+
}
7+
8+
alias = Object.keys(alias).map(function(key) {
9+
var onlyModule = false;
10+
var obj = alias[key];
11+
if(/\$$/.test(key)) {
12+
onlyModule = true;
13+
key = key.substr(0, key.length - 1);
14+
}
15+
if(typeof obj === "string") {
16+
obj = {
17+
alias: obj
18+
};
19+
}
20+
obj = Object.assign({
21+
name: key,
22+
onlyModule: onlyModule
23+
}, obj);
24+
return obj;
25+
});
26+
27+
return function(url) {
28+
alias.forEach(function(obj) {
29+
var name = obj.name;
30+
if(url === name || (!obj.onlyModule && url.startsWith(name + "/"))) {
31+
url = obj.alias + url.substr(name.length);
32+
}
33+
});
34+
return url;
35+
}
36+
}

lib/loader.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var loaderUtils = require("loader-utils");
66
var processCss = require("./processCss");
77
var getImportPrefix = require("./getImportPrefix");
88
var compileExports = require("./compile-exports");
9+
var createResolver = require("./createResolver");
910

1011

1112
module.exports = function(content, map) {
@@ -15,6 +16,7 @@ module.exports = function(content, map) {
1516
var root = query.root;
1617
var moduleMode = query.modules || query.module;
1718
var camelCaseKeys = query.camelCase || query.camelcase;
19+
var resolve = createResolver(query.alias);
1820

1921
if(map !== null && typeof map !== "string") {
2022
map = JSON.stringify(map);
@@ -69,19 +71,7 @@ module.exports = function(content, map) {
6971
var match = result.urlItemRegExp.exec(item);
7072
var idx = +match[1];
7173
var urlItem = result.urlItems[idx];
72-
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-
}
74+
var url = resolve(urlItem.url);
8575
idx = url.indexOf("?#");
8676
if(idx < 0) idx = url.indexOf("#");
8777
var urlRequest;

test/aliasTest.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
var test = require("./helpers").test;
44

55
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-
};
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+
};
2121

22-
function aliasOptions(alias) {
23-
return { options: { context: "", cssLoader: { alias: alias }}}
24-
}
22+
function aliasOptions(alias) {
23+
return { query: { alias: alias }}
24+
}
2525

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" }));
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" }));
3030
});

0 commit comments

Comments
 (0)