Skip to content

Commit 55ad466

Browse files
committed
changed hash creation to something more reproducable
added context and hashPrefix option fixes #157
1 parent e5b308d commit 55ad466

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

lib/getLocalIdent.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
Author Tobias Koppers @sokra
44
*/
55
var loaderUtils = require("loader-utils");
6+
var path = require("path");
7+
68
module.exports = function getLocalIdent(loaderContext, localIdentName, localName, options) {
7-
var request = loaderContext.options && typeof loaderContext.options.context === "string" ?
8-
loaderUtils.stringifyRequest({ context: loaderContext.options.context }, loaderUtils.getRemainingRequest(loaderContext)) :
9-
loaderContext.request;
10-
options.content = localName + " " + request;
11-
options.context = loaderContext.options && typeof loaderContext.options.context === "string" ? loaderContext.options.context : loaderContext.context;
9+
if(!options.context)
10+
options.context = loaderContext.options && typeof loaderContext.options.context === "string" ? loaderContext.options.context : loaderContext.context;
11+
var request = path.relative(options.context, loaderContext.resourcePath);
12+
options.content = options.hashPrefix + request + "+" + localName;
1213
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
1314
var hash = loaderUtils.interpolateName(loaderContext, localIdentName, options);
1415
return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^([^a-zA-Z_])/, "_$1");

lib/processCss.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
121121

122122
var query = options.query;
123123
var root = query.root;
124+
var context = query.context;
124125
var localIdentName = query.localIdentName || "[hash:base64]";
125126
var localIdentRegExp = query.localIdentRegExp;
126127
var forceMinimize = query.minimize;
@@ -149,7 +150,9 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
149150
modulesScope({
150151
generateScopedName: function(exportName) {
151152
return getLocalIdent(options.loaderContext, localIdentName, exportName, {
152-
regExp: localIdentRegExp
153+
regExp: localIdentRegExp,
154+
hashPrefix: query.hashPrefix || "",
155+
context: context
153156
});
154157
}
155158
}),

test/camelCaseTest.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ describe("camelCase", function() {
66
var css = ".btn-info { color: blue; }";
77
var exports = {
88
with: [
9-
[1, "._38r5hlPyrqKLodJwWOdM1k { color: blue; }", ""]
9+
[1, ".Vh87YsUQA8A0EbntSqs6 { color: blue; }", ""]
1010
],
1111
without: [
12-
[1, "._38r5hlPyrqKLodJwWOdM1k { color: blue; }", ""]
12+
[1, ".Vh87YsUQA8A0EbntSqs6 { color: blue; }", ""]
1313
]
1414
};
15-
exports.with.locals = {'btn-info': '_38r5hlPyrqKLodJwWOdM1k'};
16-
exports.without.locals = {btnInfo: '_38r5hlPyrqKLodJwWOdM1k', 'btn-info': '_38r5hlPyrqKLodJwWOdM1k'};
15+
exports.with.locals = {'btn-info': 'Vh87YsUQA8A0EbntSqs6'};
16+
exports.without.locals = {btnInfo: 'Vh87YsUQA8A0EbntSqs6', 'btn-info': 'Vh87YsUQA8A0EbntSqs6'};
1717
test("with", css, exports.with, "?modules");
1818
test("without", css, exports.without, "?modules&camelCase");
1919
});

