Skip to content

Commit

Permalink
feat(nitro): allow extending nitro context
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 19, 2021
1 parent 75b412b commit bef9f82
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 10 additions & 4 deletions packages/nitro/src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions packages/nuxt3/src/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit bef9f82

Please sign in to comment.