Vite & VitePress
Evan You
VueConf Toronto 2020
What is Vite?
Vite
[veet]
French: rapid, quickly
Vite
Functionally similar to
pre-configured
webpack + webpack-dev-server
What makes it special?
It’s fast. Like, really fast.
New Vue 3 project / 10 components / no Babel / 2nd run
�Dev server start time
vue-cli 2568ms�vite 232ms
Page load time
vue-cli 320ms�vite 379ms
Build time
vue-cli 5.14s�vite 2.39s
Native-ES-modules-based
server for development
+
Rollup-based build for production
Native-ES-modules-based
server for development
The Why
The Why
The Why
The Why
The Why
Bundle based dev server
entry
route
route
module
module
module
module
...
...
Bundle based dev server
entry
route
route
module
module
module
module
...
...
Dynamic import
(code split point)
Bundle based dev server
entry
route
route
module
module
module
module
...
...
Bundle
Bundle based dev server
entry
route
route
module
module
module
module
...
...
Bundle
Server
ready
Native-ES-modules-based server for development:
How Vite does it
Native ESM based dev server
Server
ready
Native ESM based dev server
Server
ready
HTTP request
entry
route
route
module
module
module
module
...
...
Native ESM based dev server
Server
ready
HTTP request
entry
route
route
module
module
module
module
...
...
Dynamic import
(code split point)
Problem: HTTP request waterfall
Problem: HTTP request waterfall
Problem: HTTP request waterfall
Problem: HTTP request waterfall
Problem: Native ESM doesn’t support bare imports (yet)
import { createApp } from 'vue'
Problem: Native ESM doesn’t support bare imports (yet)
Problem: Native ESM doesn’t support bare imports (yet)
Problem: Native ESM doesn’t support bare imports (yet)
Source�
import { createApp } from 'vue'
Rewritten�
import { createApp } from '/@modules/vue'
Challenge: HMR over native ESM
Challenge: HMR over native ESM
1. Record module import graph while rewriting imports
entry
module
module
module
module
module
module
...
...
Record import chains
Challenge: HMR over native ESM
2. import.meta.hot usage marks file as “HMR boundary”
entry
boundary
module
module
module
module
module
...
...
import.meta.hot.accept(...)
Challenge: HMR over native ESM
3. When a file changes, we trace its importer chains to look for the HMR boundaries
entry
boundary
module
module
module
changed
module
...
...
Challenge: HMR over native ESM
�4. Boundaries re-import the changed module and apply updates
entry
boundary
module
module
module
changed
module
...
...
HTTP request
Challenge: HMR over native ESM
�5. If any of the parent chains reach a “dead end”, a full reload will be triggered to ensure consistency.
entry
boundary
module
module
module
module
changed
...
...
Reached root without
HMR boundary
Rollup-based build for production��The Why
Rollup-based build for production��The How
Rollup-based build for production��The How
Vite get started in a minute�
npm init vite-app
Vite is also framework-agnostic�
npm init vite-app --template react
See all templates at create-vite-app
VitePress
Static-site generator built on top of
Vite + Vue 3
[Still experimental]
VitePress
Like VuePress, but faster, lighter and more minimalistic
Drawbacks of VuePress
Drawbacks of VuePress
Solved by Vite!
The double payload problem
Single-Page Application
Static Site Generator
(SPA-SSG)
SPA-SSG
Dynamic Markdown
Cost of Dynamic Markdown
“Double payload” + unnecessary hydration
HTML Payload
JS Payload
static
dynamic
static
Compiled
Render function
hydrate
hydrate
hydrate
Dealing with double payload
Leverages Vue 3’s compiler optimizations for
better production performance
Vue 3 static tree hoisting
Vue 3 static tree hoisting
HTML Payload
JS Payload
static
dynamic
static
Compiled
Render function
skip
hydrate
skip
VitePress static string removal
Before
const _hoisted_1 = /*#__PURE__*/_createStaticVNode("<div>static content...", 1)
After
const _hoisted_1 = /*#__PURE__*/_createStaticVNode("", 1)
VitePress static string removal
HTML Payload
JS Payload
static
dynamic
static
Render fn
skip
hydrate
skip
VitePress static string removal
SPA Navigation
Bundle size optimization
More optimizations to come...