Doolan
Doolan

Reputation: 1641

Can't get css modules to work within Typescript Storybook project

This is the current config for ./storybook/main.js and I can't seem to get any css modules working inside of my storybook components. This is a Typescript project so not entirely sure if I need anything additional to make the modules work properly.

const path = require("path");
const tailwindcss = require("tailwindcss");
const autoprefixer = require("autoprefixer");

module.exports = {
    stories: ["../stories/**/**/*.stories.tsx"],
    addons: [
        "@builder.io/storybook",
        "@storybook/addon-knobs",
        "@storybook/addon-knobs/register",
    ],
    webpackFinal: async (config) => {
        config.module.rules.push(
            {
                test: /\.(scss|sass|css)$/,
                use: [
                    {
                        loader: "sass-loader",
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                        },
                      },
                    {
                        loader: "postcss-loader",
                        options: {
                            ident: "postcss",
                            plugins: [tailwindcss, autoprefixer],
                        },
                    },
                ],
                include: path.resolve(__dirname, "../"),
            },
            {
                test: /\.(png|jpe?g|gif|jp2|webp)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            name: "assets/images/[name].[ext]",
                        },
                    },
                ],
            }
        );
        return config;
    },
};

MyComponent.jsx

import React from "react";
import styles from "./styles.css";

const MyComponent = () => {
  console.log(styles);
  return <h1>hi</h1>;
};

Logging styles returns an empty object. Am I missing something here?

Upvotes: 3

Views: 1534

Answers (1)

Doolan
Doolan

Reputation: 1641

Looks like I needed to add additional loaders to the storybook addons

{
  name: '@storybook/preset-scss',
  options: {
    cssLoaderOptions: {
      modules: true
    }
  }
},

Upvotes: 2

Related Questions