Vite: Rethinking Frontend Tooling

Rate this content
Bookmark

Vite is a new build tool that intends to provide a leaner, faster, and more friction-less workflow for building modern web apps. This talk will dive into the project's background, rationale, technical details and design decisions: what problem does it solve, what makes it fast, and how does it fit into the JS tooling landscape.

Evan You
Evan You
31 min
09 Jun, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Vite is a next-generation build tool that leverages native ES modules for improved performance. It eliminates the need for bundling and improves hot module replacement. Vite provides an opinionated default configuration while still allowing advanced customization through plugins. It is framework agnostic and can be used for React and other applications. Vite is being adopted by Next.js and Create React App, and integration with Nuxt 3 offers significant speed improvements.

1. Introduction to Vite

Short description:

Hello, everyone. Today I'll be discussing Vite, a next-generation build tool. Vite consists of a no-bundle dev server and a production bundler. It's lean, fast, and opinionated. It's also extensible and cares about dependency count and payload size. Vite's performance is influenced by the widespread support for modern JavaScript and native ES modules.

Hello, everyone. My name is Evan Yeo and I'm the author of Vue.js and Vite. Today in this talk, I'm going to be talking about Vite, a next-generation build tool that I've been working on earlier this year. Specifically, I want to discuss some of the design tradeoffs involved in creating a new build tool, why we want to do that, and what is involved.

What is Vite? Vite mainly consists of two parts, a no-bundle dev server that serves your source files over native ES modules, and a production bundler which is rollout-based, is pre-configured and produces highly optimized production builds. Why would you want to use Vite over an existing tool? First of all, it's lean and fast. Really fast. We'll see an example soon. Second, it's opinionated with very sensible defaults. It's based on the experience that I've been working on Vue CLI, which is a very widely used build tool for Vue specific projects over the years. Similar to a pre-configured Webpack and Webpack Dev Server setup. It's also quite similar in terms of scope to Parcel. Many common features like TypeScript, JSX, and PostCSS work just out of the box. Despite being opinionated, it's still very extensible. It has a Rola compatible plugin interface that makes it very easy to build on top of.

So, just to get an idea of how lean it is, here is a tweet of someone running create react app and Vite side by side on Replit, which is a service that runs your project on a remote container. The Vite project is able to get up and running before the CRA project has even finished installing. One of the reasons that Vite is so lean is because it cares about its own dependency count and payload size. We actually pre-bundle a lot of unnecessary dependencies into Vite itself so that it installs faster. So node modules disk size is often a fraction of that of Webpack-based projects. Here is another example of a user migrating a production Rollup app over to Vite. As you can see, the original start time was over two minutes long and a single reload can take up to 23 seconds. And now both numbers, after migrating to Vite, are down to almost negligible numbers. Granted, Rollup doesn't have hot module replacement. But I bet Webpack users are not unfamiliar with hot updates taking a few seconds in large projects. With Vite, the HMR is guaranteed to be always instant. Now let's dig into what contributes to Vite's performance. Vite builds upon two interesting trends that we have seen in the past year. The first is that modern JavaScript is now widely supported. Native ES modules now have over 92% global support.

2. Performance and Overhead

Short description:

Legacy browsers like IE11 are becoming obsolete, making way for new JS compilers like ESBuild and SWC, which are significantly faster. Vite leverages native ESM by building the dev server around it, eliminating the need for bundling. Native ESM allows for on-demand loading, leveraging HTTP headers for caching, and efficient hot module replacement. However, there is a downside with native ESM, namely the overhead of HTTP requests. Vite addresses this issue by pre-bundling dependencies with ESBuild, reducing the number of HTTP requests and improving performance.

And we're only going to see this number increase as legacy browsers like IE11 going out of market at an accelerated pace. The second important thing is there are new JS compilers being written in compiled to native languages. Two of the most prominent examples are ESBuild, which is written in Go, and SWC, which is written in Rust. Both of these tools are dramatically faster than tools that are written in JavaScript. Sometimes up to 100%… sometimes up to 100 times faster, depending on the work type and CPU cores utilized.

So VEED leverages CHORN1 native ESM by building the dev server around native ES modules. There are many benefits of a native ESM dev server. First of all, there's no need to do any bundling. That's a big chunk of work that is simply not needed anymore. Second, native ESM is on-demand by nature, which means if a module is not loaded for the current screen that you're working on, it doesn't even need to be processed. Third, since modules are fetched over HTTP requests, we can leverage HTTP headers and let the browser do the caching. And finally, most importantly, hot module replacement can be implemented over native ESM in a very simple yet efficient manner.

So we'll see if this gives us big performance wins soon, but I want to be honest with the technical trade-offs here. Native ESM is not perfect without any downside. The main problem that we have faced is the HTTP requests overhead. For example, a dependency like Lodash ES may contain over 700 internal modules. And because native ESM is eager, when you import even just the single method from Lodash ES, the browser will try to request all of these modules that's imported in its entry point. Now, every module request translates to an HTTP request and the overhead just adds up. It creates a network congestion and even on local machines with little to zero latency, this overhead is still noticeable.

So, Vite does implement a few tricks to work around this problem. The first is we pre-bundle dependencies with ESBuilt. There are many big dependencies like Material.UI that contains hundreds and even thousands of internal modules. But they don't change often. They mostly contain standard JavaScript that doesn't need any special processing, unlike your source files. So, this makes them the perfect candidate to be pre-bundled with ESBuilt. By pre-bundling them, we can ensure that each dependency translates to only one HTTP request at most. And since ESBuilt is so fast, the starter boost is often dramatic, especially when you're using large dependencies. In addition, dependencies don't change often, so we can easily cache the pre-bundled output on disk and skip the pre-bundling phase on subsequent server starts. This process also converts common JS dependencies into ESM so that they are consumable natively by the browser. On top of that, pre-bundled dependencies can be strongly cached with HTTP headers.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Explain about NuxtJS codebase refactor and challenges facing to implement Vue 3, Vite and other packages.
How Bun Makes Building React Apps Simpler & Faster
React Day Berlin 2022React Day Berlin 2022
9 min
How Bun Makes Building React Apps Simpler & Faster
Bun’s builtin JSX transpiler, hot reloads on the server, JSX prop punning, macro api, automatic package installs, console.log JSX support, 4x faster serverside rendering and more make Bun the best runtime for building React apps
10 Years of Independent OSS: A Retrospective
JSNation 2024JSNation 2024
33 min
10 Years of Independent OSS: A Retrospective
In this talk, Evan takes a look back at his open source journey, starting from hobby projects to today leading two of the most influential projects in the JavaScript ecosystem today: Vue and Vite. We will discuss the ups and downs during this journey, and also touch a bit on the future of the two projects.

Workshops on related topic

Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.