Welcome to our comprehensive guide on using the Vite Development Server! This tutorial is designed for both beginners and intermediate learners who are eager to understand and work with Vite in a practical, real-world setting. Let's dive in! šÆ
Vite is a modern, blazing-fast front-end build tool that simplifies web development, providing a seamless experience for developers. It stands out for its lightning-fast cold server start times and optimized hot module replacement (HMR). š”
To get started with Vite, you'll need Node.js (ā„ 14.15) and npm (ā„ 6.14.x) installed on your system. If you haven't installed them yet, you can download them from the official Node.js website and npm website.
Once you have Node.js and npm installed, you can create a new Vite project by running the following command in your terminal:
npm init vite my-projectReplace my-project with the name of your project.
Upon creating a new project, you'll notice a specific project structure. Here's a brief overview:
dist: This folder contains the built production version of your project.node_modules: All external dependencies are installed here.src: Your application's source code lives here.vite.config.js: Configuration file for Vite.š Note: The src folder is where you'll write your code for the frontend.
Now, let's create a simple HTML file and a JavaScript module to understand how Vite works.
In your src folder, create a new file named index.html. Add the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Vite App</title>
</head>
<body>
<h1>Hello, Vite! š</h1>
<script type="module" src="/src/script.js"></script>
</body>
</html>Next, create a new JavaScript file named script.js in the src folder and add the following content:
// script.js
export function greet() {
console.log('Hello, Vite Development Server!');
}Now, import the JavaScript module in your index.html file:
<!-- index.html -->
<script type="module" src="/src/script.js"></script>To run the project, navigate to your project directory in the terminal and run:
npm run devThis command starts the Vite Development Server and opens your project in the default browser. š
Let's create a simple counter application using Vite. We'll create a JavaScript module for the counter logic and an HTML file for the UI.
Create a new JavaScript file named counter.js in the src folder and add the following content:
// counter.js
let count = 0;
export function increment() {
count++;
console.log(`Count: ${count}`);
}
export function decrement() {
count--;
console.log(`Count: ${count}`);
}Create a new HTML file named counter.html in the src folder and add the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Counter App</title>
<script type="module" src="/src/counter.js"></script>
</head>
<body>
<h1>Counter App</h1>
<button id="increment">Increment</button>
<button id="decrement">Decrement</button>
<p id="count">0</p>
<script type="module">
const incrementBtn = document.getElementById('increment');
const decrementBtn = document.getElementById('decrement');
const countElement = document.getElementById('count');
import { increment, decrement } from './counter.js';
incrementBtn.addEventListener('click', () => {
increment();
countElement.textContent = count;
});
decrementBtn.addEventListener('click', () => {
decrement();
countElement.textContent = count;
});
</script>
</body>
</html>To run the counter application, replace the index.html file in the public folder with the counter.html file. Then, run the project with the npm run dev command.
What is Vite, and why is it a popular choice for front-end development?