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
27 changes: 10 additions & 17 deletions packages/nuxi/src/commands/dev-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ 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'

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,
Expand All @@ -26,18 +25,15 @@ 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
overrideEnv('development')
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<T extends NuxtDevIPCMessage>(message: T) {
Expand All @@ -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({
Expand Down
19 changes: 8 additions & 11 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<ListenOptions>) {
let childProc: ChildProcess | undefined

const kill = (signal: NodeJS.Signals | number) => {
Expand All @@ -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(),
Expand Down
19 changes: 19 additions & 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 {
public?: boolean
hostname?: string
proxy?: {
url?: string
Expand Down Expand Up @@ -339,3 +340,21 @@ function _getAddressURL(addr: AddressInfo, https: boolean) {
const port = addr.port || 3000
return `${proto}://${host}:${port}/`
}

export function _getDevServerOverrides(listenOptions: Partial<Pick<ListenOptions, 'hostname' | 'public' | 'https'>>) {
const defaultOverrides: Partial<NuxtConfig> = {}

// 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
}