DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. How Use and Deploy Vite With React

How Use and Deploy Vite With React

Simple Tutorial about project with Vite with React and deploy this project

Evgenii Kravtsov user avatar by
Evgenii Kravtsov
·
Dec. 25, 22 · Tutorial
Like (2)
Save
Tweet
Share
3.74K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will look at how to make a simple project using Vite and React. As a bonus, I will show you how to deploy our project.

First, let's create a folder for our project and start creating a project using Vite and its templates.

Shell
 
mkdir ViteProject

cd ViteProject

npm create vite@latest


Vite will prompt us to choose the name of our project, the template under which it will make it, and the compiler with which it will work. For our example, I chose the following options:

shell


That's all, our project is created! 

Let's study the structure of the created project:


vite react


  • node_modules - respectively modules used in our project 
  • public - directory with media files we use 
  • src - directory with the project code, where we will add our code and components 
  • .gitignore - file to describe ignored files for git
  • index.html - HTML file where our React application is mounted 
  • package-lock.json - a file for describing installed dependencies 
  • package.json - a file for describing the project and dependencies, script commands and other things vite.config.json - default vite config file

Let's run our project and see what we have now. 

Run the command in the terminal

npm run dev

In the terminal, we will see

terminal


In this image, we are told how much Vite has collected for our project and at what address we can open it.

 After opening the address http://localhost:5173/ we will see the standard Vite template for React, namely:

Vite + React


Yeah! our project is created and launched and we can further develop it!

Let's take a look at the Vite configuration and see how it's done.

JavaScript
 
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
})


We see that it is quite simple, and does not look like a similar webpack configuration. In this case, the Vite plugin for React is used, which uses esbuild and babel. You can read more detailed information about this plugin on the page of this plugin.

Support for Sass and Tailwind

With Vite, we can easily use Sass and Tailwind as they are supported out of the box!

I renamed the App.css file to App.scss and added an inline style for the h1.


App.css

But so far, after rebooting, we get this error

terminal


This is because we didn't install sass in our dependencies. 

We need to execute this command

npm install sass --save-dev

And after that, we see that the style has been applied to our h1 tag!


Vite + React


As for Tailwind, we also need to go through only the installation of Tailwind itself, namely:

  • instal tailwind css - npm install -D tailwindcss postcss autoprefixernpxtailwindcss init -p
  •  Configure your template paths - in file tailwind.config.cjs
JavaScript
 
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}


  • Add in index.css tailwind directives tailwind directives


That's all you need to use Tailwind!

Deploy

In order to deploy our site, you first need to build it. 

This is done with the command

npm run build

The result of the Vite build is added to the dist directory

terminal


For subsequent testing of the built site, we can use the command

npm run preview

For deployment, we need to install Vercel CLI

npm i -g vercel

and run command

vercel

You will be asked to log in and asked a series of questions about your project and that's it!

terminal


Our project - Vite React Vercel App

Conclusion

In conclusion, I would like to say that in this article we have analyzed a simple way to create a project, its configuration and deployment. This is just an overview article on the topic. Hope it helps you and teaches you something!

JavaScript React (JavaScript library) Visual Studio Code

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Comparing Map.of() and New HashMap() in Java
  • Create Spider Chart With ReactJS
  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Introduction to Spring Cloud Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: