3

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?

1 Answer 1

2

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

{
  name: '@storybook/preset-scss',
  options: {
    cssLoaderOptions: {
      modules: true
    }
  }
},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.