fix(vite): hmr port priority (#3190)

This commit is contained in:
Anthony Fu 2022-02-11 20:09:25 +08:00 committed by GitHub
parent 408a944ffd
commit 61188e15cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 13 deletions

View File

@ -6,7 +6,6 @@ import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
import type { Connect } from 'vite'
import { joinURL } from 'ufo'
import { getPort } from 'get-port-please'
import { cacheDirPlugin } from './plugins/cache-dir'
import { analyzePlugin } from './plugins/analyze'
import { wpfs } from './utils/wpfs'
@ -16,13 +15,6 @@ import { devStyleSSRPlugin } from './plugins/dev-ssr-css'
import { DynamicBasePlugin, RelativeAssetPlugin } from './plugins/dynamic-base'
export async function buildClient (ctx: ViteBuildContext) {
// TODO: After nitropack refactor, try if we can resuse the same server port as Nuxt
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, {
define: {
'process.server': false,
@ -56,11 +48,7 @@ export async function buildClient (ctx: ViteBuildContext) {
})
],
server: {
middlewareMode: true,
hmr: {
port: hmrPort,
clientPort: hmrPort
}
middlewareMode: true
}
} as ViteOptions)

View File

@ -6,6 +6,7 @@ import type { InlineConfig, SSROptions } from 'vite'
import type { Options } from '@vitejs/plugin-vue'
import { sanitizeFilePath } from 'mlly'
import { joinURL, withoutLeadingSlash } from 'ufo'
import { getPort } from 'get-port-please'
import { buildClient } from './client'
import { buildServer } from './server'
import virtual from './plugins/virtual'
@ -23,6 +24,13 @@ export interface ViteBuildContext {
}
export async function bundle (nuxt: Nuxt) {
// TODO: After nitropack refactor, try if we can resuse the same server port as Nuxt
const hmrPortDefault = 24678 // Vite's default HMR port
const hmrPort = await getPort({
port: hmrPortDefault,
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
})
const ctx: ViteBuildContext = {
nuxt,
config: vite.mergeConfig(
@ -85,6 +93,10 @@ export async function bundle (nuxt: Nuxt) {
virtual(nuxt.vfs)
],
server: {
hmr: {
clientPort: hmrPort,
port: hmrPort
},
fs: {
strict: false,
allow: [