Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for aliases starting with / (options.alias) #597

Merged
merged 2 commits into from Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/loader.js
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
Expand Up @@ -83,6 +83,11 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
exports[exportName] = replaceImportsInString(exports[exportName]);
});

function startByAlias(url) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startsWithAlias => isAlias[ed]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a change to rename it to isAlias

// 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) && (startByAlias(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
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" }));
});