Use CSS Modules

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.

Rsbuild 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

Example

  • Write style:
/* button.module.css */
.error {
  background: red;
}
  • Using styles:
// Button.tsx
import React, { Component } from 'react';
// import style file
import styles from './button.module.css';

export default () => {
  return <button className={styles.error}>Error Button</button>;
};

CSS Modules Recognition Rules

By default, only files ending with *.module.css are recognized as CSS Modules.

If you want to treat other CSS files as CSS Modules as well, you can achieve this by configuring output.cssModules.auto.

For example:

export default {
  output: {
    cssModules: {
      auto: (resource) => {
        return resource.includes('.module.') || resource.includes('shared/');
      },
    },
  },
};

After this configuration, the following two files will be recognized as CSS Modules:

import styles1 from './foo.module.css';
import styles2 from './shared/bar.css';

Custom Class Names

Customizing the class names generated by CSS Modules is also a commonly used function, you can use output.cssModules.localIdentName to configure it.

export default {
  output: {
    cssModules: {
      localIdentName: '[hash:base64:4]',
    },
  },
};

If you need to customize other configs of CSS Modules, you can set them via output.cssModules.

Type Declaration

When you import CSS Modules in TypeScript code, TypeScript may prompt that the module is missing a type definition:

TS2307: Cannot find module './index.module.css' or its corresponding type declarations.

To fix this, you need to add a type declaration file for the CSS Modules, please create a src/env.d.ts file, and add the corresponding type declaration.

  • Method 1: If the @rsbuild/core package is installed, you can directly reference the type declarations provided by @rsbuild/core:
/// <reference types="@rsbuild/core/types" />
  • Method 2: Manually add the required type declarations:
src/env.d.ts
declare module '*.module.css' {
  const classes: { readonly [key: string]: string };
  export default classes;
}
declare module '*.module.scss' {
  const classes: { readonly [key: string]: string };
  export default classes;
}
declare module '*.module.sass' {
  const classes: { readonly [key: string]: string };
  export default classes;
}
declare module '*.module.less' {
  const classes: { readonly [key: string]: string };
  export default classes;
}
declare module '*.module.styl' {
  const classes: { readonly [key: string]: string };
  export default classes;
}
declare module '*.module.stylus' {
  const classes: { readonly [key: string]: string };
  export default classes;
}

After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where env.d.ts is located, making sure the TypeScript can correctly identify the type definition.

Example

For example, there are two files src/index.ts and src/index.module.scss under a certain folder:

src/index.ts
import styles from './index.module.scss';

export default () => {
  <div>
    <div className={styles.pageHeader}>Page Header</div>
  </div>;
};
src/index.module.scss
.page-header {
  color: black;
}

After executing the build, the src/index.module.scss.d.ts type declaration file will be automatically generated:

src/index.module.scss.d.ts
// This file is automatically generated.
// Please do not change this file!
interface CssExports {
  'page-header': string;
  pageHeader: string;
}
export const cssExports: CssExports;
export default cssExports;

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:

# Ignore auto generated CSS declarations
*.module.css.d.ts
*.module.sass.d.ts
*.module.scss.d.ts
*.module.less.d.ts
*.module.styl.d.ts
*.module.stylus.d.ts

In addition, if the generated code causes ESLint to report errors, you can also add the above configuration to the .eslintignore file.