DEV Community

Cover image for Upgrade Your React App with Vite: Faster Builds, Real-Time ESLint Feedback, and Cleaner Files
frank edekobi
frank edekobi

Posted on

Upgrade Your React App with Vite: Faster Builds, Real-Time ESLint Feedback, and Cleaner Files

What is this tutorial about?

This guide will walk you through setting up Vite for a faster and simpler development experience. We'll also configure ESLint to help catch issues as you code. Finally, we'll clean up unnecessary files from your project and organize the structure.

Why go through all this work?

Switching to Vite speeds up and simplifies development. It updates changes instantly and keeps everything lightweight, with less setup and fewer headaches. Vite is perfect for projects with TypeScript, React, or Vue.

Who is this tutorial for?

This guide is for developers who are used to Create React App or Webpack and want to try out a faster, simpler build tool without changing much code.

Introduction
Vite helps make your project run faster and easier to manage. Here are the key reasons:

  • Fast Reloads: Vite instantly updates your project as you code.
  • Less Setup: Vite has a simpler configuration, so there are fewer files to manage.
  • Better Performance: Vite is optimized for speed, both in development and when building for production.

Let's begin

Step 1: Moving to Vite

Install Vite: Open your terminal, go to your project folder, and run:

npm install vite @vitejs/plugin-react --save-dev
Enter fullscreen mode Exit fullscreen mode

or

yarn add -D vite @vitejs/plugin-react
Enter fullscreen mode Exit fullscreen mode

Move index.html: In a Vite project, index.html should be in the root of the project (not in the public folder). Move it by:
Cutting index.html from the public folder.
Pasting it directly into your main project folder.

Edit index.html: In the moved index.html file, add the script tag with this:

<script type="module" src="/src/index.tsx"></script>
Enter fullscreen mode Exit fullscreen mode

This tells Vite to load your app's code from src/index.tsx, so it knows where to start.

Also, replace %PUBLIC_URL% with / in the index.html file, as Vite doesn't use this syntax. Here's how:

Open index.html: Go through the index.html file and look for any occurrences of %PUBLIC_URL%.

Replace %PUBLIC_URL% with /: For example, if you see something like this:

<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
Enter fullscreen mode Exit fullscreen mode

Change it to:

<link rel="icon" href="/favicon.ico" />
Enter fullscreen mode Exit fullscreen mode

Update Package Scripts: Open package.json and replace your old start, build, and test scripts with Vite's commands:

"scripts": {   
   "dev": "vite",   
   "build": "vite build",   
   "serve": "vite preview" 
}
Enter fullscreen mode Exit fullscreen mode

Create a Basic Vite Configuration File: In the root of your project, create a vite.config.js file and add the following:

import { defineConfig } from 'vite'; 
import react from '@vitejs/plugin-react';  

export default defineConfig({   
  plugins: [react()] 
})
Enter fullscreen mode Exit fullscreen mode

This file sets up Vite to work with React.

Step 2: Setting Up ESLint for Real-Time Code Feedback

To check your code for mistakes as you work, follow these steps to set up ESLint:

Install ESLint and Plugins: Run the following in your terminal:

npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin vite-plugin-eslint --save-dev
Enter fullscreen mode Exit fullscreen mode

or

yarn add -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin vite-plugin-eslint 
Enter fullscreen mode Exit fullscreen mode

Add ESLint Configuration: Create an .eslintrc.js file in your root folder and add this configuration:

module.exports = {   
   parser: '@typescript-eslint/parser',   
   plugins: ['@typescript-eslint'],   
   extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:react/recommended'  ],   
   rules: {     // Add any additional ESLint rules here   } 
}
Enter fullscreen mode Exit fullscreen mode

Link ESLint with Vite: Open your vite.config.js file again and add the ESLint plugin:

import { defineConfig } from 'vite'; 
import react from '@vitejs/plugin-react';
import eslint from 'vite-plugin-eslint';  

export default defineConfig({   
  plugins: [react(), eslint()], 
})
Enter fullscreen mode Exit fullscreen mode

Run Your Dev Server with ESLint: Now, start your development server:
ESLint should show any issues in the terminal or your code editor.

Step 3: Cleaning Up Unnecessary Files

After switching to Vite, you can remove the following files:
Webpack Config Files: webpack.config.js or similar files related to Webpack setup.

  • Babel Configuration Files: If you're not using Babel, you can delete .babelrc or babel.config.js.
  • Unused public Folder Content: Since index.html is now in the root, you can delete it from the public folder. If you have other files in public you don't need anymore, clean them out.

Step 4: Test Your Vite Setup

To check everything is working:
Start Development Server:

npm run dev
Enter fullscreen mode Exit fullscreen mode

or

yarn dev
Enter fullscreen mode Exit fullscreen mode

This will open the app, and you'll see updates instantly when you make changes.

Wrapping Up

You're now ready to use Vite for faster development! With ESLint, you can catch issues as you code, and the cleanup removes unnecessary files. Enjoy your improved development setup!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay