How to use the getHashDigest function from loader-utils

Find comprehensive JavaScript loader-utils.getHashDigest code examples handpicked from public code repositorys.

19
20
21
22
23
24
25
26
27
28
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,
fork icon48
star icon346
watch icon2

277
278
279
280
281
282
283
284
285
286
.then(html => {
  // Allow to use [templatehash] as placeholder for the html-webpack-plugin name
  // See also https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/
  // From https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214
  const finalOutputName = childCompilationOutputName.replace(/\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (_, hashType, digestType, maxLength) => {
    return loaderUtils.getHashDigest(Buffer.from(html, 'utf8'), hashType, digestType, parseInt(maxLength, 10));
  });
    // Add the evaluated html code to the webpack assets
  compilation.assets[finalOutputName] = {
    source: () => html,
fork icon1
star icon0
watch icon0

191
192
193
194
195
196
197
198
199
200
    }
    async.map(result, function (file, callabck) {
      var filePath = Path.normalize(Path.join(context, file))
      // loaderContext.addDependency(filePath)
      inputFileSystem.readFile(filePath, function (err, buffer) {
        callabck(err, [file, buffer, utils.getHashDigest(buffer)])
      });
    }, callback);
  });
};
fork icon0
star icon0
watch icon2

+ 11 other calls in file

171
172
173
174
175
176
177
178
179
180

translations = weblateToCounterpart(translations);

const json = JSON.stringify(translations, null, 4);
const jsonBuffer = Buffer.from(json);
const digest = loaderUtils.getHashDigest(jsonBuffer, null, null, 7);
const filename = `${lang}.${digest}.json`;

fs.writeFileSync(dest + filename, json);
if (verbose) {
fork icon0
star icon0
watch icon0

66
67
68
69
70
71
72
73
74
75
    }
}

parseName(name, content) {
    return name.replace(/\[(?:(\w+):)?hash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function () {
        return loaderUtils.getHashDigest(content, arguments[1], arguments[2], parseInt(arguments[3], 10));
    });
}

apply(compiler) {
fork icon0
star icon0
watch icon0

25
26
27
28
29
30
31
32
33
34
35


const idCache = {}


module.exports = function generateComponentId(source) {
  if (idCache[source]) return idCache[source]
  const hash = loaderUtils.getHashDigest(source, 'md5', 'base52', 5)
  idCache[source] = hash
  return hash
}
fork icon0
star icon0
watch icon0

2
3
4
5
6
7
8
9
10
11
12


function getCSSModuleLocalIdent(context, localIdentName, localName, options) {
  // Use the filename or folder name, based on some uses the index.js / index.module.(css|scss|sass) project style
  const fileNameOrFolder = context.resourcePath.match(/index\.module\.(css|scss|sass|styl)$/) ? '[folder]' : '[name]';
  // Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
  const hash = getHashDigest(posix.relative(context.rootContext, context.resourcePath) + localName, 'md5', 'base64', 5);
  // Use loaderUtils to find the file or folder name
  const className = interpolateName(context, fileNameOrFolder + '_' + localName + '__' + hash, options);
  // Remove the .module that appears in every classname when based on the file and replace all "." with "_".
  return className.replace('.module_', '_').replace(/\./g, '_');
fork icon0
star icon0
watch icon0