|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
3 | 3 | Author Tobias Koppers @sokra
|
4 | 4 | */
|
| 5 | +var crypto = require('crypto'); |
| 6 | +var path = require('path'); |
| 7 | + |
5 | 8 | 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) { |
7 | 14 | var request = loaderContext.options && typeof loaderContext.options.context === "string" ?
|
8 | 15 | loaderUtils.stringifyRequest({ context: loaderContext.options.context }, loaderUtils.getRemainingRequest(loaderContext)) :
|
9 | 16 | loaderContext.request;
|
10 | 17 | options.content = localName + " " + request;
|
11 | 18 | options.context = loaderContext.options && typeof loaderContext.options.context === "string" ? loaderContext.options.context : loaderContext.context;
|
12 | 19 | 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 | + |
13 | 34 | 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 | + |
14 | 41 | return hash.replace(/[^a-zA-Z0-9\-_]/g, "-").replace(/^([^a-zA-Z_])/, "_$1");
|
15 | 42 | };
|
0 commit comments