question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use Tailwindcss and css modules with Storybook

See original GitHub issue

Describe the bug I have a Gatsby project that uses Tailwindcss and css modules. I would like to somehow configure Storybook to use both. So far I’ve been able to use one or the other but not both.

Code snippets Here’s my webpack.config.js file for working css module:

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loaders: [
          require.resolve("style-loader"),
          {
            loader: require.resolve("css-loader"),
            options: {
              importLoaders: 1,
              modules: true,
              localIdentName: "[name]__[local]___[hash:base64:5]",
            },
          },
          require.resolve("sass-loader"),
        ],
      },
    ],
  },
}

Here’s my webpack.config.js file for working Tailwindcss:

const path = require("path")

module.exports = ({ config }) => {
  config.module.rules.push({
    test: /\.css$/,
    use: [
      // Loader for webpack to process CSS with PostCSS
      {
        loader: "postcss-loader",

        options: {
          importLoaders: 1,
          modules: true,
          localIdentName: "[name]__[local]___[hash:base64:5]",
          /*Enable Source Maps*/
          sourceMap: true,
          /*Set postcss.config.js config path && ctx*/
          config: {
            path: "./.storybook/",
          },
        },
      },
    ],

    include: path.resolve(__dirname, "../"),
  })

  // Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
  config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/]

  // use installed babel-loader which is v8.0-beta (which is meant to work with @babel/core@7)
  config.module.rules[0].use[0].loader = require.resolve("babel-loader")

  // use @babel/preset-react for JSX and env (instead of staged presets)
  config.module.rules[0].use[0].options.presets = [
    require.resolve("@babel/preset-react"),
    require.resolve("@babel/preset-env"),
  ]

  config.module.rules[0].use[0].options.plugins = [
    // use @babel/plugin-proposal-class-properties for class arrow functions
    require.resolve("@babel/plugin-proposal-class-properties"),
    // use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
    require.resolve("babel-plugin-remove-graphql-queries"),
  ]

  // Prefer Gatsby ES6 entrypoint (module) over commonjs (main) entrypoint
  config.resolve.mainFields = ["browser", "module", "main"]

  return config
}

System: System: OS: macOS Mojave 10.14.6 CPU: (8) x64 Intel® Core™ i7-7920HQ CPU @ 3.10GHz Binaries: Node: 10.15.3 - /usr/local/bin/node npm: 6.14.2 - /usr/local/bin/npm Browsers: Firefox: 67.0.4 Safari: 13.1 npmPackages: @storybook/addon-actions: ^5.3.18 => 5.3.18 @storybook/addon-backgrounds: ^5.3.18 => 5.3.18 @storybook/addon-knobs: ^5.3.18 => 5.3.18 @storybook/addon-links: ^5.3.18 => 5.3.18 @storybook/addons: ^5.3.18 => 5.3.18 @storybook/react: ^5.3.18 => 5.3.18 npmGlobalPackages: @storybook/cli: 5.3.18

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

5reactions
rbuteracommented, Apr 15, 2020

please check this gatsby-js tailwind starter:

https://github.com/rbutera/greater-gatsby

please remember to star if you find this useful!

2reactions
Hannah-goodridgecommented, May 21, 2020

UPDATE: For anyone reading, I would recommend taking a look at @rbuteras repo, it helped solve a few issues I was having by pushing a fix.

The main issue I was having was that in my package.json scripts I was passing:

"storybook": "NODE_ENV=production start-storybook -s public -p 6006",
"build-storybook": "NODE_ENV=production build-storybook -s public",

and once I remove the NODE_ENV=production from both scripts my css would then import into storybook.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storybook-tailwind. How should I add tailwind to storybook
Now I need to tell storybook to use tailwindcss for styling. But I have no idea how. Every other answer I saw tells...
Read more >
Integrate Tailwind CSS with Storybook
Tailwind CSS is a utility-first CSS framework packed with classes to build any design, directly in your markup. This recipe shows you how...
Read more >
How to set up Storybook with Next.js and Tailwind CSS
Complete configuration and setup for Storybook with Next.js and Tailwind CSS.
Read more >
How to Build a Fullstack Next.js App (with Storybook ...
Styling (Google has a simple design, we'll use Tailwind CSS to recreate it); Routing (we'll demonstrate two routes, the main "home" page and...
Read more >
a component library with Storybook, Tailwind, and Typescript.
Tailwind CSS is a highly customizable, low-level CSS framework. It's not a UI kit like many other frameworks, it gives you full control...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found