test/helpers.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function runLoader(loader, input, map, addOptions, callback) {
4545
loaderIndex: 0,
4646
context: "",
4747
resource: "test.css",
48+
resourcePath: "test.css",
4849
request: "css-loader!test.css",
4950
emitError: function(message) {
5051
throw new Error(message);
@@ -58,9 +59,9 @@ function runLoader(loader, input, map, addOptions, callback) {
5859

5960
exports.test = function test(name, input, result, query, modules) {
6061
it(name, function(done) {
61-
runLoader(cssLoader, input, undefined, {
62+
runLoader(cssLoader, input, undefined, !query || typeof query === "string" ? {
6263
query: query
63-
}, function(err, output) {
64+
} : query, function(err, output) {
6465
if(err) return done(err);
6566
assetEvaluated(output, result, modules);
6667
done();

test/localTest.js

+43-16
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ function testLocalMinimize(name, input, result, localsResult, query, modules) {
1515

1616
describe("local", function() {
1717
testLocal("locals-format", ":local(.test) { background: red; }", [
18-
[1, ".test-3tNsp { background: red; }", ""]
18+
[1, ".test-2_pBx { background: red; }", ""]
1919
], {
20-
test: "test-3tNsp"
20+
test: "test-2_pBx"
2121
}, "?localIdentName=[local]-[hash:base64:5]");
2222
testLocal("locals", ":local(.className) { background: red; }\n:local(#someId) { background: green; }\n" +
2323
":local(.className .subClass) { color: green; }\n:local(#someId .subClass) { color: blue; }", [
24-
[1, "._23_aKvs-b8bW2Vg3fwHozO { background: red; }\n#_1j3LM6lKkKzRIt19ImYVnD { background: green; }\n" +
25-
"._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 { color: green; }\n#_1j3LM6lKkKzRIt19ImYVnD ._13LGdX8RMStbBE9w-t0gZ1 { color: blue; }", ""]
24+
[1, "._23J0282swY7bwvI2X4fHiV { background: red; }\n#_3vpqN0v_IxlO3TzQjbpB33 { background: green; }\n" +
25+
"._23J0282swY7bwvI2X4fHiV ._1s1VsToXFz17cPAltMg7jz { color: green; }\n#_3vpqN0v_IxlO3TzQjbpB33 ._1s1VsToXFz17cPAltMg7jz { color: blue; }", ""]
2626
], {
27-
className: "_23_aKvs-b8bW2Vg3fwHozO",
28-
someId: "_1j3LM6lKkKzRIt19ImYVnD",
29-
subClass: "_13LGdX8RMStbBE9w-t0gZ1"
27+
className: "_23J0282swY7bwvI2X4fHiV",
28+
someId: "_3vpqN0v_IxlO3TzQjbpB33",
29+
subClass: "_1s1VsToXFz17cPAltMg7jz"
3030
});
3131
testLocalMinimize("minimized plus local", ":local(.localClass) { background: red; }\n:local .otherClass { background: red; }\n:local(.empty) { }", [
3232
[1, "._localClass,._otherClass{background:red}", ""]
@@ -146,17 +146,17 @@ describe("local", function() {
146146
});
147147
testLocal("module mode", ".className { background: url(./file.png); }\n#someId { background: url('module/file.jpg'); }\n" +
148148
".className .subClass { font-size: 5.5pt; }\n#someId .subClass { color: blue; }", [
149-
[1, "._23_aKvs-b8bW2Vg3fwHozO { background: url({./file.png}); }\n#_1j3LM6lKkKzRIt19ImYVnD { background: url({module/file.jpg}); }\n" +
150-
"._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 { font-size: 5.5pt; }\n#_1j3LM6lKkKzRIt19ImYVnD ._13LGdX8RMStbBE9w-t0gZ1 { color: blue; }", ""]
149+
[1, "._23J0282swY7bwvI2X4fHiV { background: url({./file.png}); }\n#_3vpqN0v_IxlO3TzQjbpB33 { background: url({module/file.jpg}); }\n" +
150+
"._23J0282swY7bwvI2X4fHiV ._1s1VsToXFz17cPAltMg7jz { font-size: 5.5pt; }\n#_3vpqN0v_IxlO3TzQjbpB33 ._1s1VsToXFz17cPAltMg7jz { color: blue; }", ""]
151151
], {
152-
className: "_23_aKvs-b8bW2Vg3fwHozO",
153-
someId: "_1j3LM6lKkKzRIt19ImYVnD",
154-
subClass: "_13LGdX8RMStbBE9w-t0gZ1"
152+
className: "_23J0282swY7bwvI2X4fHiV",
153+
someId: "_3vpqN0v_IxlO3TzQjbpB33",
154+
subClass: "_1s1VsToXFz17cPAltMg7jz"
155155
}, "?module");
156156
testLocal("class name parsing", ".-a0-34a___f { color: red; }", [
157-
[1, "._1YJOcrkc6cyZmBAAvyPFOn { color: red; }", ""]
157+
[1, "._3ZMCqVa1XidxdqbX65hZ5D { color: red; }", ""]
158158
], {
159-
"-a0-34a___f": "_1YJOcrkc6cyZmBAAvyPFOn"
159+
"-a0-34a___f": "_3ZMCqVa1XidxdqbX65hZ5D"
160160
}, "?module");
161161
testLocal("imported values in decl", ".className { color: IMPORTED_NAME; }\n" +
162162
":import(\"./vars.css\") { IMPORTED_NAME: primary-color; }", [
@@ -171,8 +171,35 @@ describe("local", function() {
171171
}
172172
});
173173
testLocal("issue-109", ".bar-1 { color: red; }", [
174-
[1, ".file--bar-1--2ESB7 { color: red; }", ""]
174+
[1, ".file--bar-1--2JvfJ { color: red; }", ""]
175175
], {
176-
"bar-1": "file--bar-1--2ESB7"
176+
"bar-1": "file--bar-1--2JvfJ"
177177
}, "?modules&importLoaders=1&localIdentName=[name]--[local]--[hash:base64:5]");
178+
testLocal("path naming", ".bar { color: red; }", [
179+
[1, ".path-to--file--bar { color: red; }", ""]
180+
], {
181+
"bar": "path-to--file--bar"
182+
}, {
183+
query: "?modules&localIdentName=[path]-[name]--[local]",
184+
resourcePath: "/root/path/to/file.css",
185+
options: {
186+
context: "/root/"
187+
}
188+
});
189+
testLocal("path naming with context", ".bar { color: red; }", [
190+
[1, ".to--file--bar { color: red; }", ""]
191+
], {
192+
"bar": "to--file--bar"
193+
}, {
194+
query: "?modules&localIdentName=[path]-[name]--[local]&context=/root/path",
195+
resourcePath: "/root/path/to/file.css",
196+
options: {
197+
context: "/root/"
198+
}
199+
});
200+
testLocal("hash prefix", ".bar { color: red; }", [
201+
[1, ".bar--58a3b08b9195a6af0de7431eaf3427c7 { color: red; }", ""]
202+
], {
203+
"bar": "bar--58a3b08b9195a6af0de7431eaf3427c7"
204+
}, "?modules&localIdentName=[local]--[hash]&hashPrefix=x");
178205
});

0 commit comments

Comments
 (0)