Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(vite): allow overriding client hmr options #6082

Merged
merged 4 commits into from
Jul 25, 2022
Merged
Changes from 2 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
26 changes: 15 additions & 11 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { logger } from '@nuxt/kit'
import { getPort } from 'get-port-please'
import { joinURL, withLeadingSlash, withoutLeadingSlash, withTrailingSlash } from 'ufo'
import escapeRE from 'escape-string-regexp'
import defu from 'defu'
import { cacheDirPlugin } from './plugins/cache-dir'
import { analyzePlugin } from './plugins/analyze'
import { wpfs } from './utils/wpfs'
Expand All @@ -16,11 +17,6 @@ import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
import { viteNodePlugin } from './vite-node'

export async function buildClient (ctx: ViteBuildContext) {
const hmrPortDefault = 24678 // Vite's default HMR port
const hmrPort = await getPort({
port: hmrPortDefault,
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
})
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
experimental: {
renderBuiltUrl: (filename, { type, hostType }) => {
Expand Down Expand Up @@ -66,12 +62,6 @@ export async function buildClient (ctx: ViteBuildContext) {
],
appType: 'custom',
server: {
hmr: {
// https:/nuxt/framework/issues/4191
protocol: 'ws',
clientPort: hmrPort,
port: hmrPort
},
middlewareMode: true
}
} as ViteOptions)
Expand All @@ -82,6 +72,20 @@ export async function buildClient (ctx: ViteBuildContext) {
clientConfig.server.hmr = false
}

if (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)
})
const userHMRConfig = clientConfig.server.hmr === true ? {} : clientConfig.server.hmr
pi0 marked this conversation as resolved.
Show resolved Hide resolved
clientConfig.server.hmr = defu(userHMRConfig, {
// https:/nuxt/framework/issues/4191
protocol: 'ws',
port: hmrPort
})
}

// Add analyze plugin if needed
if (ctx.nuxt.options.build.analyze) {
clientConfig.plugins.push(...analyzePlugin(ctx))
Expand Down