Key Concepts
Vite, Expo, EAS (Expo Application Services), API routes, SPA (Single Page Application), Static Site Generation (SSG), Server-Side Rendering (SSR), CommonJS, esbuild, package.json, build scripts, deployment, client-server architecture, routes.json, TypeScript.
Creating a Vite App and Deploying with EAS
-
Project Setup:
- Create a new Vite application using
npm create vite. The application is named "jur-vite". - Navigate into the project directory:
cd jur-vite. - Install dependencies using
npm install. - Run the development server using
npm run devto ensure the base application is working.
- Create a new Vite application using
-
Building the Vite App:
- Build the application for production using
npm run build. This creates adistdirectory containing static assets.
- Build the application for production using
-
Deploying to Expo Application Services (EAS):
- Install the EAS CLI globally:
npm install -g eas-cli. - Log in to EAS using
eas login. - Deploy the application using
eas deploy. - During the deployment setup, create a new EAS project and name it "jur-vite-expo-app".
- The deployment process provides a URL where the deployed application can be accessed.
- Install the EAS CLI globally:
Building an Expo App with API Routes
-
Creating an Expo App:
- Navigate back to the parent directory.
- Create a new Expo application using
npx create-expo-app. The application is named "jur-expo". - Navigate into the project directory:
cd jur-expo. - Install dependencies using
pnpm install. - Start the Expo development server using
pnpm web.
-
Building and Deploying the Expo App:
- Build the Expo app for the web platform using
npx expo build:web. - Deploy the application using
eas deploy. - Create a new EAS project and name it "j-her-expo".
- The deployment process provides a URL for the deployed Expo application.
- Build the Expo app for the web platform using
-
Adding an API Route to the Expo App:
- Create a new file named
hello.tsinside theappdirectory. This file will define the API route. - The
hello.tsfile exports a default asynchronous function that handles the request and sends a JSON response:
import { Request, Response } from 'express'; export default async function handler(req: Request, res: Response) { res.status(200).json({ name: 'Hello' }); }- Rename
hello.tsto+api.tsto mark it as an API route. - Modify the
expo.config.jsfile to change the web configuration fromstatictoserverto enable server-side processing of API routes:
web: { bundler: 'metro', output: 'server', }, - Create a new file named
-
Deploying the Expo App with the API Route:
- Deploy the updated application using
eas deploy. - Access the API route by navigating to
/helloon the deployed application's URL.
- Deploy the updated application using
Replicating Expo's API Route Functionality in a Vite App
-
Restructuring the Vite App's
distDirectory:- Modify the
package.jsonfile to include custom build scripts. - Rename the original
buildscript tobuild:vite. - Create a new
buildscript that executesbuild:viteand then restructures thedistdirectory. - The restructuring involves creating
clientandserverdirectories withindist. - Move the static assets (JavaScript bundle, images, fonts, SVG files) into the
dist/clientdirectory. - Move the
index.htmlfile into thedist/serverdirectory.
"scripts": { "dev": "vite", "build:vite": "vite build", "build:client": "mkdir -p dist/client && mv dist/assets dist/client && mv dist/vite.svg dist/client", "build:server": "mkdir -p dist/server && mv dist/index.html dist/server", "build": "npm run build:vite && npm run build:client && npm run build:server" } - Modify the
-
Creating the
routes.jsonFile:- Create a directory named
api-assetsat the root of the project. - Inside
api-assets, create a file namedroutes.json. This file will define the routing configuration for the server. - For the initial setup, the
routes.jsonfile includes a route for theindex.htmlfile:
{ "routes": [ { "src": "^/$", "dest": "/index.html" } ] } - Create a directory named
-
Copying the
routes.jsonFile to thedistDirectory:- Add a new build script to copy the
routes.jsonfile into thedist/server/expodirectory.
"build:expo": "mkdir -p dist/server/expo && cp api-assets/routes.json dist/server/expo/routes.json", "build": "npm run build:vite && npm run build:client && npm run build:server && npm run build:expo" - Add a new build script to copy the
-
Deploying the Restructured Vite App:
- Deploy the updated application using
eas deploy. - Verify that the application is still accessible at the deployment URL.
- Deploy the updated application using
-
Adding an API Route to the Vite App (Backend):
- Create a directory named
api-sourceat the root of the project. - Inside
api-source, create a file namedhello.ts. This file will define the API route handler. - The
hello.tsfile exports an asynchronous function that handles the request and sends a JSON response:
import { Request, Response } from 'express'; export async function get(req: Request, res: Response) { res.status(200).json({ name: 'Hello from Vite!' }); } - Create a directory named
-
Compiling the TypeScript API Route to CommonJS:
- Install
esbuildas a development dependency:npm install -D esbuild. - Add a new build script to compile the TypeScript files in the
api-sourcedirectory into CommonJS files in thedist/server/expodirectory.
"build:api": "esbuild api-source/*.ts --bundle --format=cjs --platform=node --outdir=dist/server/expo --tsconfig ./tsconfig.json", "build": "npm run build:vite && npm run build:client && npm run build:server && npm run build:expo && npm run build:api" - Install
-
Updating the
routes.jsonFile to Include the API Route:- Modify the
routes.jsonfile to include a route for the new API endpoint:
{ "routes": [ { "src": "^/hello$", "dest": "/hello.js" }, { "src": "^/$", "dest": "/index.html" } ] } - Modify the
-
Deploying the Vite App with the API Route:
- Deploy the updated application using
eas deploy. - Access the API route by navigating to
/helloon the deployed application's URL.
- Deploy the updated application using
-
Adding a Second API Route (Pokemon):
- Create a new file named
pokemon.tsinside theapi-sourcedirectory. This file will fetch data from the PokeAPI and return it as a JSON response.
import { Request, Response } from 'express'; export async function get(req: Request, res: Response) { const response = await fetch('https://pokeapi.co/api/v2/pokemon'); const data = await response.json(); res.status(200).json(data); }- Update the
routes.jsonfile to include a route for the new/pokemonAPI endpoint:
{ "routes": [ { "src": "^/hello$", "dest": "/hello.js" }, { "src": "^/pokemon$", "dest": "/pokemon.js" }, { "src": "^/$", "dest": "/index.html" } ] } - Create a new file named
-
Updating the Vite App (Frontend) to Consume the API:
- Modify the
App.tsxfile to fetch data from the/pokemonAPI endpoint and display the list of Pokemon names. - Use
useStateto store the list of Pokemon names anduseEffectto fetch the data when the component mounts. - Update the UI to display the list of Pokemon names.
- Remove the contents of
App.cssand updateindex.cssfor basic styling (black and white color scheme).
- Modify the
-
Deploying the Complete Application:
- Deploy the final version of the application using
eas deploy. - Verify that the application fetches and displays the Pokemon data from the API endpoint.
- Deploy the final version of the application using
EAS Hosting Features
- Overview: Provides insights into application usage and performance.
- Deployments: Lists all deployments with associated details.
- Hosting: Manages hosting configurations, including custom domains.
- Production Deployments: Supports production-ready deployments.
- Custom Domains: Allows the use of custom domain names for deployed applications.
- Unified Platform: EAS provides a unified platform for hosting web applications, iOS binaries, and Android binaries.
Conclusion
The video demonstrates how to replicate Expo's API route functionality in a Vite application by restructuring the project, creating a routes.json file, and using esbuild to compile TypeScript API routes to CommonJS. This allows developers to leverage EAS for hosting Vite SPAs with serverless API endpoints, similar to how Expo handles API routes for web applications. The process involves modifying build scripts, creating a client-server architecture in the dist directory, and configuring routing through the routes.json file. The final result is a Vite application that can fetch data from custom API endpoints deployed on EAS.
AI summaries can miss context or contain errors. Check important details against the original video.