Skip to content

Commit

Permalink
fix: add support for aliases starting with / (options.alias) (#597)
Browse files Browse the repository at this point in the history
* Add support for alias starting with /

* startByAlias -> isAlias
  • Loading branch information
atinux authored and joshwiens committed Aug 30, 2017
1 parent e16bdeb commit 63567f2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = function(content, map) {
from: loaderUtils.getRemainingRequest(this).split("!").pop(),
to: loaderUtils.getCurrentRequest(this).split("!").pop(),
query: query,
resolve: resolve,
minimize: this.minimize,
loaderContext: this,
sourceMap: sourceMap
Expand Down
10 changes: 8 additions & 2 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
exports[exportName] = replaceImportsInString(exports[exportName]);
});

function isAlias(url) {
// Handle alias starting by / and root disabled
return url !== options.resolve(url)
}

function processNode(item) {
switch (item.type) {
case "value":
Expand All @@ -98,7 +103,7 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
}
break;
case "url":
if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && (isAlias(item.url) || loaderUtils.isUrlRequest(item.url, options.root))) {
// Don't remove quotes around url when contain space
if (item.url.indexOf(" ") === -1) {
item.stringType = "";
Expand Down Expand Up @@ -149,7 +154,8 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
root: root,
mode: options.mode,
url: query.url !== false,
import: query.import !== false
import: query.import !== false,
resolve: options.resolve
};

var pipeline = postcss([
Expand Down
27 changes: 27 additions & 0 deletions test/aliasTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ describe("alias", function() {
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" }));
});

describe("alias starting with /", 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" }));
});

0 comments on commit 63567f2

Please sign in to comment.