Skip to content

Resolved https://github.com/webpack/css-loader/issues/102 #183

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,28 @@ module.exports = function(content, map) {
"[" + JSON.stringify(importItem.export) + "] + \"";
}

cssAsString = cssAsString.replace(result.importItemRegExpG, importItemMatcher.bind(this)).replace(result.urlItemRegExpG, function(item) {
var match = result.urlItemRegExp.exec(item);
var idx = +match[1];
var urlItem = result.urlItems[idx];
var url = urlItem.url;
idx = url.indexOf("?#");
if(idx < 0) idx = url.indexOf("#");
var urlRequest;
if(idx > 0) { // idx === 0 is catched by isUrlRequest
// in cases like url('webfont.eot?#iefix')
urlRequest = url.substr(0, idx);
return "\" + require(" + loaderUtils.stringifyRequest(this, urlRequest) + ") + \"" +
url.substr(idx);
}
urlRequest = url;
return "\" + require(" + loaderUtils.stringifyRequest(this, urlRequest) + ") + \"";
}.bind(this));
if(!query.hasOwnProperty('import')) {
Copy link
Member

Choose a reason for hiding this comment

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

Please don't check for exisitance of query properties. Check for the value. query.import !== false. Document it as css-loader?-url&-import.

Elsewise this cases would be broken:

  • css-loader?url
  • css-loader?{url: true}

cssAsString = cssAsString.replace(result.importItemRegExpG, importItemMatcher.bind(this));
}
if(!query.hasOwnProperty('url')) {
cssAsString = cssAsString.replace(result.urlItemRegExpG, function(item) {
var match = result.urlItemRegExp.exec(item);
var idx = +match[1];
var urlItem = result.urlItems[idx];
var url = urlItem.url;
idx = url.indexOf("?#");
if(idx < 0) idx = url.indexOf("#");
var urlRequest;
if(idx > 0) { // idx === 0 is catched by isUrlRequest
// in cases like url('webfont.eot?#iefix')
urlRequest = url.substr(0, idx);
return "\" + require(" + loaderUtils.stringifyRequest(this, urlRequest) + ") + \"" +
url.substr(idx);
}
urlRequest = url;
return "\" + require(" + loaderUtils.stringifyRequest(this, urlRequest) + ") + \"";
}.bind(this));
}


var exportJs = compileExports(result, importItemMatcher.bind(this), camelCaseKeys);
Expand Down
55 changes: 32 additions & 23 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
var urlItems = [];

function replaceImportsInString(str) {
var tokens = str.split(/(\S+)/);
tokens = tokens.map(function(token) {
var importIndex = imports["$" + token];
if(typeof importIndex === "number") {
return "___CSS_LOADER_IMPORT___" + importIndex + "___";
}
return token;
});
return tokens.join("");
if(options.import) {
var tokens = str.split(/(\S+)/);
tokens = tokens.map(function (token) {
var importIndex = imports["$" + token];
if(typeof importIndex === "number") {
return "___CSS_LOADER_IMPORT___" + importIndex + "___";
}
return token;
});
return tokens.join("");
}
return str;
}

css.walkAtRules("import", function(rule) {
Expand Down Expand Up @@ -83,23 +86,27 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
value.nodes.forEach(function(item) {
switch(item.type) {
case "item":
var importIndex = imports["$" + item.name];
if(typeof importIndex === "number") {
item.name = "___CSS_LOADER_IMPORT___" + importIndex + "___";
if(options.import) {
var importIndex = imports["$" + item.name];
if(typeof importIndex === "number") {
item.name = "___CSS_LOADER_IMPORT___" + importIndex + "___";
}
}
break;
case "url":
if(!/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
item.stringType = "";
delete item.innerSpacingBefore;
delete item.innerSpacingAfter;
var url = item.url;
item.url = "___CSS_LOADER_URL___" + urlItems.length + "___";
urlItems.push({
url: url
});
if(options.url) {
if(!/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) {
item.stringType = "";
delete item.innerSpacingBefore;
delete item.innerSpacingAfter;
var url = item.url;
item.url = "___CSS_LOADER_URL___" + urlItems.length + "___";
urlItems.push({
url: url
});
}
break;
Copy link
Member

Choose a reason for hiding this comment

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

The break should be outside of the if, at the end of the case clause.

}
break;
}
});
});
Expand Down Expand Up @@ -129,7 +136,9 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {

var parserOptions = {
root: root,
mode: options.mode
mode: options.mode,
url: !query.hasOwnProperty('url'),
import: !query.hasOwnProperty('import')
};

var pipeline = postcss([
Expand Down