Use Vite for React Apps instead of CRA

nilanth

Nilanth

Posted on May 16, 2022

Use Vite for React Apps instead of CRA

Most of us will be using Create React App for creating React App. It supports all the configurations out of the box. But when your project code grows you might face higher build times, a slower start in the development server and waiting 2 to 5 secs to reflect the changes you have made in code and this might increase rapidly when the app grows on a larger scale.

This increases

  1. Development time, as we need to wait for 2 to 6 secs for each change.
  2. Production Build Time, it might take around 10 to 20 minutes to deploy a quick fix.
  3. Time, Time is money 🙂.

Why CRA dev server is slow?

CRA uses Webpack to bundle the code. Webpack bundles the entire code, so if your codebase is very large more than 10k lines you might see a slower start in the development server and a long waiting time to see the changes made.

bundler-image

As you can see in the above image, the Entire code is bundled to make the server ready for development.

How to make it faster?

Instead of using CRA for creating React App, we can migrate to Vite. Vite is the next generation frontend tooling to build the app faster.

Highlights of Vite

  1. On-demand file serving over native ESM, no bundling required!
  2. Hot Module Replacement (HMR) that stays fast regardless of app size.
  3. Out-of-the-box support for TypeScript, JSX, CSS and more.
  4. Pre-configured Rollup builds with multi-page and library mode support.
  5. Rollup-superset plugin interface shared between dev and build.
  6. Flexible programmatic APIs with full TypeScript typing.
  7. Supports React, Vue, Preact, Svelte.

How Vite is faster than CRA?

Vite uses esbuild which is written in Go and pre-bundles dependencies 10–100x faster than JavaScript-based bundlers.

Vite improves the development server start time by dividing the modules of an application into two categories: dependencies and source code.

Dependencies are mostly plain JavaScript that does not change often during development. Some large dependencies (e.g. AntD) are also quite expensive to process.

Source code often contains non-plain JavaScript that needs transforming (e.g. JSX, CSS or etc components), and will be edited very often. Also, not all source code needs to be loaded at the same time (e.g. with route-based code-splitting).

vite-build

As you see in the above image, Vite only needs to transform and serve source code on demand, when the browser requests it. Code-behind conditional dynamic imports are only processed if actually used on the current screen.

I have migrated an existing CRA based react app to Vite. Let’s compare the difference.

CRA Dev server start duration

cra-dev-server

CRA took 12s to start the development server. The sample app only contains 2 routes and 6 components. Let’s see the same using Vite

Vite Dev server start duration

vite-dev-server

Vite only took 298ms to start the dev server, It’s blazing fast when compared to CRA. As you can see a huge difference between the two tools. Let’s also compare the production build time of both.

CRA build duration

cra-build

CRA took 16.66s to build the app. Let’s see Vite's performance.

Vite build duration

Vite uses the same bundle approach for production build with Rollup, as using unbundled native ESM in production will cause extra HTTP requests.

vite-build

Vite with rollup took only 9.11s to build the entire app, Seems better compared to CRA. As it reduces the 40 to 50 percentage for build time when using Vite. This is more effective. For instance, if your current build time is 20 minutes, it will come down to 10 to 12 minutes when using Vite 🚀.

Not satisfied with the comparison check this tweet.

Hope you are thinking about how to migrate your current React CRA app to Vite?

It’s not a big deal! Let start over

Migration CRA to Vite

  • Remove the react-scripts from the package.json dependency.
  • Add sass in package.json, if you are using CSS or SCSS.
  • Add the below dependencies as a dev dependency
"devDependencies": {
  "@vitejs/plugin-react": "1.1.1",
  "vite": "2.7.0"
},
Enter fullscreen mode Exit fullscreen mode
  • Add the below commands to scripts
"scripts": {
  "start": "vite",
  "build": "vite build"
},
Enter fullscreen mode Exit fullscreen mode
  • Create a file vite.config.js in the root folder and add the below code

react() is used to avoid the manual import of React in .jsx and .tsx modules.

  • Move the index.html file outside public the folder.

  • Remove all the %PUBLIC_URL% from index.html

//From
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
//To
<link rel="icon" href="/favicon.ico" />
Enter fullscreen mode Exit fullscreen mode
  • Add the below script tag in the body of the index.html
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script> // Need to add
Enter fullscreen mode Exit fullscreen mode
  • Update your ENVs from REACT_APP to VITE as below
// From
REACT_APP_ENV = local
REACT_APP_HOST_UR = https://reqres.in/api/
// To
VITE_ENV = local
VITE_HOST_URL = https://reqres.in/api/
Enter fullscreen mode Exit fullscreen mode
  • Now just run npm install or yarn

  • Now run yarn start or npm start, Done!. Now our CRA app is migrated to Vite.

Notes:

If you face any issues in importing the components use the resolve alias.

New React App using Vite

Use the below command to create a fresh react app.

yarn create vite my-react-app --template react
Enter fullscreen mode Exit fullscreen mode

Reference

  1. Vite
  2. esbuild
  3. Rollup
  4. Vite preset templates

Conclusion

Vite seems very efficient, speeder and saves more time compared to CRA. Try Vite, you can see the difference.

Thank you for reading.

Get more updates on Twitter.

You can support me by buying me a coffee ☕

eBook

Debugging ReactJS Issues with ChatGPT: 50 Essential Tips and Examples

ReactJS Optimization Techniques and Development Resources

Twitter Realtime Followers Count

Twiter Stats

More Blogs

  1. Twitter Followers Tracker using Next.js, NextAuth and TailwindCSS
  2. Don't Optimize Your React App, Use Preact Instead
  3. How to Reduce React App Loading Time By 70%
  4. Build a Portfolio Using Next.js, Tailwind, and Vercel with Dark Mode Support
  5. No More ../../../ Import in React
  6. 10 React Packages with 1K UI Components
  7. 5 Packages to Optimize and Speed Up Your React App During Development
  8. How To Use Axios in an Optimized and Scalable Way With React
  9. 15 Custom Hooks to Make your React Component Lightweight
  10. 10 Ways to Host Your React App For Free
  11. How to Secure JWT in a Single-Page Application
💖 💪 🙅 🚩
nilanth
Nilanth

Posted on May 16, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related