Skip to content

fix(optimizer): start optimizer after buildStart #12832

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

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { transformWithEsbuild } from '../plugins/esbuild'
import { ESBUILD_MODULES_TARGET } from '../constants'
import { resolvePackageData } from '../packages'
import type { ViteDevServer } from '../server'
import { esbuildCjsExternalPlugin, esbuildDepPlugin } from './esbuildDepPlugin'
import { scanImports } from './scan'
export {
Expand Down Expand Up @@ -66,7 +65,6 @@ export interface DepsOptimizer {
close: () => Promise<void>

options: DepOptimizationOptions
server?: ViteDevServer
}

export interface DepOptimizationConfig {
Expand Down
7 changes: 3 additions & 4 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ async function createDepsOptimizer(
ensureFirstRun,
close,
options: getDepOptimizationConfig(config, ssr),
server,
}

depsOptimizerMap.set(config, depsOptimizer)
Expand Down Expand Up @@ -471,13 +470,13 @@ async function createDepsOptimizer(
}

function fullReload() {
if (depsOptimizer.server) {
if (server) {
// Cached transform results have stale imports (resolved to
// old locations) so they need to be invalidated before the page is
// reloaded.
depsOptimizer.server.moduleGraph.invalidateAll()
server.moduleGraph.invalidateAll()

depsOptimizer.server.ws.send({
server.ws.send({
type: 'full-reload',
path: '*',
})
Expand Down
16 changes: 4 additions & 12 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,6 @@ export async function _createServer(
): Promise<ViteDevServer> {
const config = await resolveConfig(inlineConfig, 'serve')

if (isDepsOptimizerEnabled(config, false)) {
// start optimizer in the background, we still need to await the setup
await initDepsOptimizer(config)
}

const { root, server: serverConfig } = config
const httpsOptions = await resolveHttpsConfig(config.server.https)
const { middlewareMode } = serverConfig
Expand Down Expand Up @@ -656,13 +651,6 @@ export async function _createServer(
// error handler
middlewares.use(errorMiddleware(server, middlewareMode))

// when the optimizer is ready, hook server so that it can reload the page
// or invalidate the module graph when needed
const depsOptimizer = getDepsOptimizer(config)
if (depsOptimizer) {
depsOptimizer.server = server
}

// httpServer.listen can be called multiple times
// when port when using next port number
// this code is to avoid calling buildStart multiple times
Expand All @@ -674,6 +662,10 @@ export async function _createServer(

initingServer = (async function () {
await container.buildStart({})
// start deps optimizer after all container plugins are ready
if (isDepsOptimizerEnabled(config, false)) {
await initDepsOptimizer(config, server)
}
initingServer = undefined
serverInited = true
})()
Expand Down