tailwind 3 doesn't work with storybook/webpack setup
Problem
I'm using macOS, Chrome v96, Node.js 16 I created a github repository with this exact issue - https://github.com/sachinmour/tailwind-issue Basically just do `yarn` and `yarn storybook` and you'll see storybook loaded correctly. change package.json to use tailwind `^3.0.0` and run `yarn` and `yarn storybook` again and you'll see no tailwind is loaded inside storybook.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Tailwind CSS Integration with Storybook in Webpack Setup
The issue arises because Tailwind CSS requires a PostCSS configuration to process its directives. In the Storybook setup, the PostCSS loader might not be configured correctly to recognize Tailwind CSS, leading to the absence of Tailwind styles in the Storybook environment.
Awaiting Verification
Be the first to verify this fix
- 1
Install PostCSS and Tailwind CSS
Ensure that PostCSS and Tailwind CSS are installed in your project. This is necessary for processing Tailwind's utility classes.
bashyarn add -D postcss postcss-loader tailwindcss - 2
Create PostCSS Configuration
Create a PostCSS configuration file named 'postcss.config.js' in the root of your project. This file will configure PostCSS to use Tailwind CSS.
javascriptmodule.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, }; - 3
Update Storybook's Webpack Configuration
Modify the Storybook Webpack configuration to include the PostCSS loader. This ensures that Tailwind CSS is processed correctly when Storybook builds the styles.
javascriptmodule.exports = { webpackFinal: async (config) => { config.module.rules.push({ test: /.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'], }); return config; }, }; - 4
Include Tailwind CSS in Your Styles
Ensure that you include Tailwind's base styles in your main CSS file. Create a CSS file (e.g., 'styles.css') and add the following lines to include Tailwind's styles.
css@tailwind base; @tailwind components; @tailwind utilities; - 5
Restart Storybook
After making the above changes, restart your Storybook server to apply the new configurations.
bashyarn storybook
Validation
To confirm the fix worked, run 'yarn storybook' and check if Tailwind CSS styles are applied correctly in the Storybook environment. You should see Tailwind's utility classes reflected in the components.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep