Vite JS Tutorial: Mode Variable (development/production)

beginner
12 min

Vite JS Tutorial: Mode Variable (development/production)

Welcome back to CodeYourCraft! Today, we're diving into an essential aspect of using Vite JS - the mode variable for development and production. Let's get started!

What is Mode Variable in Vite JS? 🎯

In Vite JS, the mode variable is used to define the build mode, either development or production. This variable helps optimize your project based on the environment, enhancing performance and providing a better development experience.

Setting Up the Mode Variable 📝

To set the mode variable, you'll find it in your vite.config.js file. Let's take a look at the basic structure:

javascript
import { defineConfig } from 'vite' export default defineConfig({ //...other configs mode: 'development', // or 'production' })

In the example above, we've set the mode to development. If you want to switch to production, simply change it to 'production'.

Understanding the Differences 💡

Now that we've set up the mode variable, let's discuss the differences between development and production modes:

Development Mode

During development, Vite JS provides a faster and more interactive development experience. It:

  • Hot Module Replacement (HMR) enables real-time code changes without a full page reload
  • Faster build times due to a development-optimized server
  • Source maps for easier debugging

Production Mode

In production, Vite JS focuses on optimizing your project for the best possible performance. It:

  • Minifies your code (CSS, JavaScript, HTML) for smaller file sizes
  • Bundles your code for faster load times
  • Performs tree shaking to remove unused code

Real-World Example 💡

Let's see how the mode variable affects our project with a simple example. Create a new Vite project:

sh
npm create vite my-project

Navigate to your project directory:

sh
cd my-project

Now, open the vite.config.js file and change the mode to 'production':

javascript
import { defineConfig } from 'vite' export default defineConfig({ mode: 'production', })

Next, create a JavaScript file (src/main.js) with a simple function:

javascript
function greet(name) { console.log(`Hello, ${name}!`) }

Now, let's see the impact of the mode variable on our project.

Development Mode

First, run your project in development mode:

sh
npm run dev

Navigate to http://localhost:3000 in your browser to see the result.

javascript
// vite-env.d.ts declare const process: any; declare const import.meta: { read: any };

Production Mode

Now, let's build the project for production:

sh
npm run build

In the dist folder, you'll find the compiled and minified version of your project:

javascript
// dist/main.js function greet(name) { console.log("Hello," + name) }

As you can see, the code has been minified for smaller file sizes.

Quiz Time 🎯

That's it for today! We hope you've gained a better understanding of the mode variable in Vite JS. Stay tuned for more tutorials on CodeYourCraft. Happy coding! 💻😊