I am trying to include tailwindcss into my custom Gatsby Theme via twin.macro. I am using yarn workspaces and the project directory tree is setup as follows:
./site - The actual site which contains the content
./packages/gatsby-theme-custom/** - The Gatsby theme
So my site pulls in gatsby-theme-custom and fills it with its own contents.
The integration of tailwindcss per se has worked fine so far. Now I am trying to add a tailwind.config.js file to the root of gatsby-theme-custom but it seems the file does not get picked up during compilation. For example I was trying to extend the base theme with some custom colours:
module.exports = {
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {
colors: {
electric: "#db00ff",
ribbon: "#0047ff",
wonderfulgray: "#eeeeee",
},
},
},
variants: {
extend: {},
},
plugins: [],
};
Then in some file inside the theme:
import tw from "twin.macro";
...
return (
<div
css={[
tw`flex flex-col items-center justify-center h-screen`,
tw`bg-gradient-to-b from-electric to-wonderfulgray`,
]}
>
Test
</div>
)
When I now compile the site I am getting errors that the referenced colours cannot be found.
✕ from-electric was not found
When I put the config file into the root of the site everything is working fine. The issue now is that the site should basically not know anything about the styling. Everything related to the styling should be encapsulated into the theme. Is there a way to accomplish that tailwind is picking up the config file from the theme instead ? Or did I make some errors along the way ?
Any help is appreciated !
I just found the answer. It is possible with
twin.macroto specify the path to the tailwindcss config file.I added a
babel-plugin-macros.config.jsfile at the root of thegatsby-theme-customdirectory. Secondly I added thetailwind.config.jsat the root fo the theme directory as well.The content of the
babel-plugin-macros.config.jsfile looks like the following:${__dirname}is important here in order to pick the root of theGatsby-theme-customdirectory.With that configuration it was possible to place the tailwind config file inside the theme directory and have it encapsulated away from the
sitedirectory.