-
Notifications
You must be signed in to change notification settings - Fork 47
/
dev.ts
329 lines (294 loc) · 9.22 KB
/
dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import type { RequestListener, ServerResponse } from 'node:http'
import EventEmitter from 'node:events'
import { relative, resolve, join } from 'pathe'
import chokidar from 'chokidar'
import { consola } from 'consola'
import { debounce } from 'perfect-debounce'
import { toNodeListener } from 'h3'
import {
HTTPSOptions,
ListenURL,
listen,
Listener,
ListenOptions,
} from 'listhen'
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import { loadKit } from '../utils/kit'
import { loadNuxtManifest, writeNuxtManifest } from '../utils/nuxt'
import { clearBuildDir } from '../utils/fs'
import { importModule } from './esm'
import { AddressInfo } from 'node:net'
export type NuxtDevIPCMessage =
| { type: 'nuxt:internal:dev:ready'; port: number }
| { type: 'nuxt:internal:dev:loading'; message: string }
| { type: 'nuxt:internal:dev:restart' }
export interface NuxtDevContext {
proxy?: {
url?: string
urls?: ListenURL[]
https?: boolean | HTTPSOptions
}
}
export interface NuxtDevServerOptions {
cwd: string
logLevel: 'silent' | 'info' | 'verbose'
dotenv: boolean
clear: boolean
overrides: NuxtConfig
port?: string | number
loadingTemplate?: ({ loading }: { loading: string }) => string
devContext: NuxtDevContext
}
export async function createNuxtDevServer(
options: NuxtDevServerOptions,
listenOptions?: Partial<ListenOptions>,
) {
// Initialize dev server
const devServer = new NuxtDevServer(options)
// Attach internal listener
devServer.listener = await listen(
devServer.handler,
listenOptions || {
port: options.port ?? 0,
hostname: '127.0.0.1',
showURL: false,
},
)
// Merge interface with public context
// @ts-expect-error
devServer.listener._url = devServer.listener.url
if (options.devContext.proxy?.url) {
devServer.listener.url = options.devContext.proxy.url
}
if (options.devContext.proxy?.urls) {
const _getURLs = devServer.listener.getURLs.bind(devServer.listener)
devServer.listener.getURLs = async () =>
Array.from(
new Set([...options.devContext.proxy!.urls!, ...(await _getURLs())]),
)
}
return devServer
}
// https://regex101.com/r/7HkR5c/1
const RESTART_RE =
/^(nuxt\.config\.[a-z0-9]+|\.nuxtignore|\.nuxtrc|\.config\/nuxt(\.config)?\.[a-z0-9]+)$/
class NuxtDevServer extends EventEmitter {
private _handler?: RequestListener
private _distWatcher?: chokidar.FSWatcher
private _currentNuxt?: Nuxt
private _loadingMessage?: string
loadDebounced: (reload?: boolean, reason?: string) => void
handler: RequestListener
listener: Listener
constructor(private options: NuxtDevServerOptions) {
super()
this.loadDebounced = debounce(this.load)
let _initResolve: () => void
const _initPromise = new Promise<void>((resolve) => {
_initResolve = resolve
})
this.once('ready', () => {
_initResolve()
})
this.handler = async (req, res) => {
await _initPromise
if (this._handler) {
this._handler(req, res)
} else {
this._renderError(res)
}
}
// @ts-ignore we set it in wrapper function
this.listener = undefined
}
async _renderError(res: ServerResponse, _error?: Error) {
res.statusCode = 503
res.setHeader('Content-Type', 'text/html')
const loadingTemplate =
this.options.loadingTemplate ||
this._currentNuxt?.options.devServer.loadingTemplate ||
(
await importModule('@nuxt/ui-templates', this.options.cwd).then(
(r) => r.loading,
)
).catch(() => {}) ||
((params: { loading: string }) => `<h2>${params.loading}</h2>`)
res.end(
loadingTemplate({
loading: _error?.toString() || this._loadingMessage || 'Loading...',
}),
)
}
async init() {
await this.load()
await this._watchConfig()
}
async load(reload?: boolean, reason?: string) {
try {
await this._load(reload, reason)
} catch (error) {
consola.error(`Cannot ${reload ? 'restart' : 'start'} nuxt: `, error)
this._handler = undefined
this._loadingMessage =
'Error while loading Nuxt. Please check console and fix errors.'
this.emit('loading', this._loadingMessage)
}
}
async _load(reload?: boolean, reason?: string) {
const action = reload ? 'Restarting' : 'Starting'
this._loadingMessage = `${reason ? reason + '. ' : ''}${action} Nuxt...`
this._handler = undefined
this.emit('loading', this._loadingMessage)
if (reload) {
consola.info(this._loadingMessage)
}
if (this._currentNuxt) {
await this._currentNuxt.close()
}
if (this._distWatcher) {
await this._distWatcher.close()
}
const kit = await loadKit(this.options.cwd)
this._currentNuxt = await kit.loadNuxt({
cwd: this.options.cwd,
dev: true,
ready: false,
overrides: {
logLevel: this.options.logLevel as 'silent' | 'info' | 'verbose',
vite: {
clearScreen: this.options.clear,
},
nitro: {
devErrorHandler: (error, event) => {
this._renderError(event.node.res, error)
},
},
...this.options.overrides,
},
})
// Connect Vite HMR
if (!process.env.NUXI_DISABLE_VITE_HMR) {
this._currentNuxt.hooks.hook(
'vite:extendConfig',
(config, { isClient }) => {
if (isClient && config.server) {
config.server.hmr = {
...(config.server.hmr as Exclude<
typeof config.server.hmr,
boolean
>),
protocol: undefined,
port: undefined,
host: undefined,
server: this.listener.server,
}
}
},
)
}
// Remove websocket handlers on close
this._currentNuxt.hooks.hookOnce('close', () => {
this.listener.server.removeAllListeners('upgrade')
})
// Write manifest and also check if we need cache invalidation
if (!reload) {
const previousManifest = await loadNuxtManifest(
this._currentNuxt.options.buildDir,
)
const newManifest = await writeNuxtManifest(this._currentNuxt)
if (
previousManifest &&
newManifest &&
previousManifest._hash !== newManifest._hash
) {
await clearBuildDir(this._currentNuxt.options.buildDir)
}
}
await this._currentNuxt.ready()
const unsub = this._currentNuxt.hooks.hook('restart', async (options) => {
unsub() // We use this instead of `hookOnce` for Nuxt Bridge support
if (options?.hard) {
this.emit('restart')
return
}
await this.load(true)
})
if ('upgrade' in this._currentNuxt.server) {
this.listener.server.on(
'upgrade',
async (req: any, socket: any, head: any) => {
if (
req.url.startsWith(
this._currentNuxt?.options.app.buildAssetsDir /* /_nuxt/ */,
)
) {
return // Skip for Vite HMR
}
await this._currentNuxt?.server.upgrade(req, socket, head)
},
)
}
await this._currentNuxt.hooks.callHook(
'listen',
this.listener.server,
this.listener,
)
// Sync internal server info to the internals
// It is important for vite-node to use the internal URL but public proto
const addr = this.listener.address
this._currentNuxt.options.devServer.host = addr.address
this._currentNuxt.options.devServer.port = addr.port
this._currentNuxt.options.devServer.url = _getAddressURL(
addr,
!!this.listener.https,
)
this._currentNuxt.options.devServer.https = this.options.devContext.proxy
?.https as boolean | { key: string; cert: string }
if (this.listener.https && !process.env.NODE_TLS_REJECT_UNAUTHORIZED) {
consola.warn(
'You might need `NODE_TLS_REJECT_UNAUTHORIZED=0` environment vairable to make https work.',
)
}
await Promise.all([
kit.writeTypes(this._currentNuxt).catch(console.error),
kit.buildNuxt(this._currentNuxt),
])
// Watch dist directory
this._distWatcher = chokidar.watch(
resolve(this._currentNuxt.options.buildDir, 'dist'),
{ ignoreInitial: true, depth: 0 },
)
this._distWatcher.on('unlinkDir', () => {
this.loadDebounced(true, '.nuxt/dist directory has been removed')
})
this._handler = toNodeListener(this._currentNuxt.server.app)
this.emit('ready', addr)
}
async _watchConfig() {
const configWatcher = chokidar.watch(
[this.options.cwd, join(this.options.cwd, '.config')],
{
ignoreInitial: true,
depth: 0,
},
)
configWatcher.on('all', (_event, _file) => {
const file = relative(this.options.cwd, _file)
if (file === (this.options.dotenv || '.env')) {
this.emit('restart')
}
if (RESTART_RE.test(file)) {
this.loadDebounced(true, `${file} updated`)
}
})
}
}
function _getAddressURL(addr: AddressInfo, https: boolean) {
const proto = https ? 'https' : 'http'
let host = addr.address.includes(':') ? `[${addr.address}]` : addr.address
if (host === '[::]') {
host = 'localhost' // Fix issues with Docker networking
}
const port = addr.port || 3000
return `${proto}://${host}:${port}/`
}