You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getCSSModuleLocalIdent.js 1.1 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839
  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. module.exports = function getLocalIdent(
  10. context,
  11. localIdentName,
  12. localName,
  13. options
  14. ) {
  15. // Use the filename or folder name, based on some uses the index.js / index.module.(css|scss|sass) project style
  16. const fileNameOrFolder = context.resourcePath.match(
  17. /index\.module\.(css|scss|sass)$/
  18. )
  19. ? '[folder]'
  20. : '[name]';
  21. // Create a hash based on a the file location and class name. Will be unique across a project, and close to globally unique.
  22. const hash = loaderUtils.getHashDigest(
  23. context.resourcePath + localName,
  24. 'md5',
  25. 'base64',
  26. 5
  27. );
  28. // Use loaderUtils to find the file or folder name
  29. const className = loaderUtils.interpolateName(
  30. context,
  31. fileNameOrFolder + '_' + localName + '__' + hash,
  32. options
  33. );
  34. // remove the .module that appears in every classname when based on the file.
  35. return className.replace('.module_', '_');
  36. };