CSS Modules allows us to write CSS code in a modular way, and these styles can be imported and used in JavaScript files. Using CSS Modules can automatically generate unique class names, isolate styles between different modules, and avoid class name conflicts.
Modern.js supports CSS Modules by default, you don't need to add additional configuration. Our convention is to use the [name].module.css
filename to enable CSS Modules.
The following style files are considered CSS Modules:
*.module.scss
*.module.less
*.module.css
By default, only files ending in *.module.css
are treated CSS Modules.
If you want to treat all CSS files in the source directory as CSS Modules, you can enable the output.disableCssModuleExtension config, for example:
When set, the following two files are treated as CSS Modules:
We do not recommend enabling this config, because after enabling disableCssModuleExtension, CSS Modules files and ordinary CSS files cannot be clearly distinguished, which is not conducive to long-term maintenance.
By default, only files ending in *.module.css
are treated CSS Modules.
If you want to enable CSS Modules only for specified style files, you can configure output.cssModules, for example:
Customizing the class names generated by CSS Modules is also a commonly used function, you can use output.cssModuleLocalIdentName to configure it.
If you need to customize other configs of CSS Modules, you can set them via tools.cssLoader.
When you import CSS Modules in TypeScript code, TypeScript may prompt that the module is missing a type definition:
To fix this, you need to add a type declaration file for the CSS Modules, please create a src/global.d.ts
file, and add the corresponding type declaration:
If you enabled the disableCssModuleExtension
config, you also need to add the following types:
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where global.d.ts
is located, making sure the TypeScript can correctly identify the type definition.
Although the above method can provide the type of CSS Modules, it cannot accurately prompt which classNames are exported by a certain CSS file.
Modern.js supports generating accurate type declarations for CSS Modules, you only need to enable the output.enableCssModuleTSDeclaration config, and then execute the build, Modern.js will generate type declaration files for all CSS Modules.
For example, there are two files src/index.ts
and src/index.module.scss
under a certain folder:
After executing the build, the src/index.module.scss.d.ts
type declaration file will be automatically generated:
Then open the src/index.ts
file again, you will see that the styles
object already has a exact type.
In the above example, src/index.module.scss.d.ts
is generated by compilation, you can choose to commit them to the Git repository, or you can choose to ignore them in the .gitignore
file:
In addition, if the generated code causes ESLint to report errors, you can also add the above configuration to the .eslintignore
file.