Skip to content

Commit

Permalink
refactor(dev): rewrite dev to support --no-fork and improve stabili…
Browse files Browse the repository at this point in the history
…ty (#153)
  • Loading branch information
pi0 authored Sep 7, 2023
1 parent e293587 commit 2191619
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 285 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"http-proxy": "^1.18.1",
"httpxy": "^0.1.4",
"jiti": "^1.20.0",
"listhen": "^1.4.7",
"listhen": "^1.4.8",
"magicast": "^0.2.10",
"mlly": "^1.4.2",
"mri": "^1.2.0",
Expand Down
6 changes: 5 additions & 1 deletion playground/pages/ws.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { WebSocket } from 'unws'
const reqURL = useRequestURL()
const urls = [`ws://${reqURL.host}/api/ws`, 'ws://localhost:8080']
const isSecure = reqURL.protocol === 'https:'
const urls = [
`${isSecure ? 'wss' : 'ws'}://${reqURL.host}/api/ws`,
'ws://localhost:8080',
]
const _queryURL = useRoute().query.url as string
if (_queryURL && !urls.includes(_queryURL)) {
urls.push(_queryURL)
Expand Down
17 changes: 11 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions src/commands/dev-child.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { resolve } from 'pathe'
import { consola } from 'consola'
import { overrideEnv } from '../utils/env'
import { defineCommand } from 'citty'
import { sharedArgs, legacyRootDirArgs } from './_shared'
import { isTest } from 'std-env'
import { NuxtDevIPCMessage, createNuxtDevServer } from '../utils/dev'
import type { ListenURL, HTTPSOptions } from 'listhen'

export type DevChildContext = {
url?: string
urls?: ListenURL[]
https?: boolean | HTTPSOptions
}

export default defineCommand({
meta: {
name: '_dev',
description:
'Run nuxt development server (internal command to start child process)',
},
args: {
...sharedArgs,
...legacyRootDirArgs,
},
async run(ctx) {
const logger = consola.withTag('nuxi')

if (!process.send && !isTest) {
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 host info
const devProxyOptions: DevChildContext =
JSON.parse(process.env.__NUXT_DEV_PROXY__ || 'null') || {}

// Init Nuxt dev
const nuxtDev = await createNuxtDevServer({
cwd,
overrides: ctx.data?.overrides,
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
clear: !!ctx.args.clear,
dotenv: !!ctx.args.dotenv,
https: devProxyOptions.https,
})

// IPC Hooks
function sendIPCMessage<T extends NuxtDevIPCMessage>(message: T) {
if (process.send) {
process.send(message)
} else {
logger.info(
'Dev server event:',
Object.entries(message)
.map((e) => e[0] + '=' + JSON.stringify(e[1]))
.join(' '),
)
}
}
nuxtDev.on('loading', (message) => {
sendIPCMessage({ type: 'nuxt:internal:dev:loading', message })
})
nuxtDev.on('restart', () => {
sendIPCMessage({ type: 'nuxt:internal:dev:restart' })
})
nuxtDev.on('ready', (payload) => {
sendIPCMessage({ type: 'nuxt:internal:dev:ready', port: payload.port })
})

// Init server
await nuxtDev.init()
},
})
228 changes: 0 additions & 228 deletions src/commands/dev-internal.ts

This file was deleted.

Loading

0 comments on commit 2191619

Please sign in to comment.