From 121efebe2a75680d3a60fb678ce03755f2604a0b Mon Sep 17 00:00:00 2001 From: Alexandru Rosianu Date: Wed, 22 Jul 2015 18:11:39 +0300 Subject: [PATCH] Adds [dir] and [sourceHash:] in getLocalIdent --- lib/getLocalIdent.js | 29 ++++++++++++++++++++++++++++- lib/processCss.js | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/getLocalIdent.js b/lib/getLocalIdent.js index 7321669d..b441ddff 100644 --- a/lib/getLocalIdent.js +++ b/lib/getLocalIdent.js @@ -2,14 +2,41 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ +var crypto = require('crypto'); +var path = require('path'); + var loaderUtils = require("loader-utils"); -module.exports = function getLocalIdent(loaderContext, localIdentName, localName, options) { + +var DIR_REGEX = /dir(.*)dir/; +var SOURCE_HASH_REGEX = /\[(sourceHash.*)\]/; + +module.exports = function getLocalIdent(inputSource, loaderContext, localIdentName, localName, options) { var request = loaderContext.options && typeof loaderContext.options.context === "string" ? loaderUtils.stringifyRequest({ context: loaderContext.options.context }, loaderUtils.getRemainingRequest(loaderContext)) : loaderContext.request; options.content = localName + " " + request; options.context = loaderContext.options && typeof loaderContext.options.context === "string" ? loaderContext.options.context : loaderContext.context; localIdentName = localIdentName.replace(/\[local\]/gi, localName); + + var sourceHashMatch = localIdentName.match(SOURCE_HASH_REGEX); + if (sourceHashMatch && sourceHashMatch[1]) { + var hashLen = sourceHashMatch[1].split(':')[1] || 32; + var sourceHash = crypto.createHash("md5").update(localName + inputSource).digest("hex"); + localIdentName = localIdentName.replace(SOURCE_HASH_REGEX, sourceHash.substr(0, hashLen)); + } + + var replaceWithDir = false; + if (localIdentName.indexOf("[dir]") !== -1) { + localIdentName = localIdentName.replace("[dir]", "dir[path]dir"); + replaceWithDir = true; + } + var hash = loaderUtils.interpolateName(loaderContext, localIdentName, options); + + if (replaceWithDir) { + var dirName = path.basename(hash.match(DIR_REGEX)[1]); + hash = hash.replace(DIR_REGEX, dirName); + } + return hash.replace(/[^a-zA-Z0-9\-_]/g, "-").replace(/^([^a-zA-Z_])/, "_$1"); }; diff --git a/lib/processCss.js b/lib/processCss.js index e7c8a04b..3ecfb6cf 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -150,7 +150,7 @@ module.exports = function processCss(inputSource, inputMap, options) { }), modulesScope({ generateScopedName: function(exportName) { - return getLocalIdent(options.loaderContext, localIdentName, exportName, { + return getLocalIdent(inputSource, options.loaderContext, localIdentName, exportName, { regExp: localIdentRegExp }); }