Skip to content
Naoya Tech
Go back

How Vite Works: Why It's So Fast, and How It Came to Be

15:36 Frontend

Overview

Vite is a development tool for building web app front ends, downloaded about 195 million times a week on npm and used under the hood by frameworks such as Nuxt and Astro. This video explains why TypeScript, JSX and npm packages need processing before the browser can run them, why bundler-based dev servers such as webpack’s slowed down as apps grew, and how Vite stays fast by letting the browser’s native ES Modules request files and transforming only what is asked for.

It also traces Vite’s history, from Evan You’s 226-line first commit in April 2020 to Vite 8, and walks through the dev server, HMR, plugins and production builds.

Key Points

Chapters

概要

ViteはWebアプリの画面を作るときに使う開発ツールで、npmでは週に約1億9,500万回ダウンロードされ、NuxtやAstroなど多くのフレームワークが内部で使っています。この動画では、TypeScriptやJSX、npmのパッケージをブラウザで動かす前に処理が必要な理由、webpackのようなバンドラーの開発サーバーがアプリの成長とともに遅くなった理由、そしてブラウザのES Modulesにファイルを取りに行かせ、頼まれた分だけを変換することでViteが速さを保つ仕組みを解説します。

あわせて、2020年4月にEvan Youが書いた226行の最初のコミットからVite 8までの歴史をたどり、開発サーバー、HMR、プラグイン、本番ビルドの中身を順に見ていきます。

ポイント

  • Viteは、めったに変わらない依存パッケージと自分のコードを分けて扱います。依存は起動時に1度だけ事前バンドルし、自分のコードはリクエストのたびにresolveId、load、transformの3段階で1ファイルずつ変換します。
  • 自分のコードはETagと304でキャッシュし、依存はURLにハッシュが付くので1年のimmutableなキャッシュを付けます。
  • HMRでは、変更したファイルからモジュールグラフを上へたどって変更を受け止める一番近い境界を探し、WebSocketでブラウザに知らせてそのモジュールだけを取り直させます。
  • プラグインはRollupのフックの形に合わせてあるため、同じプラグインが開発と本番の両方で動きます。Vite 8では、esbuildとRollupの2本立てをやめ、バンドラーをRolldownに1本化しました。
  • Vite 8.1では開発中もまとめる実験的なモードが入り、Environment APIによってブラウザ以外の実行環境も扱えるようになっています。

チャプター

Watch on YouTube

Share this post on:

Previous Video
php2shell Was Just the Beginning: OpenAI's Patch the Planet Explained
Next Video
What Is eBPF? Running Small Programs Safely in the Linux Kernel