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

Commit

Permalink
refactor: move hmr port override back into client config file
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 25, 2022
1 parent 639907e commit b0278ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
22 changes: 22 additions & 0 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import vuePlugin from '@vitejs/plugin-vue'
import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
import type { Connect } from 'vite'
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 Down Expand Up @@ -64,6 +66,26 @@ export async function buildClient (ctx: ViteBuildContext) {
}
} as ViteOptions)

// In build mode we explicitly override any vite options that vite is relying on
// to detect whether to inject production or development code (such as HMR code)
if (!ctx.nuxt.options.dev) {
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
clientConfig.server.hmr = defu(userHMRConfig, {
// https://github.com/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
1 change: 0 additions & 1 deletion packages/vite/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export async function buildServer (ctx: ViteBuildContext) {
}
},
server: {
hmr: false,
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
preTransformRequests: false
},
Expand Down
13 changes: 0 additions & 13 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { logger, isIgnored } from '@nuxt/kit'
import type { Options } from '@vitejs/plugin-vue'
import replace from '@rollup/plugin-replace'
import { sanitizeFilePath } from 'mlly'
import { getPort } from 'get-port-please'
import { buildClient } from './client'
import { buildServer } from './server'
import virtual from './plugins/virtual'
Expand All @@ -27,11 +26,6 @@ export interface ViteBuildContext {
}

export async function bundle (nuxt: 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(
Expand Down Expand Up @@ -78,12 +72,6 @@ export async function bundle (nuxt: Nuxt) {
reactivityTransform: nuxt.options.experimental.reactivityTransform
},
server: {
hmr: {
// https://github.com/nuxt/framework/issues/4191
protocol: 'ws',
clientPort: hmrPort,
port: hmrPort
},
watch: { ignored: isIgnored },
fs: {
allow: [
Expand All @@ -100,7 +88,6 @@ export async function bundle (nuxt: Nuxt) {
// to detect whether to inject production or development code (such as HMR code)
if (!nuxt.options.dev) {
ctx.config.server.watch = undefined
ctx.config.server.hmr = false
ctx.config.build.watch = undefined
}

Expand Down

0 comments on commit b0278ef

Please sign in to comment.