This is a minimal reproduction repository for an issue with Rspack and css modules. When using native css modules support in rspack and changing the exportsConvention to camel-case, Rspack doesn't detect that camelCase referenced class names and removes them from the generated css bundle.
Rspack uses the LightningCssMinimizerRspackPlugin to minimize css.
LightningCssMinimizerRspackPlugin has a neat feature where it receives data from rspack about unused css classes for css_modules and removes those that aren't used by any JS files from the final css bundle.
So if you have a css module like this:
/* src/style.module.css */
.header-title {
color: red;
}
.header-subtitle {
color: blue;
}// src/index.mjs
import * as styles from "./style.module.css";
console.log(styles["header-title"]);The final css bundle will contain only the .header-title class, because .header-subtitle is not used.
However, if you change the exportLocalsConvention to camel-case in the rspack config then css classes are exported both in camelCase format and the original format:
// src/index.mjs
import * as styles from "./style.module.css";
console.log(styles.headerTitle);
// console.log(styles["header-title"]); this also worksHowever, in this case, the LightningCssMinimizerRspackPlugin doesn't detect that headerTitle is referencing .header-title class and think it's unused and removes the css class from the final css bundle.
- Clone this repository
- Run
pnpm install - Run
pnpm build - Open
dist/main.cssand see that it's empty. - Uncomment the line
console.log("Hello, World!", styles["header-title"]);insrc/index.js - Run
pnpm buildagain - Open
dist/main.cssand see that it now contains the.header-titleclass.