More on Vite
After scaffolding your React application with Vite there are a couple of things to cover in regards to the structure of the project directory and the default Vite config file.
Initial Project Structure
index.html
the index.html file resides within the root folder of the application. You may have worked with different tools that scaffold the index.html file into the public directory.
With Vite, the index.html is the entry point for the entire application. Lets take a look at what is inside of the index.html file below:
 |  | 
The above code block should certainly look familiar to boilerplate html starter code. Looking specifically at line 11:
<script type="module" src="/src/main.jsx"></script>This line references any JavaScript code within your application.
/src Directory
The /src directory has the following default structure when creating a new React application:
  graph TD
    subgraph src
      subgraph assets
        react.svg
      end
      App.css
      App.jsx
      index.css
      main.jsx
    end
Lets discuss the files and what they are responsible for:
The
main.jsxfile renders theApp.jsxcomponent.NoteOften times you may notice that an application has a
main.jsxorindex.jsxfile. These are used interchangeably. You may notice that apps you work on in upcoming sections have anindex.jsxfile instead of amain.jsxfile.The
App.jsxcomponent is used to call yourReactcomponents and handle your applications routing.The
App.cssfile is styling specifically related to theReactapplication. Syling within theApp.cssfile are typically used for individual components.The
index.cssfile is styling file that is globally related to your entire application. If there is styling that a user wants to be used globally across the entire application and all components this is where that styling would live.
Vite Config File
When you are starting your server from the command line, Vite will automatically resolve the config file called vite.config.js within the root directory of the project. This file is included upon scaffolding a React project with Vite. The main purpose of this file is to define the or customize how you want to serve or build your application. You can define environment variables here if you have sensitive or specific information you need to provide for the application to build.
Let’s take a look at the config file below:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
})This is the default vite.config.js file included with every project. It is customizable should it not suit your development needs but we will cover the default configuration.
It starts by importing the defineConfig function from the vite package. The second line imports the react plugin to allow React support within your project. It then exports the default configuration object (defineConfig) that was imported above, with the react plugin that enables your React code to run.
While you begin to work more with Vite and React keep in mind that Vite, like webpack
, Rollup
and Parcel
is a build tool that ehances the fontend development experience. Ultimately, the goal of this course is to learn basic skills for using the React library. If you would like to delve a bit deeper in Vite you can find more resources here at the Vite Homepage
.