Skip to content

Commit 63567f2

Browse files
atinuxjoshwiens
authored andcommitted
fix: add support for aliases starting with / (options.alias) (#597)
* Add support for alias starting with / * startByAlias -> isAlias
1 parent e16bdeb commit 63567f2

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

lib/loader.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = function(content, map) {
4242
from: loaderUtils.getRemainingRequest(this).split("!").pop(),
4343
to: loaderUtils.getCurrentRequest(this).split("!").pop(),
4444
query: query,
45+
resolve: resolve,
4546
minimize: this.minimize,
4647
loaderContext: this,
4748
sourceMap: sourceMap

lib/processCss.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
8383
exports[exportName] = replaceImportsInString(exports[exportName]);
8484
});
8585

86+
function isAlias(url) {
87+
// Handle alias starting by / and root disabled
88+
return url !== options.resolve(url)
89+
}
90+
8691
function processNode(item) {
8792
switch (item.type) {
8893
case "value":
@@ -98,7 +103,7 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
98103
}
99104
break;
100105
case "url":
101-
if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
106+
if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && (isAlias(item.url) || loaderUtils.isUrlRequest(item.url, options.root))) {
102107
// Don't remove quotes around url when contain space
103108
if (item.url.indexOf(" ") === -1) {
104109
item.stringType = "";
@@ -149,7 +154,8 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
149154
root: root,
150155
mode: options.mode,
151156
url: query.url !== false,
152-
import: query.import !== false
157+
import: query.import !== false,
158+
resolve: options.resolve
153159
};
154160

155161
var pipeline = postcss([

test/aliasTest.js

+27
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ describe("alias", function() {
2828
test("exactMatch", css, exports.exactMatch, aliasOptions({ "./path/to/file.png$": "module/file.png" }));
2929
test("notExactMatch", css, exports.notExactMatch, aliasOptions({ "./path/to/file.jpg$": "module/file.jpg" }));
3030
});
31+
32+
describe("alias starting with /", function() {
33+
var css = ".className { background: url(/path/to/file.png); }";
34+
var exports = {
35+
without: [
36+
[1, ".className { background: url(/path/to/file.png); }", ""]
37+
],
38+
onlyModule: [
39+
[1, ".className { background: url({module/file.png}); }", ""]
40+
],
41+
exactMatch: [
42+
[1, ".className { background: url({module/file.png}); }", ""]
43+
],
44+
notExactMatch: [
45+
[1, ".className { background: url(/path/to/file.png); }", ""]
46+
]
47+
};
48+
49+
function aliasOptions(alias) {
50+
return { query: { alias: alias }}
51+
}
52+
53+
test("without", css, exports.without);
54+
test("onlyModule", css, exports.onlyModule, aliasOptions({ "/path/to": "module" }));
55+
test("exactMatch", css, exports.exactMatch, aliasOptions({ "/path/to/file.png$": "module/file.png" }));
56+
test("notExactMatch", css, exports.notExactMatch, aliasOptions({ "/path/to/file.jpg$": "module/file.jpg" }));
57+
});

0 commit comments

Comments
 (0)