Skip to content

Commit d66534c

Browse files
feat: localIdentHashFunction, localIdentHashDigest, localIdentHashDigestLength options added
1 parent ec87b40 commit d66534c

7 files changed

+550
-24
lines changed

README.md

+88-3
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ Allows to configure the generated local ident name.
762762
For more information on options see:
763763

764764
- [webpack template strings](https://webpack.js.org/configuration/output/#template-strings),
765-
- [output.hashdigest](https://webpack.js.org/configuration/output/#outputhashdigest),
765+
- [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest),
766766
- [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength),
767767
- [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction),
768768
- [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
@@ -773,7 +773,7 @@ Supported template strings:
773773
- [path] the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
774774
- [file] - filename and path.
775775
- [ext] - extension with leading .
776-
- [hash] - the hash of the string, generated based on `hashPrefix`, `localIdentContext`, `resourcePath` and `exportName`
776+
- [hash] - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
777777
- [<hashFunction>:hash:<hashDigest>:<hashDigestLength>] - hash with hash settings.
778778
- [local] - original class.
779779

@@ -833,12 +833,13 @@ module.exports = {
833833
};
834834
```
835835

836-
##### `localIdentHashSalt
836+
##### `localIdentHashSalt`
837837

838838
Type: `String`
839839
Default: `undefined`
840840

841841
Allows to add custom hash to generate more unique classes.
842+
For more information see [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
842843

843844
**webpack.config.js**
844845

@@ -860,6 +861,90 @@ module.exports = {
860861
};
861862
```
862863

864+
##### `localIdentHashFunction`
865+
866+
Type: `String`
867+
Default: `md4`
868+
869+
Allows to specify hash function to generate classes .
870+
For more information see [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction).
871+
872+
**webpack.config.js**
873+
874+
```js
875+
module.exports = {
876+
module: {
877+
rules: [
878+
{
879+
test: /\.css$/i,
880+
loader: "css-loader",
881+
options: {
882+
modules: {
883+
localIdentHashFunction: "md4",
884+
},
885+
},
886+
},
887+
],
888+
},
889+
};
890+
```
891+
892+
##### `localIdentHashDigest`
893+
894+
Type: `String`
895+
Default: `hex`
896+
897+
Allows to specify hash digest to generate classes.
898+
For more information see [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest).
899+
900+
**webpack.config.js**
901+
902+
```js
903+
module.exports = {
904+
module: {
905+
rules: [
906+
{
907+
test: /\.css$/i,
908+
loader: "css-loader",
909+
options: {
910+
modules: {
911+
localIdentHashDigest: "base64",
912+
},
913+
},
914+
},
915+
],
916+
},
917+
};
918+
```
919+
920+
##### `localIdentHashDigestLength`
921+
922+
Type: `Number`
923+
Default: `20`
924+
925+
Allows to specify hash digest length to generate classes.
926+
For more information see [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength).
927+
928+
**webpack.config.js**
929+
930+
```js
931+
module.exports = {
932+
module: {
933+
rules: [
934+
{
935+
test: /\.css$/i,
936+
loader: "css-loader",
937+
options: {
938+
modules: {
939+
localIdentHashDigestLength: 5,
940+
},
941+
},
942+
},
943+
],
944+
},
945+
};
946+
```
947+
863948
##### `localIdentRegExp`
864949

865950
Type: `String|RegExp`

src/options.json

+14
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@
103103
"type": "string",
104104
"minLength": 1
105105
},
106+
"localIdentHashFunction": {
107+
"description": "Allows to specify hash function to generate classes (https://github.com/webpack-contrib/css-loader#localidenthashfunction).",
108+
"type": "string",
109+
"minLength": 1
110+
},
111+
"localIdentHashDigest": {
112+
"description": "Allows to specify hash digest to generate classes (https://github.com/webpack-contrib/css-loader#localidenthashdigest).",
113+
"type": "string",
114+
"minLength": 1
115+
},
116+
"localIdentHashDigestLength": {
117+
"description": "Allows to specify hash digest length to generate classes (https://github.com/webpack-contrib/css-loader#localidenthashdigestlength).",
118+
"type": "number"
119+
},
106120
"localIdentRegExp": {
107121
"description": "Allows to specify custom RegExp for local ident name (https://github.com/webpack-contrib/css-loader#localidentregexp).",
108122
"anyOf": [

src/utils.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,7 @@ function defaultGetLocalIdent(
358358
// eslint-disable-next-line no-param-reassign
359359
options.content = `${relativeMatchResource}${relativeResourcePath}\x00${localName}`;
360360

361-
// eslint-disable-next-line no-underscore-dangle
362-
const { outputOptions } = loaderContext._compilation;
363-
const { hashSalt = options.hashSalt } = outputOptions;
364-
let { hashFunction, hashDigest, hashDigestLength } = outputOptions;
365-
361+
let { hashFunction, hashDigest, hashDigestLength } = options;
366362
const mathes = localIdentName.match(
367363
/\[(?:([^:\]]+):)?(?:(hash|contenthash|fullhash))(?::([a-z]+\d*))?(?::(\d+))?\]/i
368364
);
@@ -386,7 +382,7 @@ function defaultGetLocalIdent(
386382

387383
// eslint-disable-next-line no-underscore-dangle
388384
const hash = loaderContext._compiler.webpack.util.createHash(hashFunction);
389-
// eslint-disable-next-line no-underscore-dangle
385+
const { hashSalt } = options;
390386

391387
if (hashSalt) {
392388
hash.update(hashSalt);
@@ -523,13 +519,19 @@ function getModulesOptions(rawOptions, loaderContext) {
523519
return false;
524520
}
525521

522+
// eslint-disable-next-line no-underscore-dangle
523+
const { outputOptions } = loaderContext._compilation;
524+
526525
let modulesOptions = {
527526
auto: true,
528527
mode: isIcss ? "icss" : "local",
529528
exportGlobals: false,
530529
localIdentName: "[hash:base64]",
531530
localIdentContext: loaderContext.rootContext,
532-
localIdentHashSalt: "",
531+
localIdentHashSalt: outputOptions.hashSalt,
532+
localIdentHashFunction: outputOptions.hashFunction,
533+
localIdentHashDigest: outputOptions.hashDigest,
534+
localIdentHashDigestLength: outputOptions.hashDigestLength,
533535
// eslint-disable-next-line no-undefined
534536
localIdentRegExp: undefined,
535537
// eslint-disable-next-line no-undefined
@@ -674,6 +676,9 @@ function getModulesPlugins(options, loaderContext) {
674676
localIdentName,
675677
localIdentContext,
676678
localIdentHashSalt,
679+
localIdentHashFunction,
680+
localIdentHashDigest,
681+
localIdentHashDigestLength,
677682
localIdentRegExp,
678683
} = options.modules;
679684

@@ -696,6 +701,9 @@ function getModulesPlugins(options, loaderContext) {
696701
{
697702
context: localIdentContext,
698703
hashSalt: localIdentHashSalt,
704+
hashFunction: localIdentHashFunction,
705+
hashDigest: localIdentHashDigest,
706+
hashDigestLength: localIdentHashDigestLength,
699707
regExp: localIdentRegExp,
700708
}
701709
);
@@ -711,6 +719,9 @@ function getModulesPlugins(options, loaderContext) {
711719
{
712720
context: localIdentContext,
713721
hashSalt: localIdentHashSalt,
722+
hashFunction: localIdentHashFunction,
723+
hashDigest: localIdentHashDigest,
724+
hashDigestLength: localIdentHashDigestLength,
714725
regExp: localIdentRegExp,
715726
}
716727
);

0 commit comments

Comments
 (0)