getCSSModuleLocalIdent.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const loaderUtils = require('loader-utils');
  9. const path = require('path');
  10. module.exports = function getLocalIdent(
  11. context,
  12. localIdentName,
  13. localName,
  14. options
  15. ) {
  16. // Use the filename or folder name, based on some uses the index.js / index.module.(css|scss|sass) project style
  17. const fileNameOrFolder = context.resourcePath.match(
  18. /index\.module\.(css|scss|sass)$/
  19. )
  20. ? '[folder]'
  21. : '[name]';
  22. // Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
  23. const hash = loaderUtils.getHashDigest(
  24. path.posix.relative(context.rootContext, context.resourcePath) + localName,
  25. 'md5',
  26. 'base64',
  27. 5
  28. );
  29. // Use loaderUtils to find the file or folder name
  30. const className = loaderUtils.interpolateName(
  31. context,
  32. fileNameOrFolder + '_' + localName + '__' + hash,
  33. options
  34. );
  35. // Remove the .module that appears in every classname when based on the file and replace all "." with "_".
  36. return className.replace('.module_', '_').replace(/\./g, '_');
  37. };