Step-by-Step Monorepo Tutorial: Turbo Repo, Vite, Next, Tailwind, and Storybook
Table of Contents
Introduction
In this tutorial, we'll create two React applications using Turbo Repo and a monorepo architecture. We'll utilize Vite and Next.js as our front-end frameworks, and incorporate Tailwind CSS for styling, along with Storybook for documentation. Additionally, we will bundle a UI library with tsup for sharing components across our applications. This setup will streamline development and improve productivity, whether you're a beginner or an experienced developer.
Step 1: Set Up Turbo Repo
- Open your terminal.
- Run the following command to create a new Turbo Repo:
npx create-turbo@latest - Name your Turbo Repo (e.g.,
turbo-demo) and chooseyarnas the package manager. - Once the project is created, navigate into the project directory:
cd turbo-demo
Step 2: Explore the Folder Structure
- The project contains three main directories:
- apps: Contains all the applications (initially includes Next.js apps).
- packages: For shared configurations and components.
- node_modules: Contains all the dependencies for the entire project.
- Open the
package.jsonto check the workspaces configuration.
Step 3: Create UI Library
- Inside the
packagesfolder, create a new folder for the UI library (e.g.,ui). - Inside the
uifolder, create apackage.jsonwith the following structure:{ "name": "@your-scope/ui", "version": "0.1.0", "main": "index.js" } - Create a button component (e.g.,
Button.tsx) in theuifolder.
Step 4: Set Up Next.js App
- In the
appsdirectory, delete thedocsfolder since we will create our own apps. - Create a new Next.js app:
yarn workspace web create next-app - Navigate to the
webapp and install Tailwind CSS:cd web yarn add -D tailwindcss postcss autoprefixer - Generate the Tailwind configuration files:
npx tailwindcss init -p
Step 5: Configure Tailwind CSS
- Update
tailwind.config.jswith:module.exports = { content: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'], theme: { extend: {}, }, plugins: [], }; - Add Tailwind directives in a global CSS file (e.g.,
styles/global.css):@tailwind base; @tailwind components; @tailwind utilities;
Step 6: Set Up Vite App
- In the
appsdirectory, create a new Vite app:yarn create vite@latest vite-app --template react-ts - Navigate to the
vite-appfolder and install Tailwind CSS as before.
Step 7: Configure Storybook
- In the
appsdirectory, create a new Storybook app:npx sb init --type react - Install the UI library as a dependency in the Storybook app's
package.json.
Step 8: Bundle UI Library with tsup
- Inside the
uifolder, installtsup:yarn add -D tsup - Create a
tsup.config.tsfile in theuifolder with:import { defineConfig } from 'tsup'; export default defineConfig({ entry: ['src/index.ts'], format: ['cjs', 'esm'], dts: true, sourcemap: true, }); - Update the
uiapp'spackage.jsonwith build scripts:"scripts": { "build": "tsup", "dev": "tsup --watch" }
Conclusion
In this tutorial, we set up a monorepo using Turbo Repo with two applications built on Next.js and Vite, integrated Tailwind CSS for styling, and created a shared UI library. We also configured Storybook for UI component documentation and bundled our library with tsup. This architecture not only enhances modularity but also improves the efficiency of managing multiple applications. Next steps could involve expanding the UI library or adding more applications to the monorepo.