Skip to content


Browse files Browse the repository at this point in the history
feat: improved cold start using deps scanner (#8869)
  • Loading branch information
patak-dev committed Jul 4, 2022
1 parent d412860 commit 188f188
Show file tree
Hide file tree
Showing 58 changed files with 1,158 additions and 553 deletions.
5 changes: 0 additions & 5 deletions docs/guide/
Expand Up @@ -34,7 +34,6 @@ This section describes the biggest architecture changes in Vite v3. To allow pro
These options are marked as experimental and deprecated. They may be removed in a future v3 minor without respecting semver. Please pin the Vite version when using them.

- `legacy.devDepsScanner`
- `legacy.buildRollupPluginCommonjs`
- `legacy.buildSsrCjsExternalHeuristics`

Expand All @@ -46,10 +45,6 @@ Vite's default dev server port is now 5173. You can use [`server.port`](../confi

Vite's default dev server host is now `localhost`. You can use [``](../config/ to set it to ``.

Vite optimizes dependencies with esbuild to both convert CJS-only deps to ESM and to reduce the number of modules the browser needs to request. In v3, the default strategy to discover and batch dependencies has changed. Vite no longer pre-scans user code with esbuild to get an initial list of dependencies on cold start. Instead, it delays the first dependency optimization run until every imported user module on load is processed.

To get back the v2 strategy, you can use `legacy.devDepsScanner`.

### Build Changes

In v3, Vite uses esbuild to optimize dependencies by default. Doing so, it removes one of the most significant differences between dev and prod present in v2. Because esbuild converts CJS-only dependencies to ESM, [`@rollupjs/plugin-commonjs`]( is no longer used.
Expand Down
8 changes: 0 additions & 8 deletions packages/vite/src/node/config.ts
Expand Up @@ -278,14 +278,6 @@ export interface ExperimentalOptions {

export interface LegacyOptions {
* Revert vite dev to the v2.9 strategy. Enable esbuild based deps scanner.
* @experimental
* @deprecated
* @default false
devDepsScanner?: boolean
* Revert vite build to the v2.9 strategy. Disable esbuild deps optimization and adds `@rollup/plugin-commonjs`
Expand Down
37 changes: 11 additions & 26 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -63,7 +63,6 @@ export interface DepsOptimizer {
isOptimizedDepFile: (id: string) => boolean
isOptimizedDepUrl: (url: string) => boolean
getOptimizedDepId: (depInfo: OptimizedDepInfo) => string

delayDepsOptimizerUntil: (id: string, done: () => Promise<any>) => void
registerWorkersSource: (id: string) => void
resetRegisteredIds: () => void
Expand Down Expand Up @@ -232,11 +231,15 @@ export async function optimizeDeps(
if (cachedMetadata) {
return cachedMetadata
const depsInfo = await discoverProjectDependencies(config)
const deps = await discoverProjectDependencies(config)

const depsString = depsLogString(Object.keys(depsInfo))
const depsString = depsLogString(Object.keys(deps))
log(`Optimizing dependencies:\n ${depsString}`))

await addManuallyIncludedOptimizeDeps(deps, config)

const depsInfo = toDiscoveredDependencies(config, deps, !!

const result = await runOptimizeDeps(config, depsInfo)

await result.commit()
Expand Down Expand Up @@ -365,9 +368,8 @@ export function loadCachedDepOptimizationMetadata(
* find deps to pre-bundle and include user hard-coded dependencies
export async function discoverProjectDependencies(
config: ResolvedConfig,
timestamp?: string
): Promise<Record<string, OptimizedDepInfo>> {
config: ResolvedConfig
): Promise<Record<string, string>> {
const { deps, missing } = await scanImports(config)

const missingIds = Object.keys(missing)
Expand All @@ -384,24 +386,7 @@ export async function discoverProjectDependencies(

return initialProjectDependencies(config, timestamp, deps)

* Create the initial discovered deps list. At build time we only
* have the manually included deps. During dev, a scan phase is
* performed and knownDeps is the list of discovered deps
export async function initialProjectDependencies(
config: ResolvedConfig,
timestamp?: string,
knownDeps?: Record<string, string>
): Promise<Record<string, OptimizedDepInfo>> {
const deps: Record<string, string> = knownDeps ?? {}

await addManuallyIncludedOptimizeDeps(deps, config)

return toDiscoveredDependencies(config, deps, !!, timestamp)
return deps

export function toDiscoveredDependencies(
Expand Down Expand Up @@ -431,7 +416,7 @@ export function toDiscoveredDependencies(

export function depsLogString(qualifiedIds: string[]): string {
if (isDebugEnabled) {
return colors.yellow(qualifiedIds.join(`\n `))
return colors.yellow(qualifiedIds.join(`, `))
} else {
const total = qualifiedIds.length
const maxListed = 5
Expand Down Expand Up @@ -656,7 +641,7 @@ export async function findKnownImports(
return Object.keys(deps)

async function addManuallyIncludedOptimizeDeps(
export async function addManuallyIncludedOptimizeDeps(
deps: Record<string, string>,
config: ResolvedConfig,
extra: string[] = [],
Expand Down

0 comments on commit 188f188

Please sign in to comment.