Anyone have success setting up a project using VueJS, PostCss, and Tailwind with component development in Storybook?
I've gotten this far:
vue
project (vue-cli 3.0.5
)<style lang="postcss"> ... </style>
@apply
within style block to apply utility classes to componentThe issue I run into is that any components I create stories for using lang="postcss"
fail during compilation when running storybook.
I tried adding a custom webpack
config which stops the errors but none of my components is styled still.
const path = require('path')
module.exports = {
module: {
rules: [
{
test: /\.postcss$/,
loaders: ["style-loader", "css-loader", "postcss-loader"],
include: path.resolve(__dirname, '../')
}
]
}
}
I've also tried importing my app.postcss
(which imports tailwind) within the stories.js
file itself to no avail. Any help would be appreciated.
PostCSS. Vue CLI uses PostCSS internally. You can configure PostCSS via . postcssrc or any config source supported by postcss-load-config, and configure postcss-loader via css.
I've got a fully working example of Svelte + TailwindCSS + Storybook in this github repo https://github.com/jerriclynsjohn/svelte-storybook-tailwind
But Integrating should be very much similar:
webpack.config.js
in your .storybook
folder and add the following:const path = require('path');
module.exports = ({ config, mode }) => {
config.module.rules.push({
test: /\.css$/,
loaders: [
{
loader: 'postcss-loader',
options: {
sourceMap: true,
config: {
path: './.storybook/',
},
},
},
],
include: path.resolve(__dirname, '../storybook/'),
});
return config;
};
postcss.config.js
in your .storybook
folder with the following:var tailwindcss = require('tailwindcss');
module.exports = {
plugins: [
require('postcss-import')(),
tailwindcss('./tailwind.config.js'), //This refers to your tailwind config
require('autoprefixer'),
],
};
utils.css
file in your storybook folder@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
<>.stories.js
:import '../../css/utils.css';
I really recommend you refer to the implementation in the github repo, it also has things that are specific to Storybook. (Github)
I've never used tailwindCSS or postCSS (explicitly) before, so I decided to take this as an opportunity to learn to setup/configure both of these.
You can find a complete example with Tailwind-styled components in storybook here: https://github.com/gurupras/vuejs-postcss-tailwind-with-storybook
vue create vue-postcss-tailwind-storybook
npm install tailwindcss
./node_modules/.bin/tailwind init
(generates tailwind.config.js
)module.exports = {
plugins: [
require('tailwindcss')('tailwind.config.js'),
require('autoprefixer')()
]
}
vue add storybook
src/style/tailwind.css
)@tailwind base;
@tailwind components;
@tailwind utilities;
import '@/style/tailwind.css'
to your main.js
to ensure that they're available to all parts of your appsrc/components/alert.vue
/* eslint-disable import/no-extraneous-dependencies */
import { storiesOf } from '@storybook/vue'
// You need to import this once
import '@/style/tailwind.css'
import Alert from '@/components/alert.vue'
storiesOf('Alert', module)
.add('with text', () => ({
components: { Alert },
template: '<Alert text="Test alert" :show-close="true"/>'
}))
npm run storybook:serve
Hope this helps with your setup. Meanwhile, I'm going to read up and see how TailwindCSS can help me create better components!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With