Gatsby plugin that transforms styleName to className using compile time CSS module resolution
npx @tessl/cli install tessl/npm-gatsby-plugin-react-css-modules@5.15.0Gatsby plugin that transforms styleName to className using compile time CSS module resolution. This plugin provides a bridge between React applications and CSS Modules by enabling babel-plugin-react-css-modules in the Gatsby build process.
Note: You probably don't need this plugin! Gatsby works with CSS Modules by default. Use this only if you specifically want to enable babel-plugin-react-css-modules for your project.
npm install gatsby-plugin-react-css-modulesThis plugin is not imported directly. It integrates with Gatsby's plugin system.
Configure the plugin in your gatsby-config.js:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-react-css-modules`,
options: {
// *.css files are included by default.
// To support another syntax (e.g. SCSS),
// add `postcss-scss` to your project's devDependencies
// and add the following option here:
filetypes: {
".scss": { syntax: `postcss-scss` },
},
// Exclude global styles from the plugin using a RegExp:
exclude: `\/global\/`,
// For all the options check babel-plugin-react-css-modules README
},
},
],
}Then use styleName in your React components:
import React from "react"
// CSS Module file
import styles from "./component.module.css"
function MyComponent() {
return (
<div styleName="container">
<h1 styleName="title">Hello World</h1>
<p styleName="description">Using styleName instead of className</p>
</div>
)
}
export default MyComponentThis plugin works by integrating babel-plugin-react-css-modules into Gatsby's Babel configuration during the build process. The plugin transforms styleName attributes to className at compile time, providing CSS module resolution and validation.
Configures Gatsby's Babel setup to include babel-plugin-react-css-modules transformation.
/**
* Gatsby lifecycle hook that configures Babel to use babel-plugin-react-css-modules
* @param {Object} args - Gatsby lifecycle arguments
* @param {Object} args.actions - Gatsby actions object containing setBabelPlugin method
* @param {Object} pluginOptions - Plugin configuration options from gatsby-config.js
*/
exports.onCreateBabelConfig = function({ actions }, pluginOptions) {}The plugin accepts configuration options that are passed through to babel-plugin-react-css-modules:
interface PluginOptions {
/**
* File type configuration for different CSS syntaxes
* Example: { ".scss": { syntax: "postcss-scss" } }
*/
filetypes?: Record<string, { syntax: string }>;
/**
* RegExp pattern to exclude global styles from processing
* Example: /\/global\//
*/
exclude?: RegExp | string;
/**
* CSS class name generation pattern
* Default: "[name]--[local]--[hash:base64:5]"
*/
generateScopedName?: string;
/**
* Enable webpack hot module reloading
* Default: process.env.NODE_ENV !== "production"
*/
webpackHotModuleReloading?: boolean;
/**
* Additional babel-plugin-react-css-modules options
* See babel-plugin-react-css-modules documentation for all available options
*/
[key: string]: any;
}Runtime Dependencies:
@babel/runtime: ^7.20.13babel-plugin-react-css-modules: ^3.4.2Peer Dependencies:
gatsby: ^5.0.0-nextfilename.module.css for Gatsby to treat them as CSS modulesThe plugin integrates with Gatsby's build process and will cause build failures if:
Build-time validation ensures that styleName references resolve to actual CSS classes in the corresponding CSS modules.