Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/nuxi/src/commands/dev-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand All @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions packages/nuxi/src/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type NuxtDevIPCMessage =
| { type: 'nuxt:internal:dev:rejection', message: string }

export interface NuxtDevContext {
hostname?: string
proxy?: {
url?: string
urls?: ListenURL[]
Expand Down
Loading