In this task, we will set up the React project using Vite. We will also clean up the boilerplate code and add Tailwind CSS for styling, among other things.
Step 1: Create web and api directories
Unlike previous tutorials, we will create two separate directories for the frontend and backend parts of the application. This separation will help us manage the project more efficiently.
We will create the frontend part of the application in the web directory and the mock server API in the api directory.
Step 2: Create a new Vite project
Navigate to the web directory and create a new Vite project using the React template.
This command will create a new Vite project with the React template in the current directory. It comes with TypeScript support out of the box.
Step 3: Clean up the boilerplate code
Let’s clean up the boilerplate code generated by Vite to keep only the essential files and remove unnecessary ones.
Notice that I have removed the README.md and .gitignore files as well. We will create a new README.md and .gitignore file later in this tutorial, and place them in the root of the project.
Add favicon.svg
Create a new favicon.svg file in the public directory. This will be used as the favicon for the application.
Add the following content to the favicon.svg file:
The SVG code above creates a simple icon that represents a list of posts.
Update index.html
Update the index.html file in the public directory to include the new favicon.
Notice that we have also updated the title of the application to “Posts App”.
Update App.tsx
Update the App.tsx file in the src directory to display a simple message.
Update README.md
Add README.md to the root of the project with the following content:
We will add a .gitignore file later in this tutorial.
Install Dependencies and Start the App
Let’s install the dependencies and start the application to see the changes.
Step 4: Install Prettier
As usual, we will install Prettier to format our code automatically.
Add .prettierrc.json
Make sure to create a .prettierrc.json file in the web folder. This file will contain the Prettier configuration.
Add the following configuration to the .prettierrc.json file:
Add .prettierignore file
Create a .prettierignore file in the web folder. This file will contain the paths to ignore when formatting the code.
Add the following paths to the .prettierignore file:
Add prettier script to package.json
Update the package.json file in the web folder to include the prettier script.
Integrate Prettier with ESLint
We will integrate Prettier with ESLint to ensure that our linter and formatter work together seamlessly. This integration will help us maintain a consistent code style throughout the project. Install the required packages:
Update .eslintrc.json
Update the .eslintrc.json file in the web folder to include the Prettier plugin.
Update package.json
Update the lint script in the package.json file as follows:
This --fix flag will automatically fix the linting issues in the code when you run the lint script.
Run Prettier and ESLint
Let’s run Prettier and ESLint to make sure everything is set up correctly.
Step 5: Install Tailwind CSS
Now that we have set up Prettier and ESLint, let’s install Tailwind CSS for styling the application.
Add tailwind.config.js
Initialize the Tailwind CSS configuration file by running the following command:
This command will create a tailwind.config.js and a postcss.config.js file in the root of the project.
Update tailwind.config.js
We need to update the tailwind.config.js file to include the paths to the HTML and JavaScript files in the project.
Add @tailwind directives to index.css
Next, we need to add the @tailwind directives to the src/index.css file to include the Tailwind CSS styles.
Test Tailwind CSS
To test if Tailwind CSS is working correctly, update the App.tsx file to include some Tailwind CSS classes.
Run the application to see if Tailwind CSS is applied correctly.
Step 6: Install Shadcn UI
Shadcn UI is a collection of accessible and customizable components for React applications. We don’t need to install it as it is not an NPM package. Instead, we will configure our application and use shadcn-ui CLI to copy the components to our project.
Update tsconfig.*.json
Add path aliases to the tsconfig.json, tsconfig.app.json, and tsconfig.node.json files to simplify the import paths in the project.
Update vite.config.ts
Add an alias to the vite.config.ts file to resolve the path aliases in the project.
Since we imported path from Node.js, we need to install the types for Node.js.
Create components.json file
The CLI of Shadcn UI relies on a components.json file to copy the components to the project. We can use the CLI to create this file.
You will be prompted with a series of questions to configure the project. Here are the answers you can provide:
Once done, you will see the following output:
You should now have a components.json file in the web folder with the following content:
Test Shadcn UI
Let’s add a button component from Shadcn UI to the project.
This command will copy the button component to the src/components/ui directory. You can now use the button component in your application.
Step 7: Add GitHub Action to deploy the app
We will add a GitHub Action workflow to deploy the Vite app to GitHub Pages automatically. This workflow will run every time we push changes to the main branch.
First, we need to initialize a new Git repository in the project directory. To that aim, add a .gitignore to the root of the project with the following content:
Then initialize a new Git repository and commit the changes:
Next, create a new GitHub Action workflow file in the .github/workflows directory. Name the file deploy.yml and add the following content:
Notice this section in the workflow file:
This section checks out the repository and moves the contents of the web directory to the root of the project. This is necessary because our Vite project is in the web directory, but GitHub Pages expects the files to be in the root directory.
Conclusion
In this task, we set up the React project using Vite. We cleaned up the boilerplate code, added Tailwind CSS for styling, and integrated Shadcn UI for accessible components. We also configured Prettier and ESLint for code formatting and linting. Finally, we added a GitHub Action workflow to deploy the Vite app to GitHub Pages automatically.