-
Notifications
You must be signed in to change notification settings - Fork 49
/
getCssModuleLocalIdent.js
65 lines (60 loc) · 2.15 KB
/
getCssModuleLocalIdent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// file fork by https://github.com/vercel/next.js/blob/283af4e69bd86e108ae712cbf6921e3f143b40f1/packages/next/build/webpack/config/blocks/css/loaders/getCssModuleLocalIdent.ts
const loaderUtils = require('loader-utils');
const path = require('path');
// ### Default ClassName
//
// | MODE | className | e.g. |
// | --------- |----------------------------|-----------------------|
// | DEV | `[local]--[hash:base64:4]` | `comp-wrapper--2Rra ` |
// | PROD | `[hash:base64:8]` | `2Rra8Ryx` |
function getCssModuleLocalIdentForNextJs(
context,
_,
exportName,
options,
__DEV__,
localIdentNameFollowDev,
) {
const relativePath = path
.relative(context.rootContext, context.resourcePath)
.replace(/\\+/g, '/');
// Generate a hash to make the class name unique.
const hash = loaderUtils.getHashDigest(
Buffer.from(`filePath:${relativePath}#className:${exportName}`),
'md5',
'base64',
__DEV__ || localIdentNameFollowDev ? 4 : 8,
);
// Have webpack interpolate the `[folder]` or `[name]` to its real value.
return (
loaderUtils
.interpolateName(
context,
// RAW name, e.g. style_nav-liist___up0
// fileNameOrFolder + '_' + exportName + '__' + hash,
//
// I remove fileNameOrFolder in here
// like `css-loader` default localIdentName
//
__DEV__ || localIdentNameFollowDev ? `${exportName}--${hash}` : hash,
options,
)
.replace(
// Webpack name interpolation returns `about.module_root__2oFM9` for
// `.root {}` inside a file named `about.module.css`. Let's simplify
// this.
/\.module_/,
'_',
)
// Replace invalid symbols with underscores instead of escaping
// https://mathiasbynens.be/notes/css-escapes#identifiers-strings
.replace(/[^a-zA-Z0-9-_]/g, '_')
// "they cannot start with a digit, two hyphens, or a hyphen followed by a digit [sic]"
// https://www.w3.org/TR/CSS21/syndata.html#characters
.replace(/^(\d|--|-\d)/, '__$1')
);
}
module.exports = {
getCssModuleLocalIdentForNextJs,
loaderUtils,
};