Skip to content

Commit 121efeb

Browse files
author
Alexandru Rosianu
committed
Adds [dir] and [sourceHash:<len>] in getLocalIdent
1 parent 3571037 commit 121efeb

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/getLocalIdent.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,41 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
var crypto = require('crypto');
6+
var path = require('path');
7+
58
var loaderUtils = require("loader-utils");
6-
module.exports = function getLocalIdent(loaderContext, localIdentName, localName, options) {
9+
10+
var DIR_REGEX = /dir(.*)dir/;
11+
var SOURCE_HASH_REGEX = /\[(sourceHash.*)\]/;
12+
13+
module.exports = function getLocalIdent(inputSource, loaderContext, localIdentName, localName, options) {
714
var request = loaderContext.options && typeof loaderContext.options.context === "string" ?
815
loaderUtils.stringifyRequest({ context: loaderContext.options.context }, loaderUtils.getRemainingRequest(loaderContext)) :
916
loaderContext.request;
1017
options.content = localName + " " + request;
1118
options.context = loaderContext.options && typeof loaderContext.options.context === "string" ? loaderContext.options.context : loaderContext.context;
1219
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
20+
21+
var sourceHashMatch = localIdentName.match(SOURCE_HASH_REGEX);
22+
if (sourceHashMatch && sourceHashMatch[1]) {
23+
var hashLen = sourceHashMatch[1].split(':')[1] || 32;
24+
var sourceHash = crypto.createHash("md5").update(localName + inputSource).digest("hex");
25+
localIdentName = localIdentName.replace(SOURCE_HASH_REGEX, sourceHash.substr(0, hashLen));
26+
}
27+
28+
var replaceWithDir = false;
29+
if (localIdentName.indexOf("[dir]") !== -1) {
30+
localIdentName = localIdentName.replace("[dir]", "dir[path]dir");
31+
replaceWithDir = true;
32+
}
33+
1334
var hash = loaderUtils.interpolateName(loaderContext, localIdentName, options);
35+
36+
if (replaceWithDir) {
37+
var dirName = path.basename(hash.match(DIR_REGEX)[1]);
38+
hash = hash.replace(DIR_REGEX, dirName);
39+
}
40+
1441
return hash.replace(/[^a-zA-Z0-9\-_]/g, "-").replace(/^([^a-zA-Z_])/, "_$1");
1542
};

lib/processCss.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ module.exports = function processCss(inputSource, inputMap, options) {
150150
}),
151151
modulesScope({
152152
generateScopedName: function(exportName) {
153-
return getLocalIdent(options.loaderContext, localIdentName, exportName, {
153+
return getLocalIdent(inputSource, options.loaderContext, localIdentName, exportName, {
154154
regExp: localIdentRegExp
155155
});
156156
}

0 commit comments

Comments
 (0)