Skip to content

Commit 3579e31

Browse files
author
Jeremiah Clothier
authored
chore: make .module.css class names human readable in local development (#1744)
making css class names human readable in css module files that way it's easier to see where styles are coming from.
1 parent 3497240 commit 3579e31

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"files": [
6565
"config.js",
6666
"*.config.js",
67-
"main.js",
67+
"main.ts",
6868
"plopfile.js",
6969
"src/**/*.js"
7070
],

.storybook/main.js renamed to .storybook/main.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { StorybookConfig } from '@storybook/react-webpack5';
2+
import type { Configuration } from 'webpack';
3+
14
/**
25
* Although `--static-dir` is marked as deprecated. The The Chromatic CLI
36
* currently pulls the staticDir from the build script, but does not support
@@ -6,7 +9,7 @@
69
* We should refrain from using the staticDirs option in this configuration until
710
* https://github.com/chromaui/chromatic-cli/issues/462 is resolved.
811
*/
9-
module.exports = {
12+
const config: StorybookConfig = {
1013
stories: [
1114
'./components/**/*.stories.mdx',
1215
'./components/**/*.stories.@(js|jsx|ts|tsx)',
@@ -70,4 +73,47 @@ module.exports = {
7073
plugins: [],
7174
};
7275
},
76+
webpackFinal(config, { configType }) {
77+
if (configType === 'DEVELOPMENT') {
78+
updateCSSLoaderPlugin(config);
79+
}
80+
return config;
81+
},
7382
};
83+
84+
/**
85+
* Updates the `css-loader` webpack plugin to make class names human readable.
86+
*
87+
* NOTE: This should only be used for local development.
88+
*/
89+
function updateCSSLoaderPlugin(config: Configuration): Configuration {
90+
config.module?.rules?.forEach((rule) => {
91+
if (rule && typeof rule === 'object' && Array.isArray(rule.use)) {
92+
const isRuleForCSS = rule.test?.toString() === '/\\.css$/';
93+
if (isRuleForCSS) {
94+
rule.use.forEach((ruleSetRule) => {
95+
if (
96+
typeof ruleSetRule === 'object' &&
97+
ruleSetRule?.loader?.includes('node_modules/css-loader')
98+
) {
99+
ruleSetRule.options = {
100+
// @ts-expect-error css-loader doesn't accept "string" options
101+
// and will either be an object or undefined
102+
...ruleSetRule.options,
103+
modules: {
104+
// @ts-expect-error css-loader doesn't accept "string" options
105+
// and will either be an object or undefined
106+
...ruleSetRule.options?.modules,
107+
localIdentName: '[name]__[local]--[hash:base64:5]',
108+
},
109+
};
110+
}
111+
});
112+
}
113+
}
114+
});
115+
116+
return config;
117+
}
118+
119+
export default config;

0 commit comments

Comments
 (0)