Vite is a frontend build tool that separates development and production workflows using native browser support for ES modules. It avoids JavaScript bundling during development, enabling fast cold starts and near-instant updates with Hot Module Replacement (HMR). For production, it leverages Rollup for bundling and tree-shaking. It is designed for frameworks like React, Vue, and Svelte, and includes support for TypeScript and JSX with minimal configuration.

Core Features of Vite

Native ESM Support

Vite serves unbundled .js and .ts files using native ES modules in browsers. This removes bundling at dev time, making hot reload almost instant.

Hot Module Replacement (HMR)

Vite implements HMR by sending only the affected modules during file changes, without refreshing the entire page or recompiling the whole app.

Optimized Build with Rollup

For production, Vite delegates bundling to Rollup. This gives tree-shaking, code-splitting, and plugin extensibility with minimal configuration.

TypeScript and JSX Support

Vite works with .ts, .tsx, and .jsx files. It doesn't transpile TypeScript (no type checking), but uses build to compile it quickly.

Project Initialization

To set up the basic project structure for a Vite frontend app with pre-configured support for selected frameworks and language variants, ensuring a consistent development environment.

To create a new Vite project:

code
npm create vite@latest

The CLI prompts for the project name, framework (e.g., React, Vue, Svelte), and variant (JavaScript or TypeScript). After setup:

code
cd project-name
code
npm install
code
npm run dev

The development server runs at http://localhost:5173 by default.

Default Project Structure

This separates configuration, static HTML, and source code to streamline development and build workflows. Each file has a defined role that supports fast development and optimized production builds. A Vite project follows this structure:

code
my-app/
code
├── index.html
code
├── vite.config.js
code
├── package.json
code
└── src/
code
├── main.jsx
code
└── App.jsx

Explanation:

  • index.html is the entry point and contains the root <script type="module">.
  • main.jsx mounts the root component into the DOM.

File-based Config and Setup

Uses a file-based configuration to manage build behavior, development server options, and plugin setup. This configuration is defined in vite.config.js or vite.config.ts for customizing how Vite handles your project’s framework integration and static assets. This centralized config file ensures consistent behavior across development and production environments.

code
import { defineConfig } from 'vite';
code
import react from '@vitejs/plugin-react';
code
export default defineConfig({
code
plugins: [react()],
code
});

Explanation:

  • defineConfig provides typing and structure for Vite config
  • @vitejs/plugin-react official plugin for JSX, HMR, and Fast Refresh in React apps.
  • react() initializes the plugin.
  • plugins: [react()] registers the React plugin with Vite’s plugin system.
  • export default: makes the configuration available to Vite CLI and server.

Using Plugins for Video Files

To copy video files into the production bundle or process them at build time, plugins like vite-plugin-static-copy can be used.

code
import { defineConfig } from 'vite';
code
import react from '@vitejs/plugin-react';
code
import viteStaticCopy from 'vite-plugin-static-copy';
code
export default defineConfig({
code
plugins: [
code
react(),
code
viteStaticCopy({
code
targets: [{ src: 'videos/*', dest: 'assets/videos' }],
code
}),
code
],
code
});

Explanation:

  • vite-plugin-static-copy: plugin that copies static files during build.
  • targets: defines source and destination for copy.
  • src: 'videos/*': includes all files inside the videos/ folder.
  • dest: 'assets/videos': places copied files inside dist/assets/videos.

Importing Static Video Assets

Static video files (.mp4, .webm, .vtt, etc.) can be placed in public/ for direct URL access, or imported using the module system.

code
import videoSrc from './assets/tutorial.mp4';

Explanation:

  • Imports the video as a module.
  • Vite processes the file and generates a hashed URL.
  • Used when referencing the video via a dynamic JS variable (e.g., src={videoSrc} in JSX).
code
<video controls width="100%">
code
<source src="/videos/tutorial.mp4" type="video/mp4" />
code
</video>

Explanation:

  • Directly references a statically served video file.
  • src points to a file inside public/videos or dist/videos (after using copy plugin).
  • Used when video is not imported in JavaScript.

Configuring Video Streaming with Environment Variables

For dynamic video URLs (e.g., Cloudflare Stream, api.video), environment variables can store base URLs or playback tokens.

code
VITE_STREAM_BASE_URL=https://video.example.com

Explanation:

  • Declares an environment variable to store the base URL for a video API or CDN.
  • Must be prefixed with VITE_ to be exposed to the frontend.

const baseURL = import.meta.env.VITE_STREAM_BASE_URL;

Explanation:

  • import.meta.env: Vite’s way to access environment variables at runtime.
  • VITE_STREAM_BASE_URL is read from .env and used in the app logic (e.g., to build video RLs).

TypeScript Integration for Video Components

Allows you to enforce typing for video-related components, ensuring predictable behavior and reducing runtime errors. Defining interfaces or types for props like `src`, `poster`, and `captions` maintains clear contracts between components and improves code maintainability when working with dynamic video content.

Use TypeScript to define strict types for video props:

code
type VideoProps = {
code
src: string;
code
poster?: string;
code
captions?: string;
code
};
code
const VideoPlayer = ({ src, poster }: VideoProps) => (
code
<video controls poster={poster}>
code
<source src={src} type="video/mp4" />
code
</video>
code
);

Explanation:

  • VideoProps defines a type for expected props: src is required, poster and captions are optional.
  • VideoPlayer is a functional component typed with VideoProps.
  • poster={poster} sets the preview image before playback.
  • <source> injects the dynamic video URL and specifies the video format.

Building and Previewing Production Output

To create a production build, run:

code
npm run build

Vite outputs static files to the dist/ directory using Rollup.

To preview the production output locally:

code
npm run preview

Serves the dist/ directory on a local server.