fix(vite): don't get available port when `hmr.server` is set (#27326)

This commit is contained in:
Jakub Szafrański 2024-05-28 14:48:33 +02:00 committed by GitHub
parent 1a57c42223
commit b8bb1aacd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 9 deletions

View File

@ -171,18 +171,22 @@ export async function buildClient (ctx: ViteBuildContext) {
}) as any
if (clientConfig.server && clientConfig.server.hmr !== false) {
const hmrPortDefault = 24678 // Vite's default HMR port
const hmrPort = await getPort({
port: hmrPortDefault,
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i),
})
clientConfig.server = defu(clientConfig.server, <ServerOptions> {
https: ctx.nuxt.options.devServer.https,
const serverDefaults: Omit<ServerOptions, 'hmr'> & { hmr: Exclude<ServerOptions['hmr'], boolean> } = {
hmr: {
protocol: ctx.nuxt.options.devServer.https ? 'wss' : 'ws',
port: hmrPort,
},
})
}
if (typeof clientConfig.server.hmr !== 'object' || !clientConfig.server.hmr.server) {
const hmrPortDefault = 24678 // Vite's default HMR port
serverDefaults.hmr!.port = await getPort({
port: hmrPortDefault,
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i),
})
}
if (ctx.nuxt.options.devServer.https) {
serverDefaults.https = ctx.nuxt.options.devServer.https === true ? {} : ctx.nuxt.options.devServer.https
}
clientConfig.server = defu(clientConfig.server, serverDefaults as ViteConfig['server'])
}
// Add analyze plugin if needed