vite/docs/guide/introduction.md

66 lines
6.2 KiB
Markdown
Raw Normal View History

2021-01-01 21:40:16 +00:00
# Introduction
## Overview
Vite (French word for "fast", pronounced `/vit/`) is a new breed of frontend build tool that significantly improves the frontend development experience. It consists of two major parts:
2021-02-05 22:10:31 +00:00
- A dev server that provides [rich feature enhancements](./features) over [native ES modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), for example extremely fast [Hot Module Replacement (HMR)](./features#hot-module-replacement).
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
- An opinionated [build command](./build) that bundles your code with [Rollup](https://rollupjs.org), pre-configured to output highly optimized static assets for production.
2021-01-01 21:40:16 +00:00
In addition, Vite is highly extensible via its [Plugin API](./api-plugin) and [JavaScript API](./api-javascript) with full typing support.
2021-02-05 22:10:31 +00:00
## The Problem
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
Before ES modules were available in browsers, developers had no native mechanism for authoring JavaScript in a modularized fashion. This is why we are all familiar with the concept of "bundling": using tools that crawl, process and concatenate our source modules into files that can run in the browser.
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
Over time we have seen tools like [webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org) and [Parcel](https://parceljs.org/), which greatly improved the development experience for frontend developers.
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
However, as we start to build more and more ambitious applications, the amount of JavaScript we are dealing with also increased exponentially. It is not uncommon for large scale projects to contain thousands of modules. We are starting to hit a performance bottleneck for JavaScript based tooling: it can often take an unreasonably long wait (sometimes up to minutes!) to spin up a dev server, and even with HMR, file edits can take a couple seconds to be reflected in the browser. The slow feedback loop can greatly affect developers' productivity and happiness.
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
Vite aims to address these issues by leveraging new advancements in the ecosystem: the availability of native ES modules in the browser, and the rise of JavaScript tools written in compile-to-native languages.
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
### Slow Server Start
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
When cold-starting the dev server, a bundler-based build setup has to eagerly crawl and build your entire application before it can be served.
Vite improves the dev server start time by first deviding the modules in an application into two categories: **dependencies** and **source code**.
- **Dependencies** are mostly plain JavaScript that do not change often during development. Some large dependencies (e.g. component libraries with hundreds of modules) are also quite expensive to process. Dependencies may also be shipped in various module formats (e.g. ESM or CommonJS).
Vite [pre-bundles dependencies](./dep-pre-bundling) using [esbuild](https://esbuild.github.io/). Esbuild is written in Go and pre-bundles dependencies 10-100x faster than JavaScript-based bundlers.
- **Source code** often contains non-plain JavaScript that needs transforming (e.g. JSX, CSS or Vue/Svelete components), and will be edited very often. Also, not all source code needs to be loaded at the same time (e.g. with route-based code-splitting).
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
Vite serves source code over [native ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). This is essentially letting the browser taking over part of the job of a bundler: Vite only needs to transform and serve source code on demand, as the browser requests them. Code behind conditional dynamic imports are only processed if actually used on the current screen.
![bundler based dev server](/images/bundler.png)
2021-01-01 21:40:16 +00:00
![esm based dev server](/images/esm.png)
2021-02-05 22:10:31 +00:00
### Slow Updates
When a file is edited in a bundler-based build setup, in addition to re-building the file itself, the bundler also needs to invalidate part of its module graph and re-construct the entire bundle. Reconstructing the bundle can be expensive, and reloading the page blows away the current state of the application. This is why some bundlers support Hot Module Replacment (HMR): allowing a module to "hot replace" itself without affecting the rest of the page. This greatly improves DX - however, in practice we've found that even HMR update speed deteriorates significantly as the size of the application grows.
In Vite, HMR is performed over native ESM. When a file is edited, Vite only needs to precisely invalidate the chain between the edited module and its closesest HMR boundary (most of the time only the module itself), making HMR updates consistently fast regardless of the size of your application.
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
Vite also leverages HTTP headers to speed up full page reloads (again, let the browser do more work for us): source code module requests are made conditional via `304 Not Modified`, and dependency module requests are strongly cached via `Cache-Control: max-age=31536000,immutable` so they don't hit the server again once cached.
2021-01-01 21:40:16 +00:00
Once you experience how fast Vite is, we highly doubt you'd be willing to put up with bundled development again.
## Why Bundle for Production
Even though native ESM is now widely supported, shipping unbundled ESM in production is still inefficient (even with HTTP/2) due to the additional network round trips caused by nested imports. To get the optimal loading performance in production, it is still better to bundle your code with tree-shaking, lazy-loading and common chunk splitting (for better caching).
2021-02-05 22:10:31 +00:00
Ensuring optimal output and behavioral consistency between the dev server and the production build isn't easy. This is why Vite ships with a pre-configured [build command](./build) that bakes in many [performance optimizations](.features#build-optimizations) out of the box.
2021-01-01 21:40:16 +00:00
## Browser Support
- Vite requires [native ESM dynamic import support](https://caniuse.com/es6-module-dynamic-import) during development.
2021-01-01 21:40:16 +00:00
2021-02-05 22:10:31 +00:00
- The production build assumes a baseline support for [native ESM via script tags](https://caniuse.com/es6-module). Vite does **not** perform any compatibility transpilation by default. Legacy browsers can be supported via the official [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) - see the [Building for Production](./build) section for more details.
2021-02-05 22:40:19 +00:00
## How is Vite Different from X?
You can check out the [Comparisons](./comparisons) section for more details on how Vite differs from other similar tools.