Building a Production Ready React Vite TypeScript Boilerplate
Amandeep Singh
Posted on November 25, 2024
Overview
As developers, we often find ourselves setting up the same tools and configurations when starting new projects. To address this challenge, I've created a fully type-safe React + Vite boilerplate/starter that serves as a solid foundation for production-ready web applications which scales well.
This template emerged from a need to standardize new projects at work, focusing on developer experience, code quality, and scalability. Let me walk you through what makes this boilerplate special and how you can use it for your next project.
Key Features
π Lightning-fast development with Vite
πͺ Full TypeScript support
ποΈ Well organized project structure
π Includes JWT authentication implementation
π‘ Modern data fetching with Tanstack React Query
ποΈ State management using Zustand
π£οΈ Type-safe and File based routing with Tanstack Router
π Internationalization support (i18n)
π Form handling with React Hook Form + Zod
π¨ UI components with Tailwind CSS + shadcn/ui
π Component documentation using Storybook
β
Comprehensive testing setup with Vitest + Cypress + MSW
Requirement
- Node.js 20+
- npm
- git
Getting Started
Installation
# Clone the repository
git clone https://github.com/singhAmandeep007/react-vite-boilerplate.git
# cd into folder
cd react-vite-boilerplate
# Removing the .git folder (and any additional files, folders or dependencies you may not need)
rm -rf .git
# Install dependencies
npm install
# Run the setup script (initializes git repository and husky)
npm run prepare
# Start development server, yeah that's all
npm run dev
Project Structure
βββ .storybook
βββ cypress
β βββ downloads
β βββ fixtures
β βββ specs
β βββ support
| βββ tsconfig.json
βββ env
βββ public
βββ src
β βββ __mocks__
β βββ app
β β βββ auth.ts
β β βββ App.ts
β β βββ router.ts
β βββ assets
β βββ api
β β βββ auth
β β βββ posts
β β βββ user
β β βββ apiService.ts
β βββ components
β β βββ forms
β β βββ hooks
β β βββ layout
β β βββ ui
β β βββ developmentTools.tsx
β βββ config
β βββ mocker
β βββ modules
β β βββ i18n
β βββ pages
β β βββ app
β β βββ home
β β βββ auth
β βββ routes
β βββ store
β β βββ auth
β βββ tests
β βββ types
β βββ utils
β βββ index.css
β βββ main.tsx
βββ index.html
βββ .editorconfig
βββ eslint.config.js
βββ prettier.config.js
βββ cypress.config.js
βββ tailwind.config.js
βββ .gitignore
βββ tsconfig.json
βββ vite.config.ts
βββ package.json
Key folders and files with purpose
Root Directory
-
.storybook/
: Contains configuration files for Storybook, a popular tool for building UI components and testing them in isolation. -
cypress/
: Holds end-to-end testing configurations and files.-
downloads/
: For storing temporary downloads during tests. -
fixtures/
: Mock data for testing. -
specs/
: Test cases for Cypress. -
support/
: Custom commands and reusable configurations. -
tsconfig.json
: TypeScript configuration specific to Cypress.
-
-
env/
: Environment-specific files (e.g., .env.development, .env.production). -
public/
: Publicly accessible assets, such as images or static files. -
index.html
: Entry point for the application. - Configuration files for linting, formatting, and build tools:
-
.editorconfig
,eslint.config.js
,prettier.config.js
-
tailwind.config.js
: Tailwind CSS configuration. -
vite.config.ts
: Vite configuration. - tsconfig.json: TypeScript compiler configuration.
-
src Directory
The src folder contains the core of the application:
-
__mocks__/
: Mock files for testing purposes. -
app/
: Application-level configurations, such as providers (e.g., for React Query, Router, etc.). -
api/
: Directory is organized into folders of related API endpoints. -
assets/
: Stores static resources that are part of application. -
components/
:-
forms/
: Reusable form components like input fields or buttons. -
hooks/
: Custom React hooks. -
layout/
: Components related to the app's layout (headers, footers). -
ui/
: Reusable UI components (buttons, cards). -
developmentTools.tsx
: Tools or utilities for local development, such as react query devtools.
-
-
config/
: Configuration files, such as API base URLs or environment settings. -
mocker/
: For mocking API responses or other utilities during development. -
modules/
:-
i18n/
: Internationalization configurations and translation files.
-
-
pages/
: Top-level pages, often aligned with routes.-
App/
: Pages related to the main application (dasboard, settings). -
Home/
: Homepage or landing page. -
Auth/
: Authentication-related pages (login, signup).
-
-
routes/
: File based Route configuration for the app, using Tanstack router. -
store/
: Global state management, organized by domain.-
auth/
: State management logic for authentication.
-
-
tests/
: Utility functions and wrappers for unit testing. -
types/
: Shared TypeScript types and interfaces. -
utils/
: Reusable utility functions and helpers. -
index.css
: Global CSS or Tailwind imports. -
main.tsx
: Application entry point, initializing React and rendering the app.
Authentication Implementation
The boilerplate includes a complete JWT authentication setup demonstrating best practices:
Data Flow
- API requests using ky for modern HTTP client capabilities. Ky allows modification the request lifecycle using hooks.
- Request/response handling with React Query for caching and synchronization
- State management through Zustand for auth status and user data
- Protected routes implementation with Tanstack Router
Testing
The boilerplate comes with a comprehensive testing setup:
- Unit Tests: Vitest + React Testing Library
- E2E Tests: Cypress
- API Mocking: MSW (Mock Service Worker)
Unit Testing
npm run test:unit
# or with coverage
npm run test:unit:coverage
# or in watch mode
npm run test:unit:watch
# or in ui mode
npm run test:unit:ui
Testing types is also supported with vitest and this application is set up to run tests for types using it. By default all tests inside *.test-d.ts
files are considered type tests.
End-to-End (E2E) Testing
npm run test:e2e
# or in headless mode
npm run test:e2e:headless
Development Tools
- Dev tools
- @tanstack/react-query-devtools - Dedicated dev tools to help visualize the inner workings of React Query
- @tanstack/router-devtools - Dedicated dev tools to help visualize the inner workings of TanStack Router
- @hookform/DevTools - React Hook Form Devtools to help debug forms with validation
-
Code Quality
- ESLint for code linting
- Prettier for code formatting
- husky for git hooks
- commitizen + devmoji for standardized commit messages
-
Pre-commit Checks
- The boilerplate automatically runs the following checks before each commit:
- Linting
- Type checking
- Format checking
Deployment
The project includes GitHub Actions workflows for:
-
Continuous Integration
- Running e2e tests
- Code quality checks
-
Continuous Deployment
- Automatic deployment to production environments
Conclusion
This boilerplate aims to provide a solid foundation for your React projects while maintaining flexibility. It incorporates modern tools and best practices without being overly opinionated, allowing you to adapt it to your specific needs.
What's Next?
- Clone the repository
- Check out the demo implementation
- Customize it for your project
- Provide feedback or contribute
Remember one design doesn't fit all, so feel free to customize it according to your needs.
Your feedback and contributions are always welcome! Feel free to open issues or submit pull requests on GitHub.
Resources
Feedback and questions are always welcome and I appreciate anyone willing to checkout this project.
Goodluck shipping you next project.
Posted on November 25, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 13, 2024
September 5, 2024