diff --git a/packages/nuxi/src/commands/dev-child.ts b/packages/nuxi/src/commands/dev-child.ts index 51ac12c5c..70d967815 100644 --- a/packages/nuxi/src/commands/dev-child.ts +++ b/packages/nuxi/src/commands/dev-child.ts @@ -7,7 +7,7 @@ import defu from 'defu' import { resolve } from 'pathe' import { isTest } from 'std-env' -import { createNuxtDevServer } from '../utils/dev' +import { _getDevServerOverrides, createNuxtDevServer } from '../utils/dev' import { overrideEnv } from '../utils/env' import { logger } from '../utils/logger' import { cwdArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared' @@ -15,8 +15,7 @@ import { cwdArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared export default defineCommand({ meta: { name: '_dev', - description: - 'Run Nuxt development server (internal command to start child process)', + description: 'Run Nuxt development server (internal command to start child process)', }, args: { ...cwdArgs, @@ -26,9 +25,7 @@ export default defineCommand({ }, async run(ctx) { if (!process.send && !isTest) { - logger.warn( - '`nuxi _dev` is an internal command and should not be used directly. Please use `nuxi dev` instead.', - ) + logger.warn('`nuxi _dev` is an internal command and should not be used directly. Please use `nuxi dev` instead.') } // Prepare @@ -36,8 +33,7 @@ export default defineCommand({ const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) // Get dev context info - const devContext: NuxtDevContext - = JSON.parse(process.env.__NUXT_DEV__ || 'null') || {} + const devContext: NuxtDevContext = JSON.parse(process.env.__NUXT_DEV__ || 'null') || {} // IPC Hooks function sendIPCMessage(message: T) { @@ -59,15 +55,12 @@ 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] } }, - }) - } + ctx.data ||= {} + ctx.data.overrides = defu(ctx.data.overrides, _getDevServerOverrides({ + hostname: devContext.hostname, + public: devContext.public, + https: devContext.proxy?.https, + })) // Init Nuxt dev const nuxtDev = await createNuxtDevServer({ diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 1cc6051f0..64d42e5cc 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -17,6 +17,7 @@ import { resolve } from 'pathe' import { isBun, isTest } from 'std-env' import { showVersions } from '../utils/banner' +import { _getDevServerOverrides } from '../utils/dev' import { overrideEnv } from '../utils/env' import { loadKit } from '../utils/kit' import { logger } from '../utils/logger' @@ -71,20 +72,15 @@ const command = defineCommand({ if (ctx.args.fork) { // Fork Nuxt dev process const devProxy = await _createDevProxy(nuxtOptions, listenOptions) - await _startSubprocess(devProxy, ctx.rawArgs, listenOptions.hostname) + await _startSubprocess(devProxy, ctx.rawArgs, listenOptions) 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] } }, - }) - } + + ctx.data ||= {} + ctx.data.overrides = defu(ctx.data.overrides, _getDevServerOverrides(listenOptions)) const devServer = await createNuxtDevServer( { cwd, @@ -167,7 +163,7 @@ async function _createDevProxy(nuxtOptions: NuxtOptions, listenOptions: Partial< } } -async function _startSubprocess(devProxy: DevProxy, rawArgs: string[], hostname?: string) { +async function _startSubprocess(devProxy: DevProxy, rawArgs: string[], listenArgs: Partial) { let childProc: ChildProcess | undefined const kill = (signal: NodeJS.Signals | number) => { @@ -194,7 +190,8 @@ async function _startSubprocess(devProxy: DevProxy, rawArgs: string[], hostname? env: { ...process.env, __NUXT_DEV__: JSON.stringify({ - hostname, + hostname: listenArgs.hostname, + public: listenArgs.public, 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 fe1260198..8f56faa88 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 { + public?: boolean hostname?: string proxy?: { url?: string @@ -339,3 +340,21 @@ function _getAddressURL(addr: AddressInfo, https: boolean) { const port = addr.port || 3000 return `${proto}://${host}:${port}/` } + +export function _getDevServerOverrides(listenOptions: Partial>) { + const defaultOverrides: Partial = {} + + // defined hostname + if (listenOptions.hostname) { + const protocol = listenOptions.https ? 'https' : 'http' + defaultOverrides.devServer = { cors: { origin: [`${protocol}://${listenOptions.hostname}`] } } + defaultOverrides.vite = { server: { allowedHosts: [listenOptions.hostname] } } + } + + if (listenOptions.public) { + defaultOverrides.devServer = { cors: { origin: '*' } } + defaultOverrides.vite = { server: { allowedHosts: true } } + } + + return defaultOverrides +}