How to configure tailwind css for use with storybook
See original GitHub issueBug or support request summary
I am trying to configure tailwind css with storybook, but since it requires some webpack configuration, it’s not as straightforward as other configs. At this point, I am not even sure if it’s possible currently.
Steps to reproduce
So far, I have tried to use custom webpack config (referring storybook and tailwind docs):
const path = require("path");
const tailwindcss = require("tailwindcss");
// Export a function. Accept the base config as the only param.
module.exports = (storybookBaseConfig, configType) => {
// configType has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.
// Make whatever fine-grained changes you need
storybookBaseConfig.module.rules.push({
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "sass-loader", "postcss-loader"],
include: path.resolve(__dirname, "../")
});
storybookBaseConfig.plugins = [
tailwindcss("./tailwind.js"),
require("autoprefixer")
];
// Return the altered config
return storybookBaseConfig;
};
But that gives an error:
info => Loading custom webpack config (full-control mode).
/Users/anishkumar/Documents/react/ez2/node_modules/tapable/lib/Tapable.js:375
arguments[i].apply(this);
^
TypeError: arguments[i].apply is not a function
at Compiler.apply (/Users/anishkumar/Documents/react/ez2/node_modules/tapable/lib/Tapable.js:375:16)
at webpack (/Users/anishkumar/Documents/react/ez2/node_modules/@storybook/core/node_modules/webpack/lib/webpack.js:33:19)
at exports.default (/Users/anishkumar/Documents/react/ez2/node_modules/@storybook/core/dist/server/middleware.js:29:40)
at buildDev (/Users/anishkumar/Documents/react/ez2/node_modules/@storybook/core/dist/server/build-dev.js:163:36)
at Object.<anonymous> (/Users/anishkumar/Documents/react/ez2/node_modules/@storybook/react/dist/server/index.js:23:22)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/anishkumar/Documents/react/ez2/node_modules/@storybook/react/bin/index.js:3:1)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
Please specify which version of Storybook and optionally any affected addons that you’re running
"@storybook/react": "^3.4.10",
"@storybook/addon-actions": "^3.4.10",
"@storybook/addon-links": "^3.4.10",
"@storybook/addons": "^3.4.10",
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (2 by maintainers)
Top Results From Across the Web
Storybook-tailwind. How should I add tailwind to storybook
Storybook recommends using the @storybook/addon-postcss for customizing the postCSS config from now on (instead of relying on customizing ...
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 >Integrate Tailwind CSS with Storybook
How to setup Tailwind CSS and Storybook. Storybook.js is a fantastic tool for developing and showcasing UI components in isolation.
Read more >Configure Tailwind for your Storybook Setup in an Nx ...
We want to have a library where to host our UI components s.t. they can be easily shared within our Nx workspace. In...
Read more >Building a front end project with React, TailwindCSS and ...
The config files in the .storybook folder are available for us to customize down the line if we need to, and the src/stories...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I did manage to get it working at the end. Just like @igor-dv mentioned, you shouldn’t put them in your
webpack.config.js
but rather in apostcss.config.js
file. Once you set up PostCSS on Webpack, then set up Tailwind as a PostCSS plugin and it’ll get picked up. That’s what the Tailwind docs also say.NOTE: I do over comment my code so you’ll have a lot to read as i post my code below.
// webpack.config.js
// postcss.config.js
This is super useful ⬆️ 🤯 💚
In addition to this, I have struggled a bit because I have CRA + Storybook + TS + Craco setup. I’m just makin a switch from SCSS to Tailwind.
So obviously I forgot to add a postcss.config.js inside the project root:
Also always remember that with Tailwind there is no import for the styles on component level (compared to simple CSS or CSS modules) so Webpack (used in Storybook) does not know where to grab the entry point for Tailwind.
❗️❗️❗️ To make this happen, be sure that you make a
import 'index.css';
in.storybook/preview.js
. This way as Webpack starts to load in, it can grab Tailwind’s entry point.Sorry for the extension but I also needed these info to make this happen. Hopefully will help someone out there. 👋
Versions used: