avatarCan Durmus

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3115

Abstract

ns some additional configurations for projects and it is really simple and easy to configure. The other difference between these two is the build times and process. While bundling the app for development, CRA rebundles the whole app whenever there is a change in the code. In contrast, Vite bundles whole app in the beginning and rebundles only the files those are changed. This makes building process faster and faster.</p><p id="15a8">Let’s see both CRA and Vite in action.</p><p id="30aa">Firstly, I will create an app both in Vite and CRA to see which one will be created first. To create a Vite app, simply write <code>npm init @vitejs/app</code>. Then name the project and choose the framework as React.</p><figure id="3b17"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Snf3s9lECO1h88xo3JuEdA.png"><figcaption>React App created with Vite</figcaption></figure><p id="f3b1">Apart from <code>npx</code> installation, the project is setup in 4 seconds. Afterwards I ran <code>npm install</code> to install dependencies. This took 15 seconds. So, all in all, the project is setup in almost 20 seconds.</p><p id="55a7">In CRA, after typing <code>npx create-react-app</code> apart from <code>npx</code> installation, it took almost 180 seconds which is pretty long time to just setup the project. In the screenshot below, you can see a portion of log while setup in CRA.</p><figure id="f658"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1VDK0KRG9UKTQWK5qqXw3g.png"><figcaption>React App with CRA</figcaption></figure><h1 id="cf10">The Weak Sides of Vite</h1><p id="fa07">Yes, like every beautiful thing in world, Vite has some weak points. But I should say that it is not too much and fortunately, all of them are fixable. The first point that is different and weak from CRA is the importance of React in every JSX file. As you know, with CRA version 17, the importance of React is handled by CRA which means you don’t have to import React in every file. Unfortunately, in Vite, you still should import React in every JSX file. The path to handle this issue goes directly into <code>vite.config.js</code> file.</p><p id="8d8b">When you open the <code>vite.config.js</code> file, you will see that there is a preconfigured React plugin for Vite named <code>reactRefresh</code>. This plugin handles HMR updates in React apps but there is one more plugin that provides HMR and some more features like auto-importance of React in every JSX file. Install the plugin using <code>npm install --save-dev vite-preset-react</code> command. And then replace the previous plugin with this one as follows:</p><figure id="1054"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lYNaE3WCZsIm57zyKbVaLw.png"><figcaption></figcaption></figure><p id="53d8">After that, you don’t have to write <code>import React from 'react'</code> to every file.</p><p id="9258">The other issue I encountered was about the SVG files. While using CRA, I was able to import SVG files as React component using <code>import {ReactComponent as Pic from '../path'</code>. However, this is not the c

Options

ase for Vite but again, there is a way to overcome this. We should install one more plugin named <code>vite-plugin-svgr</code>. Install it using <code>npm install --save-dev vite-plugin-svgr</code> command. And then call it inside plugins array as follows:</p><figure id="a3e5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lhlpjkkHGQBnj6Kdhrr3gQ.png"><figcaption></figcaption></figure><p id="d573">Now you can use the exact syntax to import SVGs as CRA.</p><p id="8f3b">And that’s it! There are no more weak points. Everything works fast and seamlessly in Vite. I think you should give it a try Vite and experience the speed. You can find further info <a href="https://vitejs.dev/">here</a>.</p><figure id="8b34"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*TtX3EIdGpUqGCOW6LtO42Q.png"><figcaption>Subscribe to get your <b>free <i>subscriber-exclusive</i></b> story.</figcaption></figure><p id="140f">Thank you for reading. If you liked it, make sure you clap, and if you have anything to say about the article, leave a response. See you in the next article.</p><p id="5d20"><b><i>Further Reading</i></b></p><div id="ab5b" class="link-block"> <a href="https://readmedium.com/can-you-spot-the-bug-570ae92642a9"> <div> <div> <h2>Can You Spot The Bug?</h2> <div><h3>Do you trust your JavaScript skills? Then come and try whether you can find the bugs in these snippets.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*EYejWG11lB57_GwgkxiYaw.png)"></div> </div> </div> </a> </div><div id="369e" class="link-block"> <a href="https://readmedium.com/real-life-experience-on-jetbrains-new-ide-fleet-5dde41b90a75"> <div> <div> <h2>Real-Life Experience On JetBrains’ New IDE: Fleet</h2> <div><h3>JetBrains tries to keep up the competition against VS Code, with Fleet. Will it succeed or will it be forgotten under…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*LQrpdw04gWPgmvAzmpxMwA.png)"></div> </div> </div> </a> </div><div id="55db" class="link-block"> <a href="https://betterprogramming.pub/did-you-know-html-thinks-chuck-norris-is-a-color-9f67edf4c34"> <div> <div> <h2>Did You Know HTML Thinks Chuck Norris Is a Color?</h2> <div><h3>Weird, isn’t it? Here’s why</h3></div> <div><p>betterprogramming.pub</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*OGnLYvJgLpaW5tGWdzHOLw.png)"></div> </div> </div> </a> </div></article></body>

You Should Choose Vite Over CRA for React Apps, Here’s Why

For many beginners, intermediate and even for some expert programmers, Create React App is the key tool to start and maintain a React project because it provides many useful features such as HMR (Hot Module Replacement) and development server. However, it has a big drawback which is poor performance.

For almost two years, I used CRA for my React applications and I was happy because there were no configuration files and the build times were not a big deal for me because I was working on simple projects that contains 5,000 to 10,000 lines of code. However, when I tried to build something massive, like 25,000 lines of code, I started to get tired of waiting because when I started my project, the development server was getting ready in almost 5 minutes, and whenever I made a change in my code, I had to wait for almost 5 to 10 seconds to see the effects. These times are pretty huge when you are working a real-life project. I knew that it was not about my computer but CRA. That’s when I searched for some alternatives for CRA and found Vite.

Under the hood, CRA uses Webpack which is a popular asset bundler that helps us develop and build a web app. The Webpack was fine until 2–3 years ago but today, we have almighty Esbuild.

Formerly people were thinking that the tools to use for JavaScript should be written in JavaScript as Webpack written. Since JavaScript is an interpreted language and therefore it is kind of slow, it is not suitable for processing tools or programs. However, the perspective of the developers has changed and they tried to write a JavaScript tool using other low-level languages like Go. That’s how Esbuild was created. It is a JavaScript build tool that is written in Go and thus, it is lightning fast.

Esbuild is almost 120 times faster than Webpack

So, What’s Vite?

Vite, pronounced as /vit/, is a next generation frontend build tool built on top of Esbuild. It is created by Evan You who is the creator of Vue.js. It has a solid CLI that eases the process of project setup amazingly. It works really fast and it has many great features that CRA provides. Moreover, there are plugins that can be added and make development process more straightforward.

When CRA and Vite is compared there are plenty of differences in terms of experience and build times. In CRA, there is literally no configuration file for the build process, everything is super-simple. This makes life easy for small projects but when the project gets bigger, the build process should be configured according to the needs of the project. In contrast, Vite provides a vite.config.js file that contains some additional configurations for projects and it is really simple and easy to configure. The other difference between these two is the build times and process. While bundling the app for development, CRA rebundles the whole app whenever there is a change in the code. In contrast, Vite bundles whole app in the beginning and rebundles only the files those are changed. This makes building process faster and faster.

Let’s see both CRA and Vite in action.

Firstly, I will create an app both in Vite and CRA to see which one will be created first. To create a Vite app, simply write npm init @vitejs/app. Then name the project and choose the framework as React.

React App created with Vite

Apart from npx installation, the project is setup in 4 seconds. Afterwards I ran npm install to install dependencies. This took 15 seconds. So, all in all, the project is setup in almost 20 seconds.

In CRA, after typing npx create-react-app apart from npx installation, it took almost 180 seconds which is pretty long time to just setup the project. In the screenshot below, you can see a portion of log while setup in CRA.

React App with CRA

The Weak Sides of Vite

Yes, like every beautiful thing in world, Vite has some weak points. But I should say that it is not too much and fortunately, all of them are fixable. The first point that is different and weak from CRA is the importance of React in every JSX file. As you know, with CRA version 17, the importance of React is handled by CRA which means you don’t have to import React in every file. Unfortunately, in Vite, you still should import React in every JSX file. The path to handle this issue goes directly into vite.config.js file.

When you open the vite.config.js file, you will see that there is a preconfigured React plugin for Vite named reactRefresh. This plugin handles HMR updates in React apps but there is one more plugin that provides HMR and some more features like auto-importance of React in every JSX file. Install the plugin using npm install --save-dev vite-preset-react command. And then replace the previous plugin with this one as follows:

After that, you don’t have to write import React from 'react' to every file.

The other issue I encountered was about the SVG files. While using CRA, I was able to import SVG files as React component using import {ReactComponent as Pic from '../path'. However, this is not the case for Vite but again, there is a way to overcome this. We should install one more plugin named vite-plugin-svgr. Install it using npm install --save-dev vite-plugin-svgr command. And then call it inside plugins array as follows:

Now you can use the exact syntax to import SVGs as CRA.

And that’s it! There are no more weak points. Everything works fast and seamlessly in Vite. I think you should give it a try Vite and experience the speed. You can find further info here.

Subscribe to get your free subscriber-exclusive story.

Thank you for reading. If you liked it, make sure you clap, and if you have anything to say about the article, leave a response. See you in the next article.

Further Reading

React
JavaScript
Web Development
Webpack
Vitejs
Recommended from ReadMedium