Bytes
Web Development

What is Vite and Why to Use Vite JS?

Last Updated: 27th September, 2023
icon

Vibha Gupta

Technical Content Writer at almaBetter

Are you looking for a rapid development tool that can significantly improve your web project's speed and performance? Look no further than Vite.js. Read more!

Are you looking for a rapid development tool that can significantly improve your web project's speed and performance? Look no further than Vite.js. In this comprehensive step-by-step guide, we will explore the power and versatility of Vite, understand what is Vite and walk you through the process of creating your first Vite project.

What is Vite?

What is Vite - Vite.js is a cutting-edge development tool that focuses on speed and performance by enhancing the development experience. It leverages native browser ES imports, eliminating the need for a build process and improving compatibility with modern browsers. The tool consists of two main components: the dev server and the build command.

The Dev Server

The dev server in Vite.js provides support for Hot Module Replacement (HMR), allowing for seamless updates to modules during the execution of the application. Unlike traditional development servers that reload the entire application, Vite.js only updates the modified parts, significantly reducing development time and improving efficiency.

The Build Command

The build command in Vite.js enables developers to bundle their code using Rollup, a powerful and efficient bundling tool. The build command is pre-configured to output highly optimized static assets for production, resulting in faster page loading times and improved overall performance.

How Vite JS Works

Now that we have understood what is Vite, let us delve into how Vite works. Vite.js leverages the native support for ES modules in modern browsers. ES modules allow developers to use the import and export statements natively, improving code modularity and organization.

However, native ES modules require either relative or absolute URLs for imports, which can be problematic when working with third-party dependencies. Vite.js solves this issue by automatically detecting bare module imports and performing two key actions:

  • Pre-Bundling: Vite.js pre-bundles the source files, optimizing them for faster page loading times. It also converts CommonJS and UMD modules to ECMAScript Modules (ESM), ensuring compatibility with modern browsers.
  • URL Rewriting: To enable browsers to import modules without errors, Vite.js rewrites the imports to valid URLs. For example, it might transform an import like import { someMethod } from 'my-dep' to /node_modules/.vite/my-dep.js?v=f3sf2ebb.

These optimizations ensure that your Vite.js projects are efficient, compatible, and performant.

Why Use Vite JS?

Vite uses - After a guide to what is Vite, let us understand why should you use Vite. Now that we understand the inner workings of Vite.js, let's explore the reasons why you should consider using it for your next project.

1. Performance

Vite.js excels in terms of performance. By leveraging pre-bundling with ESbuild, Vite.js achieves speeds 10 to 100 times faster than other JS bundlers. The pre-bundling step, performed with esbuild, significantly improves Vite's cold start time, making it one of the fastest JavaScript-based bundlers available.

2. Hot Module Replacement (HMR)

One of the standout features of Vite.js is its seamless integration of Hot Module Replacement (HMR). HMR allows for real-time updates to your application without the need to reload the entire page. With HMR, only the modified sections of your application are loaded, preserving the application's state and saving valuable development time.

3. Configuration Options

Vite.js provides extensive configuration options, giving you more control over your project's setup. You can extend the default configuration by creating a vite.config.js or vite.config.ts file in your project's root directory. This allows you to customize various aspects of your project, such as plugins, asset handling, and more. Additionally, you can specify a different configuration file using the --config CLI option.

Getting Started with Vite JS - What is Vite.js?

Now that we understand what is Vite and its benefits let's dive into the process of creating a Vite project and running it on your local machine.

Prerequisites

Before we begin, ensure that you have the following software installed on your computer:

  • Node.js version 12.2.0 or higher
  • Npm or Yarn

Step 1: Creating a Vite React Project

To create a React Vite application, open your terminal and navigate to the desired folder where you want to save your project. Then, run the following command:

npm create @vitejs/app my-vite-app

Replace my-vite-app with your preferred name for the Vite application. This command initializes a new Vite project with the specified name.

Step 2: Installing Dependencies - How to install Vite?

Vite installation - Once the project initialization is complete, navigate to the project folder by running the following command:

cd my-vite-app

Now, it's time for installing vite and the project dependencies using either npm or yarn. Run the following command to install the necessary dependencies:

npm install

This command will fetch and install all the required packages specified in the project's package.json file.

Step 3: Running the Vite Application

Create React app with Vite - With the dependencies installed, you can now start the development server and run your Vite application. In the project folder, run the following command:

npm run dev

This command will start the development server, and you should see output similar to the following:

Vite v2.7.14 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 192ms.

Open your browser and navigate to http://localhost:3000/. You should see your Vite application running successfully.

Understanding the Vite Folder Structure

To effectively work with Vite.js, it's essential to understand the structure of a Vite project and create React app using Vite. Let's look closer at the organization of folders and files within a Vite application.

The node_modules Folder

The node_modules folder contains all the project's dependencies specified in the package.json file. These dependencies are automatically downloaded and stored in this folder when you run the npm install command. It's important to note that you don't need to push the node_modules folder to version control systems like Git, as other users can install the necessary dependencies through the package.json file.

The src Folder

The src folder is where you'll spend most of your time developing your Vite application. It contains essential files such as app.jsx, main.jsx, app.css, and index.html.

  • app.jsx: This file serves as the base component and acts as a container for all other components used in the application. You can modify this file to add or remove components as needed.
  • main.jsx: The main.jsx file is responsible for targeting the root element (usually with the ID root) in the index.html file and rendering all the components used in the application.
  • app.css: The app.css file contains the CSS styles used in the application. You can modify this file or add your own CSS files to customize the styling of your Vite application.

The index.html File

The index.html file serves as the entry point for your Vite application. It contains the basic HTML structure and includes the necessary scripts and stylesheets to run your application. Vite automatically rebases all URLs inside the index.html file to ensure proper functioning.

Conclusion

In this comprehensive step-by-step guide, we explored the power and versatility of Vite.js. We learned how Vite.js leverages native browser ES imports to improve development speed and performance. We also discussed the benefits of using Vite.js, such as its exceptional performance, Hot Module Replacement (HMR) capabilities, and extensive configuration options.

We then delved into the process of creating a Vite project, installing dependencies, and running the application using the development server. Finally, we examined the folder structure of a Vite application, highlighting the importance of the src folder and its key files.

Now that you have a solid understanding of Vite.js and how to use it, you can embark on your own journey of building lightning-fast and efficient web applications. To delve deeper into Vite.js and explore its full potential, refer to the official Vite.js documentation.

Start harnessing the power of Vite.js today and revolutionize your web development experience!

Related Articles

Top Tutorials

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter