Introduction to Vite - Next Generation Frontend Tooling
Tags: vuejavascript
Published: 2021-03-18 18:00:12

---
title: Introduction to Vite - Next Generation Frontend Tooling
published: true
date: 2021-03-16 18:34:12 UTC
tags: vuejavascript
canonical_url: https://www.thisdot.co/blog/introduction-to-vite-next-generation-frontend-tooling
---

## Introduction

In April 2020, Evan You, the creator of Vue, started to share some work on a new development server called Vite. [Here is the first tweet](https://twitter.com/youyuxi/status/1252173663199277058?s=20 "Twitter - Evan You") about the idea that Evan shared.

[![Evan You's first tweet about Vite](https://images.ctfassets.net/zojzzdop0fzx/2fM5JKjulxjcVuw65tKpDb/31777b9121313ff96ba16ee093f59eaa/vite_concept.png "As I was going to bed, I had an idea about a no-bundler dev setup (using native browser ES imports), but with support for Vue SFCs \*\*with hot reload\*\*. Now it's almost 6AM and I have a PoC working. The hot reload is so fast it's near instant.")](https://images.ctfassets.net/zojzzdop0fzx/2fM5JKjulxjcVuw65tKpDb/31777b9121313ff96ba16ee093f59eaa/vite_concept.png)

At the time, it was an experiment to see what could be done to improve the development experience for Vue applications. [Evan You joked](https://twitter.com/youyuxi/status/1262419524156080130?s=20 "Twitter - Evan You") that Vite was created to power an improved version of VuePress for blogging purposes.

Recently, Vite 2 was released as the first stable version, and in [Vue Contributer Days 2021](https://www.youtube.com/watch?t=446&v=WCtmf9Lp5oE&feature=youtu.be "YouTube - Vue Contributer Days February 2021") Evan discussed that Vite would be, "the foundation that we are going to be focusing on for future Vue-oriented tooling."

[At Vue Contributor Days](https://www.youtube.com/watch?t=533&v=WCtmf9Lp5oE&feature=youtu.be), Evan noted that, "putting [new developers] through webpack doesn't feel like the ideal experience for a progressive framework," and that Vite was going to be highlighted as the next step as a developer

But what exactly is Vite? How does it work? And how can it be used in development today?

## Vite - Next Generation Frontend Tooling

According to the [official Vite site](https://vitejs.dev), Vite is, "a build tool that aims to provide a faster and leaner development experience for modern web projects." It achieves this in two parts.

First, while in development, application code is not bundled (which is currently how development is done using [Vue CLI](https://cli.vuejs.org/) or [Create React App](https://create-react-app.dev/)). Instead, the code is imported into the browser using [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), the native module system for Javascript. Since ESM is supported in all modern browsers, Vite can leverage this to completely remove a build step while in development.

Any libraries that need to be imported are still compiled, but for that, Vite uses [esbuilt](https://esbuild.github.io/) a fast build tool written in Go. Per the Vite website, "Esbuild pre-bundles dependencies 10-100x faster than JavaScript-based bundlers."

Second, Vite provides a build step using Rollup that has been highly optimized for generating static assets. By leveraging Rollup, Vite also provides a diverse plugin ecosystem. Standard Rollup plugins can be used with Vite, as well as custom Vite-specific plugins. [From the documentation]:([https://vitejs.dev/guide/api-plugin.html](https://vitejs.dev/guide/api-plugin.html)) "Vite plugins extends Rollup's well-designed plugin interface with a few extra vite-specific options. As a result, you can write a Vite plugin once and have it work for both dev and build."

Vite also provides built-in support for:

- Typescript
- JSX and TSX
- Importing CSS files and CSS preprocessor files like .scss and .less
- Static assets such as images and JSON files

One key thing to note is that Vite is _not_ Vue-specific. While the original versions of Vite were geared towards Vue 3, the current release of Vite supports any frontend framework, with supported templates for Vue, React, Preact, Lit Element, Svelte, and vanilla Javascript. At its core, Vite is simply a build tool and development environment that can be leveraged by anyone.

## Getting Started

To start working with a Vite-based app, open your terminal and run the below command:

```
# NPM
npm init @vitejs/app

# Yarn
yarn create @vitejs/app
```

A prompt will guide you to select a name for your app and the template you want to use. For this article, we will be looking at a Vue application, but as noted above, there are many options available for building apps in Vite.

[![npm init @vitejs/app](https://images.ctfassets.net/zojzzdop0fzx/61og1UMVpUUlciwlkfldpj/153668e853def41d8c7cb1bd0aa66abb/npm_init_vite.png "Result of running npm init @vitejs/app")](https://images.ctfassets.net/zojzzdop0fzx/61og1UMVpUUlciwlkfldpj/153668e853def41d8c7cb1bd0aa66abb/npm_init_vite.png)

Once you select your name and template, you will be guided to navigate to the newly created folder, run `npm install` (or `yarn`). And that's it! Compared to both Create React App and the Vue CLI, this step is much faster, because there's less to install.

Now that your local setup is ready, let's get to work! In your terminal, run the following to get Vite running:

```
# NPM
npm run dev

# Yarn
yarn dev
```

[![vite-project npm run dev](https://images.ctfassets.net/zojzzdop0fzx/7hVNaxPy5Q8KsdJ3zFdDS6/3310ed97967c547b85588c9136e5a5ef/vite-project_npm_run_dev.png "Result of running npm run dev")](https://images.ctfassets.net/zojzzdop0fzx/7hVNaxPy5Q8KsdJ3zFdDS6/3310ed97967c547b85588c9136e5a5ef/vite-project_npm_run_dev.png)

When the dev command is run, esbuild is used to pre-bundle any dependencies you are using (in this case, the only dependency we have is Vue), then provides the URL to open the app itself. For this test, that build step took less than a second (subsequent startup times were around 200ms). For larger applications, it might take longer, but it's only run when Vite finds that your dependencies or configuration has changed. In general, this will only happen occassionally.

Open your browser to the provided URL (by default, localhost:3000), and you will see your app load as expected. If we open the network tab, you will also see that each component is imported into your application separately. This is because we are using ES Modules, rather than bundling everything into a single import. Neat!

[![vite-network-tab](https://images.ctfassets.net/zojzzdop0fzx/63xnKNCOWi71O4U6SeakgX/99de240dbf1f6973298b28995d175584/vite-network-tab.png)](https://images.ctfassets.net/zojzzdop0fzx/63xnKNCOWi71O4U6SeakgX/99de240dbf1f6973298b28995d175584/vite-network-tab.png)

Also of note: As you build out your application, only the relevant code is fetched into the browser. If you have multiple pages, each with unique components, those components will only be loaded as you navigate to those pages.

Let's take a quick look at the file structure. If you're familiar with the Vue CLI, the output from Vite is very similar. The main difference is that the `index.html` file is located in the root folder, rather than in the public folder. Otherwise, it should be very familiar.

[![vite-file-structure](https://images.ctfassets.net/zojzzdop0fzx/FqjIMzTX6bsoPJNzayLhK/4a33ae3c6b3ab3969d53cb4fc14ec87b/vite-file-structure.png)](https://images.ctfassets.net/zojzzdop0fzx/FqjIMzTX6bsoPJNzayLhK/4a33ae3c6b3ab3969d53cb4fc14ec87b/vite-file-structure.png)

Development using Vite is also very familiar. Create components, import files- everything you are used to doing with Vue CLI or Create React App should work as expected. When you're ready to deploy your app, run the below command:

```
# NPM
npm run build

# Yarn
yarn build
```

## Configuration and Plugins

Vite provides a helpful way to manage any configuration for your project. [From the Vite documentation](https://vitejs.dev/config/), "When running vite from the command line, Vite will automatically try to resolve a config file named vite.config.js inside project root."

As with a Vue CLI app, `vite.config.js` allows for a wide variety of configurations, including setting a base URL, any proxies to a backend service, and where to output the build files. In addition, plugins are also added to Vite in this file. Despite being a young platform, there are a wide range of plugins already available (some Rollup plugins can also be used in Vite).

Here's a few notable plugins to be aware of:

- [vite-plugin-pwa](https://github.com/antfu/vite-plugin-pwa) - Zero-config PWA.
- [vite-plugin-windicss](https://github.com/windicss/vite-plugin-windicss) - Windi CSS integration (Tailwind-compatible CSS framework).
- [vite-plugin-pages](https://github.com/hannoeru/vite-plugin-pages) - File system based route generator for Vue.
- [vite-plugin-md](https://github.com/antfu/vite-plugin-md) - Markdown as Vue components / Vue components in Markdown.
- [@brillout/vite-plugin-mdx](https://github.com/brillout/vite-plugin-mdx) - (React) Use MDX for your Vite app, with support for MDX v1, MDX v2, HMR, and SSR.
- [vite-plugin-ruby](https://github.com/ElMassimo/vite_rails/tree/main/package) - Vite configuration for Ruby backends.

There are a lot more plugins and other community contributions to look at as well. [Check out this list of awesome Vite plugins](https://github.com/vitejs/awesome-vite#plugins) for more examples!

## Why Use Vite?

The primary benefit of Vite is the development experience. Vite starts fast, loads fast, and allows you as a developer to get your job done better. In addition, the plugin ecosystem allows you to extend your app in numerous ways, all depending on what you want to do. Some opinionated templates also exist, such as [Vitesse](https://github.com/antfu/vitesse) by Anthony Fu of the Vue core team, that provide additional built-in functionality.

Taking that into consideration, Vite can be used for:

- Single-page applications: This is the default when starting a Vite app. The final build will be a standard SPA using Rollup that can be deployed to Netlify or any other provider that supports this type of application. 
- Server-side Rendering: While it doesn't come built in or as a template, [the documentation explains](https://vitejs.dev/guide/ssr.html) how to use Vite for SSR. Note that it is described as experimental. There is also a plugin that provides this functionality.
- Static Site Generation: Want a static site? [There's a plugin for Vue](https://github.com/antfu/vite-ssg) that does exactly that. [See it in action](https://antfu.me/)!
- Backend Integration: If you're building a site with a backend framework such as Rails or Laravel, you may want a more complete integration between the two development environments. [Vite provides documentation](https://vitejs.dev/guide/backend-integration.html) on how to do just that.

## Comparisons

As Evan noted, Vite is the foundation for Vue tooling going forward. [At Vue Contributor Days](https://www.youtube.com/watch?t=533&v=WCtmf9Lp5oE&feature=youtu.be), Evan noted that, "putting new developers through webpack doesn't feel like the ideal experience for a progressive framework." So how does it compare to existing solutions?

Keep in mind that Vite is a development environment, not a framework. The closest true comparisons to Vite are tools like Snowpack or WMR from the Preact team. The Vite site has [a complete writeup](https://vitejs.dev/guide/comparisons.html) on comparisons with other build tools. Rather than focusing on build tools, let's compare Vite with existing ways to build applications.

### Vue CLI/Create React App (webpack-based SPA)

We've already touched on Vue CLI and Create React App, but it's important to point out that Vite is, first and foremost, a development environment and build tool. In the end, it will build a single-page application, just like existing tools. The main benefit to Vite is the plugin ecosystem and the next-generation tooling. If you're comfortable with webpack, then that's fine!

As an aside, at Vue Contributor Days, [Evan and the panel talked about](https://youtu.be/WCtmf9Lp5oE?t=7829) the potential for integrating Vite into existing tools such as Vue CLI, and leveraging it for other uses such as Nuxt or Ionic. I strongly encourage listening to it when you have a moment.

### Nuxt/Next.js (Server-Side Rendered apps)

While Vite can provide SSR, it is still in an experimental state, and not recommended for production. If you are needing server-side rendering in your application, it's probably best to stick with the existing tooling for that.

That said, there's some work going on at Nuxt to provide Vite for your local development environment. [Here's an experimental plugin](https://github.com/nuxt/vite) for integrating Vite into Nuxt 2, with a polished version of this planned for Nuxt 3 later this year.

### Gatsby (Static Site Generators)

This one really depends on the kind of website that you're building. Gatsby (and other SSG tools like 11ty, Gridsome, etc) provides plugins to integrate with headless CMS platforms like Contentful or Sanity.io, as well as ways to manage image assets, RSS feed generation, and other features of blogs or documentation sites.

Vite, by comparison, provides none of that for you out of the box. What it can provide is excellent tooling for building a simple site, and generate static assets. We already see that in the Vue ecosystem, both with the SSG plugin mentioned above as well as [VitePress](https://vitepress.vuejs.org/), which can provide a fast-loading documentation site (or any other static site you want).

## Conclusion

As a build tool, Vite is still fairly new. However, it has already proven itself to be a fast development environment, leveraging modern tooling and language features to provide an excellent developer experience. While it may not replace existing tooling such as Vue CLI for some time, Evan has decided that it is the way forward for Vue tooling. Its fast uptime and vibrant community will only grow with time.

Next time you need to spin up a quick app in Vue or React, try using Vite for yourself! I look forward to see what you think!

_This Dot Labs is a modern web consultancy focused on helping companies realize their digital transformation efforts. For expert architectural guidance, training, or consulting in React, Angular, Vue, Web Components, GraphQL, Node, Bazel, or Polymer, visit [thisdotlabs.com](https://www.thisdotlabs.com)._

_This Dot Media is focused on creating an inclusive and educational web for all. We keep you up to date with advancements in the modern web through events, podcasts, and free content. To learn, visit [thisdot.co](https://www.thisdot.co)._