Deploying a CSS/Vite Application

Tutorial 5 of 5

Deploying a CSS/Vite Application

1. Introduction

In this tutorial, we will be focusing on how to deploy a Vite application that utilizes CSS. We will walk through the process of building your Vite application into static files, which can then be hosted on any static hosting provider.

By the end of this tutorial, you will learn:

  • How to build your Vite application
  • How to deploy your application to a static hosting provider

Prerequisites:

  • Basic knowledge of HTML, CSS, and JavaScript
  • Familiarity with Vite (a build tool that aims to provide a leaner and faster developer experience)
  • A Vite application to deploy

2. Step-by-Step Guide

Step 1: Build Your Vite Application

Before deploying, you should build your Vite application into static files. It's as simple as running this command in your project directory:

npm run build

This command will create a dist directory with your built project.

Step 2: Choose a Hosting Provider

There are numerous hosting providers available. For this tutorial, we will use Vercel, but you can choose any provider that supports static sites (like Netlify or GitHub Pages).

Step 3: Deploy to Vercel

First, install Vercel CLI by running:

npm i -g vercel

Then, in your project directory, simply run:

vercel

Follow the prompts and your application will be deployed!

3. Code Examples

Building the Vite Application

Here's what happens when you run npm run build:

$ npm run build
> vite build

vite v2.6.4 building for production...
✓ 10 modules transformed.
dist/assets/favicon.17e50649.svg   1.49kb
dist/assets/index.3e41ec8e.css  0.78kb / brotli: 0.43kb
dist/index.html                    0.52kb
dist/assets/index.b311b06b.js    0.49kb / brotli: 0.29kb

The dist directory now contains your built project, ready to be deployed.

Deploying to Vercel

When you run vercel, you might see something like this:

$ vercel
Vercel CLI 23.1.2
? Set up and deploy “~/my-vite-app”? [Y/n] y
? Which scope do you want to deploy to? My User
? Link to existing project? [y/N] n
? What’s your project’s name? my-vite-app
? In which directory is your code located? ./
Auto-detected project settings (Vite.js):
- Build Command: `npm run build` or `yarn build`
- Output Directory: `dist`
- Development Command: vite dev --port $PORT
? Want to override the settings? [y/N] n
🔗 Linked to username/my-vite-app (created .vercel)
🔍 Inspect: https://vercel.com/username/my-vite-app/settings
✅ Production: https://my-vite-app.vercel.app [copied to clipboard]

Your application is now live at the provided URL!

4. Summary

In this tutorial, you learned how to build your Vite application into static files and deploy those files to a static hosting provider. The next step could be learning how to add dynamic features to your application, or how to use a CSS preprocessor like Sass with Vite.

For more about Vite, check out the official Vite documentation.

5. Practice Exercises

  1. Create a simple Vite application with HTML, CSS, and JavaScript and deploy it using the steps above.
  2. Modify your Vite application to include an external CSS library (like Bootstrap or Bulma), build it, and deploy the new version.
  3. Experiment with deploying your Vite application to a different static hosting provider, like Netlify or GitHub Pages.

For each exercise, be sure to test your live application to ensure everything works as expected. Happy coding!