Skip to content

Commit

Permalink
perf(optimizer): start optimizer early (#12593)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Mar 26, 2023
1 parent edbd262 commit 4f9b8b4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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 @@ -71,6 +72,7 @@ export interface DepsOptimizer {
close: () => Promise<void>

options: DepOptimizationOptions
server?: ViteDevServer
}

export interface DepOptimizationConfig {
Expand Down
9 changes: 5 additions & 4 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import colors from 'picocolors'
import _debug from 'debug'
import { getHash } from '../utils'
import { getDepOptimizationConfig } from '..'
import { getDepOptimizationConfig } from '../config'
import type { ResolvedConfig, ViteDevServer } from '..'
import {
addManuallyIncludedOptimizeDeps,
Expand Down Expand Up @@ -122,6 +122,7 @@ async function createDepsOptimizer(
ensureFirstRun,
close,
options: getDepOptimizationConfig(config, ssr),
server,
}

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

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

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

// start optimizer in the background
let depsOptimizerReady: Promise<void> | undefined
if (isDepsOptimizerEnabled(config, false)) {
depsOptimizerReady = initDepsOptimizer(config)
}

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

let initingServer: Promise<void> | undefined
let serverInited = false
const initServer = async () => {
if (serverInited) {
return
}
if (initingServer) {
return initingServer
}
initingServer = (async function () {
await container.buildStart({})
if (isDepsOptimizerEnabled(config, false)) {
// non-ssr
await initDepsOptimizer(config, server)
// when the optimizer is ready, hook server so that it can reload the page
// or invalidate the module graph when needed
if (depsOptimizerReady) {
depsOptimizerReady.then(() => {
const depsOptimizer = getDepsOptimizer(config)
if (depsOptimizer) {
depsOptimizer.server = server
}
initingServer = undefined
serverInited = true
})()
return initingServer
})
}

if (!middlewareMode && httpServer) {
// overwrite listen to init optimizer before server start
const listen = httpServer.listen.bind(httpServer)
httpServer.listen = (async (port: number, ...args: any[]) => {
try {
await initServer()
await container.buildStart({})
} catch (e) {
httpServer.emit('error', e)
return
}
return listen(port, ...args)
}) as any
} else {
await initServer()
await container.buildStart({})
}

return server
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ async function createDevServer() {
configFile: false,
root,
logLevel: 'silent',
optimizeDeps: {
disabled: true,
},
})
server.pluginContainer.buildStart({})
return server
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrStacktrace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ async function createDevServer() {
configFile: false,
root,
logLevel: 'silent',
optimizeDeps: {
disabled: true,
},
})
server.pluginContainer.buildStart({})
return server
Expand Down
2 changes: 2 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default defineConfig({
'./playground-temp/**/*.*',
],
testTimeout: 20000,
// node14 segfaults often with threads
threads: !process.versions.node.startsWith('14'),
},
esbuild: {
target: 'node14',
Expand Down

0 comments on commit 4f9b8b4

Please sign in to comment.