Sub topic 12
Bundler and framework support
This chapter assumes you know basic React, can read a .module.css file, and have used a bundler such as Vite, webpack or Next.js. CSS Modules are not a React feature. The build tool does the work, so your project needs a tool that understands .module.css files.
What the bundler does
When the bundler meets import styles from './Button.module.css', it runs the CSS through a CSS Modules processor. The processor renames each local class to a unique name. It also emits a JavaScript object that maps your original names to the generated ones. Your component gets that object as the default import.
Support by tool
Most modern tools handle the .module.css suffix with no setup.
| Tool | Setup needed | Notes |
|---|---|---|
| Vite | None | Uses the `.module.css` suffix. Options go under `css.modules` in `vite.config.js`. |
| Next.js | None | Supports `.module.css` in components. Plain global CSS is limited to specific files, such as the root layout or `_app`. |
| Create React App | None | Supported out of the box, but the project is no longer maintained. |
| webpack | Loader rules | Use `css-loader` with the `modules` option, plus `style-loader` or a CSS extraction plugin. |
Configuring Vite
The defaults work for most projects. You can change how generated names look, which helps when debugging in the browser.
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
css: {
modules: {
// Use camelCase keys so styles.primaryButton works for .primary-button
localsConvention: 'camelCase',
generateScopedName: '[name]__[local]__[hash:base64:5]',
},
},
});With this setting, a class .button in Button.module.css might become Button__button__x3k9a. The readable prefix makes it easy to find the source file in developer tools.
Configuring webpack
webpack does not know about CSS by default. You add loaders and tell css-loader to treat files with the right suffix as modules.
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
auto: true, // only *.module.css files become modules
localIdentName: '[name]__[local]--[hash:base64:5]',
},
},
},
],
},
],
},
};The auto: true option matters. It limits module behavior to files whose names match .module.css. Other CSS files, such as a reset stylesheet, stay global.
Preprocessors
Sass and Less work with the same naming rule. Name the file Button.module.scss and install the preprocessor (npm install -D sass). Vite and Next.js then compile it before applying CSS Modules. In webpack, add sass-loader after css-loader in the array, so it runs first.
Common errors
stylesisundefinedor an empty object. The file name does not end in.module.css, so the bundler treated it as global CSS. Rename it.- Class names look unchanged. The webpack
modulesoption is missing, orautodoes not match your file name. - A class is missing from
styles. The class name has a hyphen, such as.primary-button. Usestyles['primary-button'], or enablelocalsConvention. - Next.js reports that global CSS cannot be imported from a component. You imported a plain
.cssfile. Rename it to.module.cssor move the import to the root layout.
Exercise
You add import styles from './Card.module.css' in a webpack project, but styles.card is undefined and the page shows no styling. The CSS file contains .card { padding: 1rem; }. Name two settings you would check.
Show answer
First, check that the webpack rule uses css-loader with the modules option enabled, and that auto: true (or an equivalent test) matches the Card.module.css file name. Second, check that style-loader (or an extraction plugin) is present and listed before css-loader, so the processed CSS is applied to the page.