diff --git a/lib/loader.js b/lib/loader.js index ca856cd3..cb8ff307 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -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 diff --git a/lib/processCss.js b/lib/processCss.js index 944bc89f..8979f7ad 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -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": @@ -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 = ""; @@ -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([ diff --git a/test/aliasTest.js b/test/aliasTest.js index 19ebaf16..2805c0bb 100644 --- a/test/aliasTest.js +++ b/test/aliasTest.js @@ -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" })); +});