Nuxt/packages/nuxt3/src/nitro.ts

49 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-03-18 14:26:41 +00:00
// eslint-disable-next-line import/named
import { wpfs, getNitroContext, createDevServer, resolveMiddleware, build, prepare, generate } from '@nuxt/nitro'
2021-01-22 20:57:09 +00:00
import type { Nuxt } from './index'
export function initNitro (nuxt: Nuxt) {
// Create contexts
const nitroContext = getNitroContext(nuxt.options, nuxt.options.nitro || {})
2021-02-22 19:11:54 +00:00
const nitroDevContext = getNitroContext(nuxt.options, { preset: 'dev' })
2021-01-22 20:57:09 +00:00
nuxt.server = createDevServer(nitroDevContext)
// Connect hooks
nuxt.addHooks(nitroContext.nuxtHooks)
nuxt.hook('close', () => nitroContext._internal.hooks.callHook('close'))
nuxt.addHooks(nitroDevContext.nuxtHooks)
nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close'))
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset
// Resolve middleware
nuxt.hook('modules:done', () => {
const { middleware, legacyMiddleware } =
resolveMiddleware(nuxt.options.serverMiddleware, nuxt.resolver.resolvePath)
nuxt.server.setLegacyMiddleware(legacyMiddleware)
nitroContext.middleware.push(...middleware)
nitroDevContext.middleware.push(...middleware)
})
// nuxt build/dev
nuxt.hook('build:done', async () => {
if (nuxt.options.dev) {
await build(nitroDevContext)
} else if (!nitroContext._nuxt.isStatic) {
await prepare(nitroContext)
await generate(nitroContext)
await build(nitroContext)
}
})
// nude dev
if (nuxt.options.dev) {
nitroDevContext._internal.hooks.hook('nitro:compiled', () => { nuxt.server.watch() })
nuxt.hook('build:compile', ({ compiler }) => { compiler.outputFileSystem = wpfs })
nuxt.hook('server:devMiddleware', (m) => { nuxt.server.setDevMiddleware(m) })
}
}