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

View File

@ -171,18 +171,22 @@ export async function buildClient (ctx: ViteBuildContext) {
}) as any }) as any
if (clientConfig.server && clientConfig.server.hmr !== false) { if (clientConfig.server && clientConfig.server.hmr !== false) {
const hmrPortDefault = 24678 // Vite's default HMR port const serverDefaults: Omit<ServerOptions, 'hmr'> & { hmr: Exclude<ServerOptions['hmr'], boolean> } = {
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,
hmr: { hmr: {
protocol: ctx.nuxt.options.devServer.https ? 'wss' : 'ws', 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 // Add analyze plugin if needed