Skip to content

Commit 84b4321

Browse files
committed
append a hash of the css source to scoped classnames
1 parent 066a603 commit 84b4321

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ var through = require('through');
44
var Core = require('css-modules-loader-core');
55
var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader');
66
var assign = require('object-assign');
7+
var stringHash = require('string-hash');
8+
9+
/*
10+
Custom `generateScopedName` function for `postcss-modules-scope`.
11+
Appends a hash of the css source.
12+
*/
13+
function createNameFunc (plugin) {
14+
var orig = plugin.generateScopedName;
15+
return function (name, path, css) {
16+
var hash = stringHash(css).toString(36).substr(0, 5);
17+
return orig.apply(plugin, arguments) + '___' + hash;
18+
}
19+
};
720

821
var cssExt = /\.css$/;
922
module.exports = function (browserify, options) {
@@ -31,6 +44,11 @@ module.exports = function (browserify, options) {
3144

3245
var plugin = require(name);
3346

47+
// custom scoped name generation
48+
if (name === 'postcss-modules-scope') {
49+
options[name].generateScopedName = createScopedNameFunc(plugin);
50+
}
51+
3452
if (name in options) {
3553
plugin = plugin(options[name]);
3654
} else {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"css-modules-loader-core": "0.0.10",
88
"object-assign": "^3.0.0",
9+
"string-hash": "^1.1.0",
910
"through": "^2.3.7"
1011
},
1112
"devDependencies": {},

0 commit comments

Comments
 (0)