Skip to content

Commit 0141a5f

Browse files
committed
Merge pull request #16 from css-modules/hash
append a hash of the css source to scoped classnames
2 parents 9ecdd6c + 6b10363 commit 0141a5f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

index.js

Lines changed: 19 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 createScopedNameFunc (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,12 @@ module.exports = function (browserify, options) {
3144

3245
var plugin = require(require.resolve(name));
3346

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"description": "A browserify transform to load CSS Modules",
55
"main": "index.js",
66
"dependencies": {
7-
"css-modules-loader-core": "0.0.10",
7+
"css-modules-loader-core": "0.0.11",
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)