Skip to content

Commit 8c54fd1

Browse files
committed
fixed css-loader/locals
1 parent 22596d1 commit 8c54fd1

File tree

4 files changed

+89
-66
lines changed

4 files changed

+89
-66
lines changed

lib/generateLocals.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var loaderUtils = require("loader-utils");
6+
module.exports = function(locals, localExtends, importedUrls, importUrlPrefix, result, importAccess) {
7+
var localKeys = Object.keys(locals);
8+
if(localKeys.length > 0) {
9+
var localLines = localKeys.map(function(key, idx) {
10+
var line = " " + JSON.stringify(key) + ": ";
11+
function addExtend(extend) {
12+
if(extend.from) {
13+
var importUrl = importUrlPrefix + extend.from;
14+
if(importedUrls && result && importedUrls.indexOf(importUrl) < 0) {
15+
result.push("exports.i(require(" + loaderUtils.stringifyRequest(this, importUrl) + "), \"\");");
16+
importedUrls.push(importUrl);
17+
}
18+
line += " + \" \" + require(" + loaderUtils.stringifyRequest(this, importUrl) + ")" + importAccess + "[" + JSON.stringify(extend.name) + "]";
19+
} else if(locals[extend.name]) {
20+
line += " + \" \" + " + JSON.stringify(locals[extend.name]);
21+
if(localExtends[extend.name]) {
22+
localExtends[extend.name].forEach(addExtend, this);
23+
}
24+
} else if(this.emitError) {
25+
this.emitError("Cannot extend from unknown class '" + extend.name + "'");
26+
}
27+
}
28+
line += JSON.stringify(locals[key]);
29+
if(localExtends[key]) {
30+
localExtends[key].forEach(addExtend, this);
31+
}
32+
if(idx !== localKeys.length - 1) line += ",";
33+
return line;
34+
}, this);
35+
return "{\n" + localLines.join("\n") + "\n}";
36+
}
37+
};

lib/loader.js

+4-54
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var ReplaceMany = require("./ReplaceMany");
88
var loaderUtils = require("loader-utils");
99
var SourceListMap = require("source-list-map").SourceListMap;
1010
var CleanCSS = require("clean-css");
11-
var getLocalIdent = require("./getLocalIdent");
1211

