UNPKG

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