diff --git a/packages/nuxi/src/commands/dev-child.ts b/packages/nuxi/src/commands/dev-child.ts index 18480e69f..51ac12c5c 100644 --- a/packages/nuxi/src/commands/dev-child.ts +++ b/packages/nuxi/src/commands/dev-child.ts @@ -3,6 +3,7 @@ import type { NuxtDevContext, NuxtDevIPCMessage } from '../utils/dev' import process from 'node:process' import { defineCommand } from 'citty' +import defu from 'defu' import { resolve } from 'pathe' import { isTest } from 'std-env' @@ -58,6 +59,16 @@ export default defineCommand({ process.exit() }) + const hostname = devContext.hostname + if (hostname) { + ctx.data ||= {} + const protocol = devContext.proxy?.https ? 'https' : 'http' + ctx.data.overrides = defu(ctx.data.overrides, { + devServer: { cors: { origin: [`${protocol}://${hostname}`] } }, + vite: { server: { allowedHosts: [hostname] } }, + }) + } + // Init Nuxt dev const nuxtDev = await createNuxtDevServer({ cwd, diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 72cbb0cbc..1cc6051f0 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -10,6 +10,7 @@ import process from 'node:process' import { setupDotenv } from 'c12' import { defineCommand } from 'citty' +import defu from 'defu' import { createJiti } from 'jiti' import { getArgs as getListhenArgs, parseArgs as parseListhenArgs } from 'listhen/cli' import { resolve } from 'pathe' @@ -70,12 +71,20 @@ const command = defineCommand({ if (ctx.args.fork) { // Fork Nuxt dev process const devProxy = await _createDevProxy(nuxtOptions, listenOptions) - await _startSubprocess(devProxy, ctx.rawArgs) + await _startSubprocess(devProxy, ctx.rawArgs, listenOptions.hostname) return { listener: devProxy?.listener } } else { // Directly start Nuxt dev const { createNuxtDevServer } = await import('../utils/dev') + if (listenOptions.hostname) { + ctx.data ||= {} + const protocol = listenOptions.https ? 'https' : 'http' + ctx.data.overrides = defu(ctx.data.overrides, { + devServer: { cors: { origin: [`${protocol}://${listenOptions.hostname}`] } }, + vite: { server: { allowedHosts: [listenOptions.hostname] } }, + }) + } const devServer = await createNuxtDevServer( { cwd, @@ -158,7 +167,7 @@ async function _createDevProxy(nuxtOptions: NuxtOptions, listenOptions: Partial< } } -async function _startSubprocess(devProxy: DevProxy, rawArgs: string[]) { +async function _startSubprocess(devProxy: DevProxy, rawArgs: string[], hostname?: string) { let childProc: ChildProcess | undefined const kill = (signal: NodeJS.Signals | number) => { @@ -185,6 +194,7 @@ async function _startSubprocess(devProxy: DevProxy, rawArgs: string[]) { env: { ...process.env, __NUXT_DEV__: JSON.stringify({ + hostname, proxy: { url: devProxy.listener.url, urls: await devProxy.listener.getURLs(), diff --git a/packages/nuxi/src/utils/dev.ts b/packages/nuxi/src/utils/dev.ts index b25f26f77..fe1260198 100644 --- a/packages/nuxi/src/utils/dev.ts +++ b/packages/nuxi/src/utils/dev.ts @@ -28,6 +28,7 @@ export type NuxtDevIPCMessage = | { type: 'nuxt:internal:dev:rejection', message: string } export interface NuxtDevContext { + hostname?: string proxy?: { url?: string urls?: ListenURL[]