Introduction

React is a popular JavaScript library for building user interfaces used by many developers worldwide. One of the tools that can help developers build React applications more efficiently is a build tool.

What is a Build tool?

A build tool is a software that helps automate the process of building, testing, and deploying an application.

Several build tools are available for React, including Webpack, Parcel, and Rollup.

However, a newer build tool called Vite has recently gained attention for its unique approach to building React applications.

What is Vite?

Vite, or “Vite Is the Template Engine“, is a build tool designed explicitly for building modern web applications. It was developed by Evan You, the creator of Vue.js, and was released in June 2020.

Vite aims to be a fast and lightweight build tool that is easy to use and configure.

What makes Vite stand out from other build tools?

There are several ways in which Vite differs from other build tools for React. Here are some of the main differences:

Native ESM imports

As mentioned earlier, Vite uses native ESM imports to bypass the bundling step. This means that Vite can serve the source files directly to the browser, resulting in faster build times and smaller bundle size.

On the other hand, other build tools like Webpack and Parcel use a bundling process to compile the source code into a single bundle file. This can take longer, especially for large applications, and results in a larger bundle size.

Development server with hot module reloading

Vite includes a development server that automatically reloads the application when changes are made to the source code. This is known as Hot-Module Reloading, or Hot-Reloading, allowing developers to see their changes in real time without needing to refresh the browser manually.

Other build tools like Webpack and Parcel also have hot module reloading, but it is not built-in and requires additional configuration.

JSX and TypeScript Support

Vite supports JSX, the syntax extension used by React to write components out of the box. This means developers can use JSX in their React applications without additional configuration.

It does also support TypeScript, a popular programming language that adds optional static types to JavaScript. This makes it easier for developers to catch errors and prevent bugs in their code.

Webpack and Parcel also support JSX and TypeScript; however, they require additional configuration to enable these features.

If you haven’t yet worked with TypeScript in React, you shall not worry; we have an article that will walk you through the required steps in order to set up your React application using React. You can check it out here.

Straightforward configuration

Vite has a more straightforward configuration than other build tools like Webpack and Parcel. This is because it uses native ESM imports and includes a development server with hot module reloading, so developers don’t have to configure these features separately.

In comparison, Webpack and Parcel require more complex configurations to enable features like hot module reloading and support for JSX and TypeScript. This can be a drawback for developers who are new to building tools or want a simple and easy-to-use tool.

Using Vite with React

Now that we have a better understanding of what Vite is and how it differs from other build tools, let’s take a look at how we can set up our new React application.

To use it with React, we first need to install the package using npm or yarn:

  • npm: npm install -g vite
  • yarn: yarn global add vite

Next, we can create a new Vite project by running the following command:

vite create my-project

This will create a new project folder called my-project with the basic files and configuration needed to start a new Vite project.

Once the project is created, we can install the dependencies needed for a React application by running the following command:

  • npm: npm install react react-dom
  • yarn: yarn add react react-dom

Next, we can create a new React component by creating a file called App.js in the src directory and add the following code:

import React from 'react';

const App = () => {
  return (
    <div>
      Hello, world!
    </div>
  );
}

export default App;

Finally, we can update the index.html file in the public directory to include our React component by adding the following code:

<body>
  <div id="app"></div>
  <script type="module" src="/src/App.js"></script>
</body>

Now, we can start the Vite development server by running the following command:

vite

This will start the development server and open the application in the browser.

Any changes we make to the source code will be automatically reloaded in the browser thanks to Vite’s hot module reloading feature.

Conclusion

Vite is a fast and lightweight build tool specifically designed for building modern web applications. Its use of native ESM imports and a built-in development server with hot module reloading makes it a unique and efficient tool for building React applications.

While it may not have all the features of other build tools like Webpack and Parcel, its simplicity and ease of use make it an excellent option for developers who want a quick and easy way to build React applications.

I hope you’ve enjoyed the read and it helped you gain some insight into how to setup a new React application using Vite, as well as the benefits of doing so.

I’m looking forward to hearing your thoughts on this article, so feel free to leave a comment below.

Cheers!

👋 Hey, I'm Vlad Mihet
I'm Vlad Mihet, a blogger & Full-Stack Engineer who loves teaching others and helping small businesses develop and improve their technical solutions & digital presence.

💬 Leave a comment

Your email address will not be published. Required fields are marked *

We will never share your email with anyone else.