Storybook styled-components tailwindcss design system - TypeError: plugin is not a function
Describe the problem:
I'm trying to create custom plugins for the themes we are creating for our components using styled-components and tailwindcss in a storybook setup. I have tthe tailwind.config.js setup like this
// postccss.config.js
module.exports = {
plugins: [
require('postcss-import'),
require('tailwindcss'),
require('./tailwind.config.js'),// only fails when I include this line
require('autoprefixer')
]
}
// tailwind.config.js
const plugin = require('tailwindcss/plugin')
module.exports = {
future: {
removeDeprecatedGapUtilities: true,
purgeLayersByDefault: true
},
purge: [],
target: 'relaxed',
prefix: '',
important: false,
separator: ':',
theme: {
color: {
super: '#F26D0D',
primary: '#202124',
secondary: '#FFFFFF'
},
fontSize: {
'2xl': '48px',
xl: '36px',
lg: '24px'
},
extend: {}
},
variants: {},
plugins: [
// 'preflight'
plugin(function({ addBase, config }) {
// only fails after this method is called.
addBase({
h1: { background: config('theme.color.primary') },
h2: { background: config('theme.color.primary') },
h3: { background: config('theme.color.primary') }
})
})
]
}
However when I run it I get this output:
% yarn build:css
yarn run v1.22.5
$ postcss src/assets/tailwind.css -o src/assets/styles.css
TypeError: plugin is not a function
at LazyResult.run (/node_modules/postcss-cli/node_modules/postcss/lib/lazy-result.js:295:14)
at LazyResult.asyncTick (/node_modules/postcss-cli/node_modules/postcss/lib/lazy-result.js:208:26)
at LazyResult.asyncTick (/node_modules/postcss-cli/node_modules/postcss/lib/lazy-result.js:221:14)
at /node_modules/postcss-cli/node_modules/postcss/lib/lazy-result.js:213:17
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
If I take out the tailwindcss plugin everything compiles normal. When I include it then it fails. I'm not sure why.
Somehow it is causing the postcss to fail, with a result that isn't a method. I haven't been able to determine what is the cause.
The tailwind.config.js file is not a PostCSS plugin, it's your Tailwind config file. You shouldn't include it in your postcss.config.js file. Just write it like this:
// postccss.config.js
module.exports = {
plugins: [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer')
]
}
You only need require('tailwindcss') 👍 Tailwind will pick up your config file automatically. If you used an unconventional name or location for your Tailwind config file, you specify it like this:
// postccss.config.js
module.exports = {
plugins: [
require('postcss-import'),
require('tailwindcss')('./config-files/tailwind.config.js'),
require('autoprefixer')
]
}
This solved my problem! It wasn't running my config otherwise. Now with doing it like this it can find my file.
require('tailwindcss')('./config-files/tailwind.config.js'),
Thanks!!