Skip to content

Commit 03fe610

Browse files
Merge pull request #78 from pascalduez/features/hashprefix-option
Add generic-names `hashPrefix` option
2 parents 22624ed + d2469ca commit 03fe610

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Provides the full list of PostCSS plugins to the pipeline. Providing this cancel
203203

204204
Short alias for the [postcss-modules-extract-imports](https://github.com/css-modules/postcss-modules-extract-imports) plugin's `createImportedName` option.
205205

206-
### `generateScopedName` function
206+
### `generateScopedName` string|function
207207

208208
Short alias for the [postcss-modules-scope](https://github.com/css-modules/postcss-modules-scope) plugin's option. Helps you to specify the custom way to build generic names for the class selectors.
209209
You may also use a string pattern similar to the webpack's [css-loader](https://github.com/webpack/css-loader#local-scope).
@@ -230,6 +230,11 @@ hook({
230230
});
231231
```
232232

233+
### `hashPrefix` string
234+
235+
Short alias for the [generic-names](https://github.com/css-modules/generic-names) helper option.
236+
Provides additional hash uniqueness. Might be useful for projects with several stylesheets sharing a same name.
237+
233238
### `mode` string
234239

235240
Short alias for the [postcss-modules-local-by-default](https://github.com/css-modules/postcss-modules-local-by-default) plugin's option.

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function setupHook({
3434
prepend = [],
3535
createImportedName,
3636
generateScopedName,
37+
hashPrefix,
3738
mode,
3839
use,
3940
rootDir: context = process.cwd(),
@@ -51,7 +52,7 @@ module.exports = function setupHook({
5152
let scopedName;
5253
if (generateScopedName) {
5354
scopedName = typeof generateScopedName !== 'function'
54-
? genericNames(generateScopedName, {context}) // for example '[name]__[local]___[hash:base64:5]'
55+
? genericNames(generateScopedName, {context, hashPrefix}) // for example '[name]__[local]___[hash:base64:5]'
5556
: generateScopedName;
5657
} else {
5758
// small fallback

lib/validate.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const rules = {
1717
use: 'array',
1818
createImportedName: 'function',
1919
generateScopedName: 'function|string',
20+
hashPrefix: 'string',
2021
mode: 'string',
2122
rootDir: 'string',
2223
};

test/api/hashPrefix.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const detachHook = require('../sugar').detachHook;
2+
const dropCache = require('../sugar').dropCache;
3+
4+
suite('api/hashPrefix', () => {
5+
let samples = [];
6+
7+
suite('using string pattern and hashPrefix', () => {
8+
let tokens;
9+
10+
test('should return tokens with prefixed id', () => assert.deepEqual(tokens, {
11+
color: 'oceanic__color___3xlBZ',
12+
}));
13+
14+
setup(() => {
15+
hook({generateScopedName: '[name]__[local]___[hash:base64:5]', hashPrefix: 'test'});
16+
tokens = require('./fixture/oceanic.css');
17+
samples.push(tokens);
18+
});
19+
});
20+
21+
suite('using string pattern', () => {
22+
let tokens;
23+
24+
test('should return tokens with different hashes', () => assert.notDeepEqual(
25+
samples
26+
));
27+
28+
setup(() => {
29+
hook({generateScopedName: '[name]__[local]___[hash:base64:5]'});
30+
tokens = require('./fixture/oceanic.css');
31+
samples.push(tokens);
32+
});
33+
});
34+
35+
teardown(() => {
36+
detachHook('.css');
37+
dropCache('./api/fixture/oceanic.css');
38+
});
39+
});

0 commit comments

Comments
 (0)