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

Commit

Permalink
fix(vite): move hmr settings to defaults, not overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 23, 2022
1 parent 416f98b commit b0ef470
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
18 changes: 0 additions & 18 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 { cacheDirPlugin } from './plugins/cache-dir'
Expand All @@ -16,11 +15,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,22 +60,10 @@ export async function buildClient (ctx: ViteBuildContext) {
],
appType: 'custom',
server: {
hmr: {
// https://github.com/nuxt/framework/issues/4191
protocol: 'ws',
clientPort: hmrPort,
port: hmrPort
},
middlewareMode: true
}
} 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
}

// Add analyze plugin if needed
if (ctx.nuxt.options.build.analyze) {
clientConfig.plugins.push(...analyzePlugin(ctx))
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ 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: 13 additions & 0 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 @@ -26,6 +27,11 @@ 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 @@ -72,6 +78,12 @@ 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 @@ -88,6 +100,7 @@ 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 b0ef470

Please sign in to comment.