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.

This talk has been presented at JSNation Live 2021, check out the latest edition of this JavaScript Conference.

FAQ

Vite is a next-generation build tool that consists of two main parts: a no-bundle development server that serves source files over native ES modules, and a production bundler that is pre-configured to produce highly optimized production builds.

Vite is lean and fast, with very sensible defaults based on years of experience with Vue CLI. It is very similar to pre-configured Webpack setups but offers faster performance due to lower dependency count and payload size. Additionally, Vite is still very extensible with a Rollup-compatible plugin interface.

Vite leverages native ES modules and modern JavaScript compilers like ESBuild (written in Go) and SWC (written in Rust) for faster performance. It pre-bundles dependencies to reduce HTTP request overhead and uses efficient caching mechanisms to minimize reload times.

Yes, Vite is suitable for large projects. Users have reported significant performance improvements when migrating from other tools like Webpack to Vite, even for projects with large codebases.

Yes, Vite is framework-agnostic and can be used for building React applications. It provides a fast and efficient development experience for React developers.

Native ES modules eliminate the need for bundling, reduce on-demand processing, allow efficient HTTP caching, and enable instant hot module replacement (HMR). These features contribute to Vite's overall performance gains.

While Vite's native ESM dev server offers faster server start times and efficient HMR, the first page load may be slower due to on-demand processing of source file modules and HTTP request overhead. However, the combined server start time and first page load time still show significant performance gains.

Vite uses Rollup for production builds because it provides better control over code splitting, which is essential for optimizing user-facing applications. While ESBuild is faster, Rollup offers more flexibility and better performance for end users.

Yes, Vite supports server-side rendering and provides an API for loading ESM source files in Node.js. Several SSR meta-frameworks like SvelteKit, Ream, ViteSSR, and VitePluginSSR are built on top of Vite.

Yes, Vite is a competent replacement for Create React App (CRA). It offers faster performance and a better development experience. However, some features like Jest support may require additional configuration.

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
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
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 is a modern all-in-one JavaScript runtime environment that achieves new levels of performance. It includes BUN dev, a fast front-end dev server, BUN install, a speedy package manager, and BUN run, a fast package runner. BUN supports JSX, has optimized React server-side rendering, and offers hot module reloading on the server. The priorities for BUN include stability, node compatibility, documentation improvement, missing features in BUN install, AST plugin API, native Windows support, Bundler and Minifier optimization, and easier deployment to production. BUN's AST plugin API allows for bundle-time JavaScript execution and embedding code, potentially inspiring new frameworks.
Turbopack. Why? How? When? and the Vision...
React Day Berlin 2022React Day Berlin 2022
32 min
Turbopack. Why? How? When? and the Vision...
The Talk discusses TurboPack, a successor to Webpack, aiming to create a framework-independent, flexible, and extensible tool for the open-source community. It addresses performance challenges by integrating SWC into Next.js. The challenges with Next.js and Webpack include orchestration issues, backward compatibility constraints, and cache invalidation problems. TurboEngine and TurboPack provide constant performance in incremental builds, leveraging Rust's predictable performance and parallelism. The Talk also covers topics like dependency tracking, task graphs, cache invalidation, lazy asset graphs, and the integration of TurboPack with Next.js. The future plans involve reconfiguring Webpack and TurboEngine, moving computations to the cloud, providing insights into builds, and facilitating migration and integration with JavaScript projects.
The Core of Turbopack Explained (Live Coding)
JSNation 2023JSNation 2023
29 min
The Core of Turbopack Explained (Live Coding)
Tobias Koppers introduces TurboPack and TurboEngine, addressing the limitations of Webpack. He demonstrates live coding to showcase the optimization of cache validation and build efficiency. The talk covers adding logging and memorization, optimizing execution and tracking dependencies, implementing invalidation and watcher, and storing and deleting invalidators. It also discusses incremental compilation, integration with other monorepo tools, error display, and the possibility of a plugin system for Toolpag. Lastly, the comparison with Bunn's Builder is mentioned.
10 Years of Independent OSS: A Retrospective
JSNation 2024JSNation 2024
33 min
10 Years of Independent OSS: A Retrospective
This talk is a ten-year retrospective into the growth of the Vue.js framework as an open-source project. It highlights the challenges faced by open-source developers, the importance of finding balance and managing scope, and the collaborative nature of the Vue community. The talk also discusses the development of Vite as a build tool and the vision for a unified JavaScript toolchain. It emphasizes the need for community alignment, contributions, and testing, while acknowledging the challenges of bad actors in the open-source community.

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.