Sharing Tailwind components in a monorepo

See original GitHub issue

First of all thanks for the great tool. This is a report that something is not working right, but also a question on how you recommend to do this kind of thing. There are many tools at play here so hopefully you can point me in the right direction.

Describe the problem:

I have a monorepo like this:

packages/
  app-1/
  app-2/
  ui/

My apps are a combination of create-react-app and Next.js, but for now I just want to get my demo working with two create-react-app applications.

Ideally I’d be able to have a ui/common.css file that I can import into my applications:

@import '@tailwind-test/ui/common.css';

/* ./src/index.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

but that doesn’t work. Additionally I’d like to be able to import components (e.g. ui/button.tsx) and have the Tailwind classes still autogenerated. ie. ui/button.tsx uses bg-blue-500 and that CSS class is still generated in the application.

Link to a minimal reproduction:

https://github.com/AlexBHarley/tailwind-shared-components-demo.

Right now I can’t even get @import to work so I’m copy pasting my common.css file into the tailwind.css file of each of my three applications.

To run:

  1. yarn
  2. yarn lerna bootstrap
  3. yarn lerna run start --stream

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:21
  • Comments:9

github_iconTop GitHub Comments

15reactions
ANTARES-KORcommented, Jul 1, 2022

One Solution is to elaborate all file paths which uses tailwind in the tailwind.config.js

for example, if package structure is like this

/packages
   /ui : @something/ui
/app

and if you want to import component from your app project /app/tailwind.config.js should be somewhat like this :

module.exports = {
  content: [
    "../packages/ui/**/*.{js,ts,jsx,tsx}"
    ...
  ],
  ...
}

I hope this might help someone.

7reactions
max-symcommented, May 3, 2022

@Manubi I didn’t find any way other than installing tailwind separately in each project that uses UI package, but having only the code in the UI package + tailwind configuration file. So would be something like this:

packages
  landing-page
    src
      index.js
  ui
    src
      button.js
    package.json
    tailwindcss.config.js
package.json

And then landing-page would have tailwindcss in it, but it would use the config from ui. This is useful when there’s, let’s say another landing page that is supposed to use the same config as the first landing page.

The drawback is that we’d have to install tailwind package in both of the landing pages (but not in the UI!). Your ui tailwindcss config file would have the base and the tailwindcss configs in the other packages can be defined as:

const baseConfig = require("../ui/tailwind.config")

module.exports = {
  ...baseConfig,
  content: ["./src/**/*.{js,jsx,ts,tsx}", "../ui/src/**/*.{js,jsx,ts,tsx}"],
  plugins: [],
}

And then import the components from the ui as:

import {Button} from "@my-app/ui"

const Page = () => (
  <Button> {"Hello"}</Button>
)
...

This worked perfectly for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to setup react shared components in monorepo with ...
Previously, we have integrated tailwind and react using webpack. Now we will try to apply it to shared components in monorepo.
Read more >
Configure Tailwind in a Nx Monorepo With Potentially Multiple ...
In this lesson, we learn how to have a global Nx monorepo ... Share React Components in Next.js Applications with Nx Workspace Libraries....
Read more >
Compiling Tailwind CSS components in monorepo
If you have monorepo with Tailwind CSS components in one package and application in the other you may find that Tailwind won't work...
Read more >
Monorepo, Multi Project Components, & Styling Consistency.
Tailwind provided us a configurable structure while writing very minimal CSS and with the help of PostCSS providing plugin system, adding ...
Read more >
Turborepo Tutorial | Part 1 - Typescript, Eslint, Tailwind, Husky ...
Turborepo Tutorial | Part 1 - Typescript, Eslint, Tailwind, Husky shared config setup in a Monorepo · Chapters. View all · Chapters ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found