|
| 1 | +--- |
| 2 | +title: TailwindCSS |
| 3 | +description: Learn how to use TailwindCSS in a Turborepo. |
| 4 | +--- |
| 5 | + |
| 6 | +import { PackageManagerTabs, Tabs, Tab } from '#components/tabs'; |
| 7 | +import { Callout } from '#components/callout'; |
| 8 | +import { Steps, Step } from '#components/steps'; |
| 9 | + |
| 10 | +[TailwindCSS](https://tailwindcss.com/) is a CSS framework that allows you to rapidly build modern websites without ever leaving your HTML. |
| 11 | + |
| 12 | +## Quickstart |
| 13 | + |
| 14 | +If you'd rather use a template, this guide is walking throw how to build [this TailwindCSS + Turborepo template](https://github.com/vercel/turborepo/tree/main/examples/with-tailwind). |
| 15 | + |
| 16 | +<PackageManagerTabs> |
| 17 | + |
| 18 | +<Tab value="pnpm"> |
| 19 | + |
| 20 | +```bash title="Terminal" |
| 21 | +pnpm dlx create-turbo@latest -e with-tailwind |
| 22 | +``` |
| 23 | + |
| 24 | +</Tab> |
| 25 | + |
| 26 | +<Tab value="yarn"> |
| 27 | + |
| 28 | +```bash title="Terminal" |
| 29 | +yarn dlx create-turbo@latest -e with-tailwind |
| 30 | +``` |
| 31 | + |
| 32 | +</Tab> |
| 33 | + |
| 34 | +<Tab value="npm"> |
| 35 | + |
| 36 | +```bash title="Terminal" |
| 37 | +npx create-turbo@latest -e with-tailwind |
| 38 | +``` |
| 39 | + |
| 40 | +</Tab> |
| 41 | + |
| 42 | +<Tab value="bun (Beta)"> |
| 43 | + |
| 44 | +```bash title="Terminal" |
| 45 | +bunx create-turbo@latest -e with-tailwind |
| 46 | +``` |
| 47 | + |
| 48 | +</Tab> |
| 49 | +</PackageManagerTabs> |
| 50 | + |
| 51 | +## Guide |
| 52 | + |
| 53 | +<Callout type="info">This guide is for TailwindCSS v4.</Callout> |
| 54 | + |
| 55 | +<Steps> |
| 56 | +<Step> |
| 57 | + |
| 58 | +### Create a monorepo |
| 59 | + |
| 60 | +If you don't have an existing project, use [create-turbo](/docs/getting-started/installation) to create a new monorepo: |
| 61 | + |
| 62 | +<PackageManagerTabs> |
| 63 | + |
| 64 | +<Tab value="pnpm"> |
| 65 | + |
| 66 | +```bash title="Terminal" |
| 67 | +pnpm dlx create-turbo@latest |
| 68 | +``` |
| 69 | + |
| 70 | +</Tab> |
| 71 | + |
| 72 | +<Tab value="yarn"> |
| 73 | + |
| 74 | +```bash title="Terminal" |
| 75 | +yarn dlx create-turbo@latest |
| 76 | +``` |
| 77 | + |
| 78 | +</Tab> |
| 79 | + |
| 80 | +<Tab value="npm"> |
| 81 | + |
| 82 | +```bash title="Terminal" |
| 83 | +npx create-turbo@latest |
| 84 | +``` |
| 85 | + |
| 86 | +</Tab> |
| 87 | + |
| 88 | +<Tab value="bun (Beta)"> |
| 89 | + |
| 90 | +```bash title="Terminal" |
| 91 | +bunx create-turbo@latest |
| 92 | +``` |
| 93 | + |
| 94 | +</Tab> |
| 95 | +</PackageManagerTabs> |
| 96 | + |
| 97 | +</Step> |
| 98 | + |
| 99 | +<Step> |
| 100 | + |
| 101 | +### Add TailwindCSS to your application |
| 102 | + |
| 103 | +[Follow TailwindCSS's guides](https://tailwindcss.com/docs/installation/using-vite) to set up TailwindCSS for your frontend framework. |
| 104 | + |
| 105 | +Once completed, we can integrate your UI package into the applications. |
| 106 | + |
| 107 | +</Step> |
| 108 | + |
| 109 | +<Step> |
| 110 | + |
| 111 | +### Create a shared TailwindCSS configuration package |
| 112 | + |
| 113 | +First, build an [Internal Package](https://turborepo.com/docs/core-concepts/internal-packages) with four files: |
| 114 | + |
| 115 | +<Tabs items={["package.json", "tailwind.config.ts", "tsconfig.json", "postcss.config.js (Optional)"]}> |
| 116 | + |
| 117 | +<Tab value="package.json"> |
| 118 | + |
| 119 | +This `package.json` installs TailwindCSS so we can create the configuration file and exports the configuration file for use in the rest of the repository. |
| 120 | + |
| 121 | +```json title="./packages/tailwind-config/package.json" |
| 122 | +{ |
| 123 | + "name": "@repo/tailwind-config", |
| 124 | + "version": "0.0.0", |
| 125 | + "private": true, |
| 126 | + "exports": { |
| 127 | + ".": "./tailwind.config.ts" |
| 128 | + }, |
| 129 | + "devDependencies": { |
| 130 | + "@repo/typescript-config": "workspace:*", |
| 131 | + "tailwindcss": "^4.1.5" |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +</Tab> |
| 137 | + |
| 138 | +<Tab value="tailwind.config.ts"> |
| 139 | + |
| 140 | +This `tailwind.config.ts` will be shared to the libraries and applications in the repository. |
| 141 | + |
| 142 | +```ts title="./packages/tailwind-config/tailwind.config.ts" |
| 143 | +import type { Config } from 'tailwindcss'; |
| 144 | + |
| 145 | +const config: Omit<Config, 'content'> = { |
| 146 | + darkMode: 'class', |
| 147 | +}; |
| 148 | +export default config; |
| 149 | +``` |
| 150 | + |
| 151 | +</Tab> |
| 152 | + |
| 153 | +<Tab value="tsconfig.json"> |
| 154 | + |
| 155 | +A simple TypeScript configuration file so we can use TypeScript for the `tailwind.config.ts` |
| 156 | + |
| 157 | +```ts title="./packages/tailwind-config/tconfig.json" |
| 158 | +{ |
| 159 | + "extends": "@repo/typescript-config/base.json", |
| 160 | + "include": ["."], |
| 161 | + "exclude": ["dist", "build", "node_modules"] |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +</Tab> |
| 166 | + |
| 167 | +<Tab value="postcss.config.js (Optional)"> |
| 168 | + |
| 169 | +If your frontends need a PostCSS configuration file, you can create one to share. |
| 170 | + |
| 171 | +```js title="./packages/tailwind-config/postcss.config.js" |
| 172 | +export const postcssConfig = { |
| 173 | + plugins: { |
| 174 | + '@tailwindcss/postcss': {}, |
| 175 | + }, |
| 176 | +}; |
| 177 | +``` |
| 178 | + |
| 179 | +</Tab> |
| 180 | +</Tabs> |
| 181 | + |
| 182 | +</Step> |
| 183 | + |
| 184 | +<Step> |
| 185 | + |
| 186 | +### Create the UI package |
| 187 | + |
| 188 | +You can now build the components you share to your applications. |
| 189 | + |
| 190 | +For a full example, [visit the source code for `@repo/ui` package in the TailwindCSS example](https://github.com/vercel/turborepo/tree/main/examples/with-tailwind/packages/ui). The files required for your TailwindCSS setup are below. |
| 191 | + |
| 192 | +<Tabs items={["package.json", "turbo.json", "tailwind.config.ts", "styles.css"]}> |
| 193 | + |
| 194 | +<Tab value="package.json"> |
| 195 | + |
| 196 | +The `package.json` installs the dependencies for the package, sets up scripts for development and build environments, and marks the exports for the package. |
| 197 | + |
| 198 | +```json title="./packages/ui/package.json" |
| 199 | +{ |
| 200 | + "exports": { |
| 201 | + "./styles.css": "./dist/index.css", |
| 202 | + "./*": "./dist/*.js" |
| 203 | + }, |
| 204 | + "scripts": { |
| 205 | + "build:styles": "tailwindcss -i ./src/styles.css -o ./dist/index.css", |
| 206 | + "build:components": "tsc", |
| 207 | + "dev:styles": "tailwindcss -i ./src/styles.css -o ./dist/index.css --watch", |
| 208 | + "dev:components": "tsc --watch" |
| 209 | + }, |
| 210 | + "devDependencies": { |
| 211 | + "@repo/tailwind-config": "workspace:*", |
| 212 | + "@tailwindcss/cli": "^4.1.5", |
| 213 | + "@tailwindcss/postcss": "^4.1.5", |
| 214 | + "autoprefixer": "^10.4.20", |
| 215 | + "tailwindcss": "^4.1.5" |
| 216 | + } |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +<Callout type="good-to-know"> |
| 221 | + Above, we've only included the code related to setting up Tailwind. The full |
| 222 | + package.json is |
| 223 | + [here](https://github.com/vercel/turborepo/tree/main/examples/with-tailwind/packages/ui/package.json). |
| 224 | +</Callout> |
| 225 | + |
| 226 | +</Tab> |
| 227 | + |
| 228 | +<Tab value="turbo.json"> |
| 229 | + |
| 230 | +Create a `build` and `dev` task that runs the scripts for building of components and style sheets in parallel. |
| 231 | + |
| 232 | +```json title="./packages/ui/turbo.json" |
| 233 | +{ |
| 234 | + "extends": ["//"], |
| 235 | + "tasks": { |
| 236 | + "build": { |
| 237 | + "dependsOn": ["build:styles", "build:components"] |
| 238 | + }, |
| 239 | + "build:styles": { |
| 240 | + "outputs": ["dist/**"] |
| 241 | + }, |
| 242 | + "build:components": { |
| 243 | + "outputs": ["dist/**"] |
| 244 | + }, |
| 245 | + "dev": { |
| 246 | + "with": ["dev:styles", "dev:components"] |
| 247 | + }, |
| 248 | + "dev:styles": { |
| 249 | + "cache": false, |
| 250 | + "persistent": true |
| 251 | + }, |
| 252 | + "dev:components": { |
| 253 | + "cache": false, |
| 254 | + "persistent": true |
| 255 | + } |
| 256 | + } |
| 257 | +} |
| 258 | +``` |
| 259 | + |
| 260 | +</Tab> |
| 261 | + |
| 262 | +<Tab value="tailwind.config.ts"> |
| 263 | + |
| 264 | +Use the shared configuration from the Tailwind configuration package. |
| 265 | + |
| 266 | +```ts title="./packages/ui/turbo.json" |
| 267 | +import sharedConfig from '@repo/tailwind-config'; |
| 268 | + |
| 269 | +export default sharedConfig; |
| 270 | +``` |
| 271 | + |
| 272 | +</Tab> |
| 273 | + |
| 274 | +<Tab value="styles.css"> |
| 275 | + |
| 276 | +```css title="./packages/ui/src/index.css" |
| 277 | +@import 'tailwindcss'; |
| 278 | +``` |
| 279 | + |
| 280 | +</Tab> |
| 281 | + |
| 282 | +</Tabs> |
| 283 | + |
| 284 | +</Step> |
| 285 | +<Step> |
| 286 | + |
| 287 | +### Use the UI package in an application |
| 288 | + |
| 289 | +Install the packages you've created into your application. |
| 290 | + |
| 291 | +<PackageManagerTabs> |
| 292 | + |
| 293 | +<Tab value="pnpm"> |
| 294 | + |
| 295 | +```bash title="Terminal" |
| 296 | +pnpm install @repo/ui @repo/tailwind-config --save-dev --filter=@repo/ui --filter=web |
| 297 | +``` |
| 298 | + |
| 299 | +</Tab> |
| 300 | + |
| 301 | +<Tab value="yarn"> |
| 302 | + |
| 303 | +```bash title="Terminal" |
| 304 | +yarn workspace web add @repo/ui @repo/tailwind-config --dev |
| 305 | +yarn workspace @repo/ui add @repo/ui @repo/tailwind-config --dev |
| 306 | +``` |
| 307 | + |
| 308 | +</Tab> |
| 309 | + |
| 310 | +<Tab value="npm"> |
| 311 | + |
| 312 | +```bash title="Terminal" |
| 313 | +npm install @repo/ui @repo/tailwind-config --workspace=web --workspace=@repo/ui --save-dev |
| 314 | +``` |
| 315 | + |
| 316 | +</Tab> |
| 317 | + |
| 318 | +<Tab value="bun (Beta)"> |
| 319 | + |
| 320 | +```bash title="Terminal" |
| 321 | +bun install @repo/ui @repo/tailwind-config --dev --filter=@repo/ui --filter=web |
| 322 | +``` |
| 323 | + |
| 324 | +</Tab> |
| 325 | +</PackageManagerTabs> |
| 326 | + |
| 327 | +Then, configure the files in your application so the styles from the UI package are reflected in the application. |
| 328 | + |
| 329 | +<Tabs items={["tailwind.config.ts", "globals.css", "postcss.config.js (Optional)"]}> |
| 330 | + |
| 331 | +<Tab value="tailwind.config.ts"> |
| 332 | + |
| 333 | +```ts title="./apps/web/tailwind.config.ts" |
| 334 | +import sharedConfig from '@repo/tailwind-config'; |
| 335 | + |
| 336 | +export default sharedConfig; |
| 337 | +``` |
| 338 | + |
| 339 | +</Tab> |
| 340 | + |
| 341 | +<Tab value="globals.css"> |
| 342 | + |
| 343 | +```css title="./apps/web/app/globals.css" |
| 344 | +@import 'tailwindcss'; |
| 345 | +@import '@repo/ui/styles.css'; |
| 346 | +``` |
| 347 | + |
| 348 | +</Tab> |
| 349 | + |
| 350 | +<Tab value="postcss.config.js (Optional)"> |
| 351 | + |
| 352 | +```js title="./apps/web/postcss.config.js" |
| 353 | +import { postcssConfig } from '@repo/tailwind-config/postcss'; |
| 354 | + |
| 355 | +export default postcssConfig; |
| 356 | +``` |
| 357 | + |
| 358 | +</Tab> |
| 359 | + |
| 360 | +</Tabs> |
| 361 | + |
| 362 | +</Step> |
| 363 | + |
| 364 | +</Steps> |
0 commit comments