Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improved cold start using deps scanner #8869

Merged
merged 32 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
76df629
feat: optimizeDeps.devStrategy, new 'dynamic-scan' strat
patak-dev Jun 30, 2022
48ae304
chore: merge main
patak-dev Jun 30, 2022
f3bb834
chore: update
patak-dev Jun 30, 2022
b6f2d04
fix: use urlWithoutBase
patak-dev Jun 30, 2022
fd46ccf
test: add example of full-reload with 'lazy' in preload playground
patak-dev Jun 30, 2022
562d479
chore: merge main
patak-dev Jul 1, 2022
cda64d2
refactor: dynamic-scan -> scan
patak-dev Jul 1, 2022
a7cf18f
feat: scan strategy, fix alias issue by making it the default
patak-dev Jul 2, 2022
d7da60e
chore: remove unneeded changes to scan.ts
patak-dev Jul 2, 2022
6d288fa
fix: use scanner during build time, remove devStrategy option
patak-dev Jul 2, 2022
39f9858
chore: update
patak-dev Jul 2, 2022
7ad4962
fix: preAliasPlugin during build
patak-dev Jul 2, 2022
89fef62
fix: restrict preAlias to aliased bareImports
patak-dev Jul 2, 2022
6eb114d
fix: post scan optimization
patak-dev Jul 2, 2022
387b18e
chore: try serialize esbuild
patak-dev Jul 2, 2022
80e7a4d
chore: serialize esbuild scan calls
patak-dev Jul 2, 2022
04f34b2
fix: await scanner before optimizing SSR deps
patak-dev Jul 2, 2022
aa58dd8
chore: revert 04f34b2939131edaae16b4f0c5a80978b6ff1210
patak-dev Jul 2, 2022
1ec2f5b
chore: update
patak-dev Jul 2, 2022
e491071
chore: test CI, await scanner and ssr deps optimization before server…
patak-dev Jul 2, 2022
9b33538
chore: sync set of depsOptimizer.scanning
patak-dev Jul 2, 2022
967dfd4
test: move SSR module error test to ssr-vue
patak-dev Jul 2, 2022
6c72736
test: avoid starting two servers concurrently in a test
patak-dev Jul 3, 2022
e814d41
fix: improve init logic
patak-dev Jul 3, 2022
0cb384b
chore: use hints for snapshots
patak-dev Jul 3, 2022
23df866
chore: remove optimizeDeps guard
patak-dev Jul 3, 2022
d81a855
fix: SSR deps external/noExternal optimization
patak-dev Jul 3, 2022
1a58e7d
fix: don't collide external/noExternal with optimizeDeps
patak-dev Jul 3, 2022
48b215e
fix: correct init server await
patak-dev Jul 4, 2022
96b9170
chore: reduce changeset
patak-dev Jul 4, 2022
81b3992
chore: reduce changeset
patak-dev Jul 4, 2022
8861550
chore: remove changes to SSR/optimizeDeps, TBD in another PR
patak-dev Jul 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions docs/guide/migration.md
Expand Up @@ -34,7 +34,6 @@ This section describes the biggest architecture changes in Vite v3. To allow pro
:::warning
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 [`server.host`](../config/server-options.md#server-host) to set it to `127.0.0.1`.

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`](https://github.com/rollup/plugins/tree/master/packages/commonjs) is no longer used.
Expand Down
8 changes: 0 additions & 8 deletions packages/vite/src/node/config.ts
Expand Up @@ -276,14 +276,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(colors.green(`Optimizing dependencies:\n ${depsString}`))

await addManuallyIncludedOptimizeDeps(deps, config)

const depsInfo = toDiscoveredDependencies(config, deps, !!config.build.ssr)

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, !!config.build.ssr, 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