1312
module.exports = function(content, map) {
1413
if(this.cacheable) this.cacheable();
@@ -17,8 +16,6 @@ module.exports = function(content, map) {
1716
var forceMinimize = query.minimize;
1817
var importLoaders = parseInt(query.importLoaders, 10) || 0;
1918
var minimize = typeof forceMinimize !== "undefined" ? !!forceMinimize : (this && this.minimize);
20-
var localIdentName = query.localIdentName || "[hash:base64]";
21-
var localIdentRegExp = query.localIdentRegExp;
2219

2320
if(typeof map !== "string") {
2421
map = JSON.stringify(map);
@@ -65,26 +62,7 @@ module.exports = function(content, map) {
6562
// replace :local()
6663
var locals = {};
6764
var localExtends = {};
68-
stuff.locals.forEach(function(local) {
69-
var ident;
70-
var name = local.name;
71-
if(!locals[name]) {
72-
ident = getLocalIdent(this, localIdentName, name, {
73-
regExp: localIdentRegExp
74-
});
75-
locals[name] = ident;
76-
} else {
77-
ident = locals[name];
78-
}
79-
if(local.extends) {
80-
local.extends.forEach(function(extend) {
81-
if(!localExtends[name])
82-
localExtends[name] = [];
83-
localExtends[name].push(extend);
84-
});
85-
}
86-
replacer.replace(local.start, local.length, local.prefix + ident);
87-
}, this);
65+
require("./processLocals").call(this, stuff.locals, query, replacer, locals, localExtends);
8866

8967
// remove stuff
9068
stuff.remove.forEach(function(rem) {
@@ -99,37 +77,9 @@ module.exports = function(content, map) {
9977
}
10078

10179
// generate the locals
102-
var localKeys = Object.keys(locals);
103-
if(localKeys.length > 0) {
104-
var localLines = localKeys.map(function(key, idx) {
105-
var line = " " + JSON.stringify(key) + ": ";
106-
function addExtend(extend) {
107-
if(extend.from) {
108-
var importUrl = importUrlPrefix + extend.from;
109-
if(importedUrls.indexOf(importUrl) < 0) {
110-
result.push("exports.i(require(" + loaderUtils.stringifyRequest(this, importUrl) + "), \"\");");
111-
importedUrls.push(importUrl);
112-
}
113-
line += " + \" \" + require(" + loaderUtils.stringifyRequest(this, importUrl) + ").locals[" + JSON.stringify(extend.name) + "]";
114-
} else if(locals[extend.name]) {
115-
line += " + \" \" + " + JSON.stringify(locals[extend.name]);
116-
if(localExtends[extend.name]) {
117-
localExtends[extend.name].forEach(addExtend, this);
118-
}
119-
} else if(this.emitError) {
120-
this.emitError("Cannot extend from unknown class '" + extend.name + "'");
121-
}
122-
}
123-
line += JSON.stringify(locals[key]);
124-
if(localExtends[key]) {
125-
localExtends[key].forEach(addExtend, this);
126-
}
127-
if(idx !== localKeys.length - 1) line += ",";
128-
return line;
129-
}, this);
130-
result.push("exports.locals = {");
131-
result.push(localLines.join("\n"));
132-
result.push("};");
80+
var localsData = require("./generateLocals").call(this, locals, localExtends, importedUrls, importUrlPrefix, result, ".locals");
81+
if(localsData) {
82+
result.push("exports.locals = " + localsData + ";");
13383
}
13484

13585
// transform the CSS

lib/localsLoader.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
*/
55
var loaderUtils = require("loader-utils");
66
var parseSource = require("./parseSource");
7-
var getLocalIdent = require("./getLocalIdent");
87

98
module.exports = function(content) {
109
if(this.cacheable) this.cacheable();
1110
var query = loaderUtils.parseQuery(this.query);
12-
var localIdentName = query.localIdentName || "[hash:base64]";
13-
var localIdentRegExp = query.localIdentRegExp;
11+
var importLoaders = parseInt(query.importLoaders, 10) || 0;
12+
13+
// for importing CSS
14+
var loadersRequest = this.loaders.slice(
15+
this.loaderIndex,
16+
this.loaderIndex + 1 + importLoaders
17+
).map(function(x) { return x.request; }).join("!");
18+
var importUrlPrefix = "-!" + loadersRequest + "!";
1419

1520
var stuff = parseSource(content);
1621

1722
var locals = {};
18-
stuff.locals.forEach(function(local) {
19-
if(!locals[local.name]) {
20-
locals[local.name] = getLocalIdent(this, localIdentName, local.name, {
21-
regExp: localIdentRegExp
22-
});
23-
}
24-
}, this);
25-
26-
return "module.exports = " + JSON.stringify(locals) + ";";
23+
var localExtends = {};
24+
require("./processLocals").call(this, stuff.locals, query, null, locals, localExtends);
25+
26+
27+
// generate the locals
28+
var localsData = require("./generateLocals").call(this, locals, localExtends, null, importUrlPrefix, null, "");
29+
30+
31+
return "module.exports = " + localsData + ";";
2732
};

lib/processLocals.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var getLocalIdent = require("./getLocalIdent");
6+
module.exports = function(parsedLocals, query, replacer, locals, localExtends) {
7+
var localIdentName = query.localIdentName || "[hash:base64]";
8+
var localIdentRegExp = query.localIdentRegExp;
9+
10+
parsedLocals.forEach(function(local) {
11+
var ident;
12+
var name = local.name;
13+
if(!locals[name]) {
14+
ident = getLocalIdent(this, localIdentName, name, {
15+
regExp: localIdentRegExp
16+
});
17+
locals[name] = ident;
18+
} else {
19+
ident = locals[name];
20+
}
21+
if(local.extends) {
22+
local.extends.forEach(function(extend) {
23+
if(!localExtends[name])
24+
localExtends[name] = [];
25+
localExtends[name].push(extend);
26+
});
27+
}
28+
if(replacer)
29+
replacer.replace(local.start, local.length, local.prefix + ident);
30+
}, this);
31+
};

0 commit comments

Comments
 (0)