|
| 1 | +--- |
| 2 | +title: Use with ESBuild - Flowbite React |
| 3 | +description: Learn how to install Flowbite React with ESBuild |
| 4 | +--- |
| 5 | + |
| 6 | +This guide provides three ways to integrate Flowbite React with ESBuild: |
| 7 | + |
| 8 | +1. [Quick Start](#quick-start): Create a new project with everything pre-configured |
| 9 | +2. [Add to Existing Project](#add-to-existing-project): Add Flowbite React to an existing ESBuild project |
| 10 | +3. [Manual Setup](#manual-setup): Set up everything from scratch manually |
| 11 | + |
| 12 | +<TextDivider>Quick Start (Recommended)</TextDivider> |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +The fastest way to get started is using our project creation CLI, which sets up a new ESBuild project with Flowbite React, Tailwind CSS, and all necessary configurations: |
| 17 | + |
| 18 | +```bash |
| 19 | +npx create-flowbite-react@latest --template esbuild |
| 20 | +``` |
| 21 | + |
| 22 | +This will: |
| 23 | + |
| 24 | +- Create a new ESBuild project |
| 25 | +- Install and configure Tailwind CSS |
| 26 | +- Set up Flowbite React with all required dependencies |
| 27 | +- Configure dark mode support |
| 28 | +- Set up example components |
| 29 | + |
| 30 | +<TextDivider>Add to Existing Project</TextDivider> |
| 31 | + |
| 32 | +## Add to Existing Project |
| 33 | + |
| 34 | +If you already have an ESBuild project and want to add Flowbite React, you can use our initialization CLI: |
| 35 | + |
| 36 | +```bash |
| 37 | +npx flowbite-react@latest init |
| 38 | +``` |
| 39 | + |
| 40 | +This will automatically: |
| 41 | + |
| 42 | +- Install Flowbite React and its dependencies |
| 43 | +- Configure Tailwind CSS to include Flowbite React plugin |
| 44 | +- Set up necessary configurations |
| 45 | + |
| 46 | +<TextDivider>Manual Setup</TextDivider> |
| 47 | + |
| 48 | +## Manual Setup |
| 49 | + |
| 50 | +If you prefer to set everything up manually or need more control over the configuration, follow these steps: |
| 51 | + |
| 52 | +### 1. Create Project |
| 53 | + |
| 54 | +Create a new directory and initialize a new project: |
| 55 | + |
| 56 | +```bash |
| 57 | +mkdir esbuild-react-app |
| 58 | +cd esbuild-react-app |
| 59 | +npm init -y |
| 60 | +``` |
| 61 | + |
| 62 | +Create `package.json` file: |
| 63 | + |
| 64 | +```json {7,8} |
| 65 | +{ |
| 66 | + "name": "esbuild-react-app", |
| 67 | + "private": true, |
| 68 | + "version": "0.0.0", |
| 69 | + "type": "module", |
| 70 | + "scripts": { |
| 71 | + "dev": "node esbuild.dev.js", |
| 72 | + "build": "node esbuild.build.js" |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Install the required dependencies: |
| 78 | + |
| 79 | +```bash |
| 80 | +npm install react react-dom |
| 81 | +npm install -D @types/react @types/react-dom esbuild typescript tailwindcss @tailwindcss/postcss |
| 82 | +``` |
| 83 | + |
| 84 | +Create `tsconfig.json`: |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "compilerOptions": { |
| 89 | + "target": "ES2020", |
| 90 | + "useDefineForClassFields": true, |
| 91 | + "lib": ["ES2020", "DOM", "DOM.Iterable"], |
| 92 | + "module": "ESNext", |
| 93 | + "skipLibCheck": true, |
| 94 | + "moduleResolution": "bundler", |
| 95 | + "allowImportingTsExtensions": true, |
| 96 | + "resolveJsonModule": true, |
| 97 | + "isolatedModules": true, |
| 98 | + "noEmit": true, |
| 99 | + "jsx": "react-jsx", |
| 100 | + "strict": true, |
| 101 | + "noUnusedLocals": true, |
| 102 | + "noUnusedParameters": true, |
| 103 | + "noFallthroughCasesInSwitch": true |
| 104 | + }, |
| 105 | + "include": ["src"] |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +Create `esbuild.dev.js`: |
| 110 | + |
| 111 | +```ts |
| 112 | +import { readFile } from "node:fs/promises"; |
| 113 | +import { createServer } from "node:http"; |
| 114 | +import tailwindcss from "@tailwindcss/postcss"; |
| 115 | +import * as esbuild from "esbuild"; |
| 116 | +import flowbiteReact from "flowbite-react/plugin/esbuild"; |
| 117 | +import postcss from "postcss"; |
| 118 | + |
| 119 | +const clients = new Set(); |
| 120 | + |
| 121 | +const cssPlugin = { |
| 122 | + name: "css", |
| 123 | + setup(build) { |
| 124 | + build.onLoad({ filter: /\.css$/ }, async (args) => { |
| 125 | + const css = await readFile(args.path, "utf8"); |
| 126 | + const result = await postcss([tailwindcss]).process(css, { |
| 127 | + from: args.path, |
| 128 | + map: { inline: true }, |
| 129 | + }); |
| 130 | + |
| 131 | + return { |
| 132 | + contents: result.css, |
| 133 | + loader: "css", |
| 134 | + }; |
| 135 | + }); |
| 136 | + }, |
| 137 | +}; |
| 138 | + |
| 139 | +const buildOptions = { |
| 140 | + entryPoints: ["src/main.tsx"], |
| 141 | + bundle: true, |
| 142 | + outdir: "dist", |
| 143 | + sourcemap: true, |
| 144 | + format: "esm", |
| 145 | + loader: { |
| 146 | + ".tsx": "tsx", |
| 147 | + ".ts": "tsx", |
| 148 | + ".jsx": "jsx", |
| 149 | + ".js": "jsx", |
| 150 | + }, |
| 151 | + plugins: [ |
| 152 | + cssPlugin, |
| 153 | + flowbiteReact(), |
| 154 | + { |
| 155 | + name: "live-reload", |
| 156 | + setup(build) { |
| 157 | + build.onEnd(() => { |
| 158 | + clients.forEach((client) => client.write("data: update\n\n")); |
| 159 | + }); |
| 160 | + }, |
| 161 | + }, |
| 162 | + ], |
| 163 | +}; |
| 164 | + |
| 165 | +const ctx = await esbuild.context(buildOptions); |
| 166 | +await ctx.watch(); |
| 167 | + |
| 168 | +// Initial build |
| 169 | +await ctx.rebuild(); |
| 170 | + |
| 171 | +// Simple HTTP server |
| 172 | +createServer(async (req, res) => { |
| 173 | + const { url } = req; |
| 174 | + |
| 175 | + if (url === "/esbuild") { |
| 176 | + return clients.add( |
| 177 | + res.writeHead(200, { |
| 178 | + "Content-Type": "text/event-stream", |
| 179 | + "Cache-Control": "no-cache", |
| 180 | + Connection: "keep-alive", |
| 181 | + }), |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + try { |
| 186 | + let path = url === "/" ? "/index.html" : url; |
| 187 | + const filePath = path.startsWith("/dist") ? path.slice(1) : `.${path}`; |
| 188 | + |
| 189 | + const content = await readFile(filePath); |
| 190 | + const ext = path.split(".").pop(); |
| 191 | + |
| 192 | + const contentTypes = { |
| 193 | + html: "text/html", |
| 194 | + js: "text/javascript", |
| 195 | + css: "text/css", |
| 196 | + png: "image/png", |
| 197 | + jpg: "image/jpeg", |
| 198 | + jpeg: "image/jpeg", |
| 199 | + gif: "image/gif", |
| 200 | + svg: "image/svg+xml", |
| 201 | + }; |
| 202 | + |
| 203 | + res.writeHead(200, { |
| 204 | + "Content-Type": contentTypes[ext] || "text/plain", |
| 205 | + }); |
| 206 | + res.end(content); |
| 207 | + } catch (e) { |
| 208 | + res.writeHead(404); |
| 209 | + res.end("Not found"); |
| 210 | + } |
| 211 | +}).listen(3000); |
| 212 | + |
| 213 | +console.log("Development server running on http://localhost:3000"); |
| 214 | +``` |
| 215 | + |
| 216 | +Create `esbuild.build.js`: |
| 217 | + |
| 218 | +```ts |
| 219 | +import { readFile } from "node:fs/promises"; |
| 220 | +import tailwindcss from "@tailwindcss/postcss"; |
| 221 | +import * as esbuild from "esbuild"; |
| 222 | +import flowbiteReact from "flowbite-react/plugin/esbuild"; |
| 223 | +import postcss from "postcss"; |
| 224 | + |
| 225 | +const cssPlugin = { |
| 226 | + name: "css", |
| 227 | + setup(build) { |
| 228 | + build.onLoad({ filter: /\.css$/ }, async (args) => { |
| 229 | + const css = await readFile(args.path, "utf8"); |
| 230 | + const result = await postcss([tailwindcss]).process(css, { |
| 231 | + from: args.path, |
| 232 | + }); |
| 233 | + |
| 234 | + return { |
| 235 | + contents: result.css, |
| 236 | + loader: "css", |
| 237 | + }; |
| 238 | + }); |
| 239 | + }, |
| 240 | +}; |
| 241 | + |
| 242 | +await esbuild.build({ |
| 243 | + entryPoints: ["src/main.tsx"], |
| 244 | + bundle: true, |
| 245 | + minify: true, |
| 246 | + sourcemap: true, |
| 247 | + outdir: "dist", |
| 248 | + format: "esm", |
| 249 | + loader: { |
| 250 | + ".tsx": "tsx", |
| 251 | + ".ts": "tsx", |
| 252 | + ".jsx": "jsx", |
| 253 | + ".js": "jsx", |
| 254 | + }, |
| 255 | + plugins: [cssPlugin, flowbiteReact()], |
| 256 | +}); |
| 257 | +``` |
| 258 | + |
| 259 | +Create `index.html`: |
| 260 | + |
| 261 | +```html |
| 262 | +<!DOCTYPE html> |
| 263 | +<html lang="en"> |
| 264 | + <head> |
| 265 | + <meta charset="UTF-8" /> |
| 266 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 267 | + <title>esbuild React App</title> |
| 268 | + <link rel="stylesheet" href="/dist/main.css" /> |
| 269 | + <script type="module" src="/dist/main.js"></script> |
| 270 | + <script> |
| 271 | + new EventSource("/esbuild").addEventListener("message", () => location.reload()); |
| 272 | + </script> |
| 273 | + </head> |
| 274 | + <body> |
| 275 | + <div id="root"></div> |
| 276 | + </body> |
| 277 | +</html> |
| 278 | +``` |
| 279 | + |
| 280 | +Create `src/main.tsx`: |
| 281 | + |
| 282 | +```tsx |
| 283 | +import { StrictMode } from "react"; |
| 284 | +import { createRoot } from "react-dom/client"; |
| 285 | +import App from "./App"; |
| 286 | + |
| 287 | +import "./index.css"; |
| 288 | + |
| 289 | +const root = document.getElementById("root"); |
| 290 | +if (!root) throw new Error("Root element not found"); |
| 291 | + |
| 292 | +createRoot(root).render( |
| 293 | + <StrictMode> |
| 294 | + <App /> |
| 295 | + </StrictMode>, |
| 296 | +); |
| 297 | +``` |
| 298 | + |
| 299 | +Create `src/App.tsx`: |
| 300 | + |
| 301 | +```tsx |
| 302 | +export default function App() { |
| 303 | + return ( |
| 304 | + <div className="flex min-h-screen items-center justify-center bg-gray-50"> |
| 305 | + <div className="w-full max-w-md rounded-lg bg-white p-8 shadow-lg"> |
| 306 | + <h1 className="mb-4 text-3xl font-bold text-gray-900">esbuild + React + Tailwind</h1> |
| 307 | + <p className="text-gray-600"> |
| 308 | + Welcome to your new project! This template is set up with esbuild for blazing-fast builds, React for UI |
| 309 | + components, and Tailwind CSS for styling. |
| 310 | + </p> |
| 311 | + </div> |
| 312 | + </div> |
| 313 | + ); |
| 314 | +} |
| 315 | +``` |
| 316 | + |
| 317 | +Create `src/index.css`: |
| 318 | + |
| 319 | +```css |
| 320 | +@import "tailwindcss"; |
| 321 | +``` |
| 322 | + |
| 323 | +### 2. Install Flowbite React |
| 324 | + |
| 325 | +Install Flowbite React: |
| 326 | + |
| 327 | +```bash |
| 328 | +npx flowbite-react@latest init |
| 329 | +``` |
| 330 | + |
| 331 | +This will: |
| 332 | + |
| 333 | +- Install Flowbite React and its dependencies |
| 334 | +- Configure Tailwind CSS to include Flowbite React plugin |
| 335 | +- Configure Vite to include Flowbite React plugin |
| 336 | + |
| 337 | +## Try it out |
| 338 | + |
| 339 | +Now that you have successfully installed Flowbite React you can start using the components from the library: |
| 340 | + |
| 341 | +```tsx |
| 342 | +// src/App.tsx |
| 343 | +import { Button } from "flowbite-react"; |
| 344 | + |
| 345 | +export default function App() { |
| 346 | + return ( |
| 347 | + <> |
| 348 | + <Button>Click me</Button> |
| 349 | + </> |
| 350 | + ); |
| 351 | +} |
| 352 | +``` |
| 353 | + |
| 354 | +<hr /> |
| 355 | + |
| 356 | +## Templates |
| 357 | + |
| 358 | +- [Github](https://github.com/themesberg/flowbite-react-template-esbuild) |
| 359 | +- [StackBlitz](https://stackblitz.com/edit/flowbite-react-template-esbuild) |
0 commit comments