Skip to content

Commit

Permalink
chore: make .module.css class names human readable in local develop…
Browse files Browse the repository at this point in the history
…ment (#1744)

making css class names human readable in css module files
that way it's easier to see where styles are coming from.
  • Loading branch information
jeremiah-clothier authored Sep 7, 2023
1 parent 3497240 commit 3579e31
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"files": [
"config.js",
"*.config.js",
"main.js",
"main.ts",
"plopfile.js",
"src/**/*.js"
],
Expand Down
48 changes: 47 additions & 1 deletion .storybook/main.js → .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { StorybookConfig } from '@storybook/react-webpack5';
import type { Configuration } from 'webpack';

/**
* Although `--static-dir` is marked as deprecated. The The Chromatic CLI
* currently pulls the staticDir from the build script, but does not support
Expand All @@ -6,7 +9,7 @@
* We should refrain from using the staticDirs option in this configuration until
* https://github.com/chromaui/chromatic-cli/issues/462 is resolved.
*/
module.exports = {
const config: StorybookConfig = {
stories: [
'./components/**/*.stories.mdx',
'./components/**/*.stories.@(js|jsx|ts|tsx)',
Expand Down Expand Up @@ -70,4 +73,47 @@ module.exports = {
plugins: [],
};
},
webpackFinal(config, { configType }) {
if (configType === 'DEVELOPMENT') {
updateCSSLoaderPlugin(config);
}
return config;
},
};

/**
* Updates the `css-loader` webpack plugin to make class names human readable.
*
* NOTE: This should only be used for local development.
*/
function updateCSSLoaderPlugin(config: Configuration): Configuration {
config.module?.rules?.forEach((rule) => {
if (rule && typeof rule === 'object' && Array.isArray(rule.use)) {
const isRuleForCSS = rule.test?.toString() === '/\\.css$/';
if (isRuleForCSS) {
rule.use.forEach((ruleSetRule) => {
if (
typeof ruleSetRule === 'object' &&
ruleSetRule?.loader?.includes('node_modules/css-loader')
) {
ruleSetRule.options = {
// @ts-expect-error css-loader doesn't accept "string" options
// and will either be an object or undefined
...ruleSetRule.options,
modules: {
// @ts-expect-error css-loader doesn't accept "string" options
// and will either be an object or undefined
...ruleSetRule.options?.modules,
localIdentName: '[name]__[local]--[hash:base64:5]',
},
};
}
});
}
}
});

return config;
}

export default config;

0 comments on commit 3579e31

Please sign in to comment.