Skip to content

Commit

Permalink
feat(*): use custom css-modules naming
Browse files Browse the repository at this point in the history
  • Loading branch information
reme3d2y committed Mar 31, 2020
1 parent ebbc4bd commit 7eacdab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .storybook/utils/getCSSModuleLocalIdent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Fork of https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/getCSSModuleLocalIdent.js

const loaderUtils = require('loader-utils');
const path = require('path');

module.exports = function getCSSModuleLocalIdent(context, localIdentName, localName, options) {
const matches = context.resourcePath.match(
/src\/(.*)\/src\/(index|Component)\.module\.(css|scss|sass)$/,
);

// Use the filename or component folder name, based on some uses the index.js / index.module.(css|scss|sass) project style
// We store sources in /src folder, so [folder] === 'src'. For this reason, we should get component name from regexp.
const fileNameOrFolder = matches ? matches[1] : '[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 = loaderUtils.getHashDigest(
path.posix.relative(context.rootContext, context.resourcePath) + localName,
'md5',
'base64',
5,
);
// Use loaderUtils to find the file or folder name
const className = loaderUtils.interpolateName(
context,
fileNameOrFolder + '_' + localName + '__' + hash,
options,
);

// remove the .module that appears in every classname when based on the file.
return className.replace('.module_', '_');
};
3 changes: 2 additions & 1 deletion .storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const getCSSModuleLocalIdent = require('./utils/getCSSModuleLocalIdent');

/**
* Добавляет генерацию интерфейсов в зависимости от флага RDTL.
Expand Down Expand Up @@ -69,7 +70,7 @@ module.exports = ({ config }) => ({
loader: 'css-loader',
options: {
modules: {
localIdentName: '[local]_[hash:base64:5]',
getLocalIdent: getCSSModuleLocalIdent,
},
localsConvention: 'dashes',
importLoaders: 1,
Expand Down

0 comments on commit 7eacdab

Please sign in to comment.