feat: swtich to h2 stack for dev server

This commit is contained in:
Pooya Parsa 2020-12-07 14:09:09 +01:00
parent 0199610834
commit 921bb15130

View File

@ -1,10 +1,9 @@
import { Worker } from 'worker_threads' import { Worker } from 'worker_threads'
import { Server } from 'http'
import { resolve } from 'upath' import { resolve } from 'upath'
import debounce from 'debounce' import debounce from 'debounce'
import connect from 'connect'
import getPort from 'get-port-please'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import { createApp } from '@nuxt/h2'
import { listen, Listener } from 'listhen'
import serveStatic from 'serve-static' import serveStatic from 'serve-static'
import { createProxy } from 'http-proxy' import { createProxy } from 'http-proxy'
import { stat } from 'fs-extra' import { stat } from 'fs-extra'
@ -48,7 +47,7 @@ export function createDevServer (sigmaContext: SigmaContext) {
} }
// App // App
const app = connect() const app = createApp()
// _nuxt and static // _nuxt and static
app.use(sigmaContext._nuxt.publicPath, serveStatic(resolve(sigmaContext._nuxt.buildDir, 'dist/client'))) app.use(sigmaContext._nuxt.publicPath, serveStatic(resolve(sigmaContext._nuxt.buildDir, 'dist/client')))
@ -86,16 +85,11 @@ export function createDevServer (sigmaContext: SigmaContext) {
}) })
// Listen // Listen
let listeners: Server[] = [] let listeners: Listener[] = []
async function listen (port) { const _listen = async (port) => {
port = await getPort({ name: 'nuxt' }) const listener = await listen(app, { port })
const listener = await new Promise<Server>((resolve, reject) => {
const l = app.listen(port, err => err ? reject(err) : resolve(l))
})
listeners.push(listener) listeners.push(listener)
return { return listener
url: 'http://localhost:' + port
}
} }
// Watch for dist and reload worker // Watch for dist and reload worker
@ -122,16 +116,14 @@ export function createDevServer (sigmaContext: SigmaContext) {
if (pendingWorker) { if (pendingWorker) {
await pendingWorker.terminate() await pendingWorker.terminate()
} }
await Promise.all(listeners.map(l => new Promise((resolve) => { await Promise.all(listeners.map(l => l.close()))
l.close(() => resolve(undefined))
})))
listeners = [] listeners = []
} }
sigmaContext._internal.hooks.hook('close', close) sigmaContext._internal.hooks.hook('close', close)
return { return {
reload, reload,
listen, listen: _listen,
close, close,
watch, watch,
setLoadingMiddleware, setLoadingMiddleware,