feat(nitro): allow extending nitro context

This commit is contained in:
Pooya Parsa 2021-04-19 22:39:11 +02:00
parent 75b412bf0f
commit bef9f82a8d
2 changed files with 20 additions and 8 deletions

View File

@ -20,8 +20,9 @@ export default function nuxt2CompatModule () {
nuxt.options.build.indicator = false
// Create contexts
const nitroContext = getNitroContext(nuxt.options, nuxt.options.nitro || {})
const nitroDevContext = getNitroContext(nuxt.options, { preset: 'dev' })
const nitroOptions = (nuxt.options as any).nitro || {}
const nitroContext = getNitroContext(nuxt.options, nitroOptions)
const nitroDevContext = getNitroContext(nuxt.options, { ...nitroOptions, preset: 'dev' })
// Connect hooks
nuxt.addHooks(nitroContext.nuxtHooks)
@ -65,8 +66,13 @@ export default function nuxt2CompatModule () {
}
})
// Resolve middleware
nuxt.hook('modules:done', () => {
// Wait for all modules to be ready
nuxt.hook('modules:done', async () => {
// Extend nitro with modules
await nuxt.callHook('nitro:context', nitroContext)
await nuxt.callHook('nitro:context', nitroDevContext)
// Resolve middleware
const { middleware, legacyMiddleware } = resolveMiddleware(nuxt)
if (nuxt.server) {
nuxt.server.setLegacyMiddleware(legacyMiddleware)

View File

@ -3,8 +3,9 @@ import type { Nuxt } from '@nuxt/kit'
export function initNitro (nuxt: Nuxt) {
// Create contexts
const nitroContext = getNitroContext(nuxt.options, (nuxt.options as any).nitro || {})
const nitroDevContext = getNitroContext(nuxt.options, { preset: 'dev' })
const nitroOptions = (nuxt.options as any).nitro || {}
const nitroContext = getNitroContext(nuxt.options, nitroOptions)
const nitroDevContext = getNitroContext(nuxt.options, { ...nitroOptions, preset: 'dev' })
nuxt.server = createDevServer(nitroDevContext)
@ -20,8 +21,13 @@ export function initNitro (nuxt: Nuxt) {
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset
// Resolve middleware
nuxt.hook('modules:done', () => {
// Wait for all modules to be ready
nuxt.hook('modules:done', async () => {
// Extend nitro with modules
await nuxt.callHook('nitro:context', nitroContext)
await nuxt.callHook('nitro:context', nitroDevContext)
// Resolve middleware
const { middleware, legacyMiddleware } = resolveMiddleware(nuxt)
nuxt.server.setLegacyMiddleware(legacyMiddleware)
nitroContext.middleware.push(...middleware)