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

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
Daniel Roe 2022-07-25 13:29:41 +01:00 committed by GitHub
parent 210cf30691
commit 8298cf27e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 13 deletions

View File

@ -2,11 +2,12 @@ import { join, resolve } from 'pathe'
import * as vite from 'vite' import * as vite from 'vite'
import vuePlugin from '@vitejs/plugin-vue' import vuePlugin from '@vitejs/plugin-vue'
import viteJsxPlugin from '@vitejs/plugin-vue-jsx' import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
import type { Connect } from 'vite' import type { Connect, HmrOptions } from 'vite'
import { logger } from '@nuxt/kit' import { logger } from '@nuxt/kit'
import { getPort } from 'get-port-please' import { getPort } from 'get-port-please'
import { joinURL, withLeadingSlash, withoutLeadingSlash, withTrailingSlash } from 'ufo' import { joinURL, withLeadingSlash, withoutLeadingSlash, withTrailingSlash } from 'ufo'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
import defu from 'defu'
import { cacheDirPlugin } from './plugins/cache-dir' import { cacheDirPlugin } from './plugins/cache-dir'
import { analyzePlugin } from './plugins/analyze' import { analyzePlugin } from './plugins/analyze'
import { wpfs } from './utils/wpfs' import { wpfs } from './utils/wpfs'
@ -16,11 +17,6 @@ import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
import { viteNodePlugin } from './vite-node' import { viteNodePlugin } from './vite-node'
export async function buildClient (ctx: ViteBuildContext) { 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, { const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
experimental: { experimental: {
renderBuiltUrl: (filename, { type, hostType }) => { renderBuiltUrl: (filename, { type, hostType }) => {
@ -66,12 +62,6 @@ export async function buildClient (ctx: ViteBuildContext) {
], ],
appType: 'custom', appType: 'custom',
server: { server: {
hmr: {
// https://github.com/nuxt/framework/issues/4191
protocol: 'ws',
clientPort: hmrPort,
port: hmrPort
},
middlewareMode: true middlewareMode: true
} }
} as ViteOptions) } as ViteOptions)
@ -82,6 +72,19 @@ export async function buildClient (ctx: ViteBuildContext) {
clientConfig.server.hmr = false 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)
})
clientConfig.server.hmr = defu(clientConfig.server.hmr as HmrOptions, {
// https://github.com/nuxt/framework/issues/4191
protocol: 'ws',
port: hmrPort
})
}
// Add analyze plugin if needed // Add analyze plugin if needed
if (ctx.nuxt.options.build.analyze) { if (ctx.nuxt.options.build.analyze) {
clientConfig.plugins.push(...analyzePlugin(ctx)) clientConfig.plugins.push(...analyzePlugin(ctx))

View File

@ -96,7 +96,8 @@ export async function buildServer (ctx: ViteBuildContext) {
}, },
server: { server: {
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027 // https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
preTransformRequests: false preTransformRequests: false,
hmr: false
}, },
plugins: [ plugins: [
cacheDirPlugin(ctx.nuxt.options.rootDir, 'server'), cacheDirPlugin(ctx.nuxt.options.rootDir, 'server'),