DEV Community

Cover image for πŸš€ Getting started with Vite
Bruno
Bruno

Posted on

πŸš€ Getting started with Vite

Vite. Vite? Yes! But... what is Vite? Well, Vite is the next generation frontend tooling. It's a build tool that aims to provide a faster and leaner development experience for modern web projects. Come with me and let's explore Vite together. Who knows? Maybe you'll find it interesting and useful for your next project πŸ˜‰

What is Vite?

First, let's learn a bit of history, because you know what they say: "Those who don't know history are doomed to repeat it" πŸ“– Vite is a French word that means "fast". It's pronounced like "veet". The name is a reference to the tool's main selling point: speed. Vite was created by Evan You, the creator of Vue.js, and it's designed to be a build tool that is the next generation in frontend development. If you try Vite, forget about slow builds and waiting for your project to compile. Vite is fast, really fast. It's so fast that it feels like you're working with a framework that has a built-in server. It's like, hmm well... magic! 🎩✨

Why Vite?

For starters, Vite was designed to solve a problem that many frontend developers face: slow builds. When you're working on a project, you want to see the changes you make in real-time. You don't want to wait for your project to compile every time you make a change.

tom cat painting a dog house

Even Tom Cat knows that waiting is not fun. Vite addresses slow build times by providing a highly efficient development server that significantly speeds up the feedback loop for developers. It achieves this by using native ES modules in the browser, which allows for on-demand file serving and transformation, and by pre-bundling dependencies with esbuild, which is much faster than traditional JavaScript-based bundlers. This approach not only accelerates the development process, but also reduces the time developers spend waiting for changes to be reflected in the browser, making it feel very responsive.

combined chunks, kind of like Lego blocks, illustrated to create all different kinds of applications from the same set of modules

Like the above illustration from Mozilla(see https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) shows, breaking up the code into smaller chunks is possible with the ability to export and import variables between modules. This allows Vite to serve your code as-is, without having to bundle it into a single file. Comparing to tools like Webpack, which has to bundle your code into a single file before serving it, this is a huge performance improvement, and it's one of the main reasons why Vite is so fast.

Vite vs Webpack vs Snowpack

To get a better picture into the whole JavaScript bundler landscape, let's compare Vite with Webpack and Snowpack. Webpack is a popular JavaScript bundler that has been around for a while. It's powerful and flexible, but it can be slow, especially for large projects, the more they grow and the more packages you are using. Snowpack is a newer JavaScript bundler that is designed to be fast and lightweight. It's similar to Vite in that it uses native ES modules in the browser, but it's not as feature-rich as Vite. Vite, on the other hand, is designed to be fast, feature-rich, and highly optimized for Vue.js projects. It's also designed to be extensible, so you can use it with other frameworks and libraries as well.

vuejs prettier vite code snippet gif

In short, Vite is probably the best choice for Vue.js projects, but it's also a great choice for other modern web projects. Still not convinced? Let's take a look at some of the features that make Vite stand out.

Features

Let's break down some of the features that make Vite stand out:

  • Fast Builds: Vite is designed to be fast. It uses native ES modules in the browser, which allows for on-demand file serving and transformation, and it pre-bundles dependencies with esbuild, which is much faster than traditional JavaScript-based bundlers. This makes Vite feel very responsive and accelerates the development process.

  • Hot Module Replacement (HMR): Vite supports HMR out of the box, which means that you can see the changes you make in real-time without having to refresh the page. This makes the development process much more efficient and enjoyable.

  • Highly Optimized for Vue.js: Vite is designed to be highly optimized for Vue.js projects. It leverages Vue's reactivity system to provide a fast and efficient development experience for Vue developers.

  • Extensible: Vite is designed to be extensible, so you can use it with other frameworks and libraries as well. This makes it a great choice for modern web projects, not just Vue.js projects.

  • Built-in Features: Vite comes with built-in features like TypeScript support, CSS preprocessing, and more. This makes it easy to get started with Vite and reduces the need for additional configuration.

Β Getting Started

If this article has opened your appetite for Vite, you can get started by installing it with npm or yarn. To install Vite with npm, run the following command:

npm init vite@latest my-vite-project
Enter fullscreen mode Exit fullscreen mode

To install Vite with yarn, run the following command:

yarn create vite my-vite-project
Enter fullscreen mode Exit fullscreen mode

Once you've installed Vite, you can start your project by running the following commands:

cd my-vite-project
npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

This will start the Vite development server and open your project in the browser. You can start making changes to your project and see the changes in real-time. It's that easy!

mindblown

Conclusion

I hope this article has given you a good overview of Vite and why it's worth considering for your next project. As a quick recap, Vite is a fast, feature-rich, and highly optimized build tool that provides a faster and leaner development experience for modern web projects. It's designed to be highly efficient and responsive, and it comes with built-in features like TypeScript support, CSS preprocessing, and more. If you're looking for a modern build tool that is fast, feature-rich, and highly optimized for Vue.js projects, Vite is definitely worth considering. Give it a try and see for yourself!πŸš€

Resources

πŸ™‹β€β™‚οΈ About the Author

I'm a frontend developer and technical writer based in Portugal. I'm passionate about software engineering πŸ‘¨β€πŸ’», and I love to explore new tools in my day-to-day. You can find me on GitHub. If you have any questions or feedback, feel free to reach out! πŸš€

Top comments (2)

Collapse
 
fpaghar profile image
Fatemeh Paghar

While highlighting Vite's optimization for Vue.js, exploring its seamless integration with the Vue.js ecosystem could add depth. Vite's compatibility and synergy with Vue Router, Vuex, and other Vue-specific tools contribute to a holistic development experience, further enhancing its appeal for Vue.js projects. Understanding how Vite collaborates with the broader Vue.js ecosystem could offer valuable insights for developers seeking a comprehensive toolchain. πŸ› οΈπŸ”„

Collapse
 
bcostaaa01 profile image
Bruno

Thank you so much for your feedback and suggestion, Fatemeh! 😊 I will make sure to mention the broader integration in a future article for sure πŸ‘

Do you have any specific experiences with Vite in conjunction with Vue? I am curious to hear about others’ experiences working with these, maybe that will serve as further insight and inspiration 😊