or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-gatsby-plugin-react-css-modules

Gatsby plugin that transforms styleName to className using compile time CSS module resolution

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/gatsby-plugin-react-css-modules@5.15.x

To install, run

npx @tessl/cli install tessl/npm-gatsby-plugin-react-css-modules@5.15.0

index.mddocs/

Gatsby Plugin React CSS Modules

Gatsby 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.

Package Information

  • Package Name: gatsby-plugin-react-css-modules
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install gatsby-plugin-react-css-modules

Core Imports

This plugin is not imported directly. It integrates with Gatsby's plugin system.

Basic Usage

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 MyComponent

Architecture

This 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.

Capabilities

Gatsby Babel Configuration

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) {}

Plugin Configuration Options

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;
}

Dependencies

Runtime Dependencies:

  • @babel/runtime: ^7.20.13
  • babel-plugin-react-css-modules: ^3.4.2

Peer Dependencies:

  • gatsby: ^5.0.0-next

Requirements

  • CSS Module Files: Files must be named using the pattern filename.module.css for Gatsby to treat them as CSS modules
  • Node.js: >=18.0.0
  • Gatsby: ^5.0.0-next

Error Handling

The plugin integrates with Gatsby's build process and will cause build failures if:

  • babel-plugin-react-css-modules encounters invalid CSS module references
  • Required dependencies are missing
  • Invalid plugin configuration is provided

Build-time validation ensures that styleName references resolve to actual CSS classes in the corresponding CSS modules.