Why Use Vite Over CRA?
» Why Vite instead of CRA?? š¤
-
Hi there,šš» In this article, let’s discuss about the advantages of Vite and why we should use it instead of create-react-app.
-
Vite is a Front-end tool that focuses on speed and performance.
-
Vite has several advantages such as its a lightning-fast build tool that lives up to its name, because its insanely quick and has a good development experience.
-
Vite also doesn’t bundle our entire project during development. Instead, it leverages ES module imports, serving dependencies on-demand. This means our development server can start up in a flash, and changes you make reflect almost instantly in the browser.
-
It has a Built-in Development Server which supports hot module replacement. Now you might ask what does that do? š¤
-
Ok, so answer to that is, it ensures that when you make changes to your code, the browser instantly updates, preserving our app’s state. It is like you have a live preview of your changes without hitting the refresh button again and again. It also supports VUE, PReact, Svelte and various other frameworks too.
-
Vite also supports plugins. You want to add typescript support, you can, or you want to integrate CSS preprocessor like sass, less, you can, or you want to optimise the images,you really can.š For each of these vite has a plugin. To use a plugin you just have to add them to your dev dependencies of our project and include them in viteās config file.
» Create a Project with VITE.
- Now we will build a simple vite + react js project and then deploy it to netlify by using netlify CLI. So for that letās open the terminal and in here letās run the command
node -v
pnpm -v
- If you donāt use pnpm its okay, just run the command
npm -v
- No that we have confirmed, that we do have node and pnpm on our local machine, letās create a vite + react app. We will not use npx create-react-app command, instead we will use:
pnpm create vite
- Then letās navigate to our project directory and open it into VS code and then there in the terminal run the command:
pnpm install
- Then it ask us to choose our framework so letās go with react and then after itās done letās open the package json file and in that you will have three scripts: Dev, Build and Preview.
-
Dev is for used to start the development server, because of which we will be able to see changes in our react application in real time without the need to reload the page.
-
And the build script is used for creating a production ready build. When we run this command vite will generate optimised and minified files which will be suitable for our deployment. This is script which we will use before deploying the application to a hosting service like netlify.
-
And last which is peview is used to locally preview the production build before deploying it. We do this to test and ensure that everything looks and works as expected in a production like environment. Its used for catching issue that might arise during our build process.
-
We will look at these commands one by one but before this letās try and understand how can we use environment variables in vite.
» How to use environment variables in vite?
- We first have to create a .env file in our root directory and in this file letās have two environment variables like:
VITE_API_URL=api.example.com
DB_PASSWORD=123456
-
If you notice we have API_URL env variable uses VITE as a prefix and our db password donāt use vite as a prefix. Letās see what happens.
-
And now if we want to access these variables what we can do is, we can use import.meta.env object provided by vite.
-
Letās open an App.jsx file and letās create two variables at the top letās have a variable named apiURL and another one named DB_PASSWORD and lets also bring in import.meta.env.VITE_API_URL
- And then down inside the jsx letās call the variables inside the curly braces.
- Inside the paragraph tag letās have a paragraph API key: and call the variable we just declared. Now save the file and run the command:
npm run dev
It will start on (http://localhost:3000/) and there we have our API_URL showing but now letās try and bring in the database password.
-
Want a spoiler? It wonāt work. š
-
Letās have a break line tag and and call the DB_PASS variable. Letās save the file and see, it does not gives us the database password because site does not recognize to.
-
So for it letās have vite as a prefix for our database password in .env file. Also have prefix vite for our database variable in the app.jsx file too. Now save the file and there see: now we have the db password too.
-
We can also create environment specific configuration files like .env.local or .env.production.
» Host site on netlify.
- Now letās host this site to netlify. For that we first will rub the command
pnpm build
- And there you will see that in the root direcotry the vite created a Dist folder which will have all the assets and files it needs. And then letās run the command
pnpm preview
-
And there you will find that the site now started the server on local host 4173. Now letās look at the preview of our site. Same as the local one but if there were any errors it would have shown us here.
-
Now to deploy it to the netlify we first need to install the netlify CLI
-
So for that run the command in the terminal:
npm install -g netlify-cli
-
-g = globally
-
It will ask for authorization, do that and next run the command:
ntl init
-
After it has logged into your netlify account. Then it will ask you to if you want to create a netlify site without a git repo, if you want select yes or no based on the your choice completely.
-
Then letās give a site name or leave it up top netlify and finally deploy the site run the command:
netlify deploy
-
And then it will ask you to provide the publishing directory so write Dist. And then letās hit enter
-
Then it will ask us to run the command:
netlify deploy --prod
-
Run the command and then netlify will provide you your site URL.
-
Ctrl plus click on it and there we have our site up and running. Pretty right?S
» Wrap up
-
And that is all you need to know about vite.
-
I hope you found this article helpful and if you did, then donāt forget to vite in your next project. Okay?
And thar’s it for this post. I hope it was helpful. Alright then Ill seeeee youuu in the next one, till then Bye Bye and happy coding! š„°