fix(nitro): resolve alias for serverMiddleware

This commit is contained in:
Pooya Parsa 2021-04-04 19:54:12 +02:00
parent 6e1cb62231
commit c864c5a30c
3 changed files with 8 additions and 6 deletions

View File

@ -57,7 +57,7 @@ export default function nuxt2CompatModule () {
// Resolve middleware
nuxt.hook('modules:done', () => {
const { middleware, legacyMiddleware } = resolveMiddleware(nuxt.options.serverMiddleware)
const { middleware, legacyMiddleware } = resolveMiddleware(nuxt)
if (nuxt.server) {
nuxt.server.setLegacyMiddleware(legacyMiddleware)
}

View File

@ -2,7 +2,7 @@ import { resolve, join, extname } from 'upath'
import { joinURL } from 'ufo'
import globby from 'globby'
import { watch } from 'chokidar'
import { tryResolvePath } from '@nuxt/kit'
import { tryResolvePath, Nuxt } from '@nuxt/kit'
export interface ServerMiddleware {
route: string
@ -51,11 +51,11 @@ export function scanMiddleware (serverDir: string, onChange?: (results: ServerMi
return scan()
}
export function resolveMiddleware (serverMiddleware: any[]) {
export function resolveMiddleware (nuxt: Nuxt) {
const middleware: ServerMiddleware[] = []
const legacyMiddleware: ServerMiddleware[] = []
for (let m of serverMiddleware) {
for (let m of nuxt.options.serverMiddleware) {
if (typeof m === 'string') { m = { handler: m } }
const route = m.path || m.route || '/'
const handle = m.handler || m.handle
@ -67,7 +67,9 @@ export function resolveMiddleware (serverMiddleware: any[]) {
middleware.push({
...m,
handle: tryResolvePath(handle, {
extensions: ['.ts', '.js']
extensions: ['.ts', '.js'],
alias: nuxt.options.alias,
base: nuxt.options.srcDir
}),
route
})

View File

@ -22,7 +22,7 @@ export function initNitro (nuxt: Nuxt) {
// Resolve middleware
nuxt.hook('modules:done', () => {
const { middleware, legacyMiddleware } = resolveMiddleware(nuxt.options.serverMiddleware)
const { middleware, legacyMiddleware } = resolveMiddleware(nuxt)
nuxt.server.setLegacyMiddleware(legacyMiddleware)
nitroContext.middleware.push(...middleware)
nitroDevContext.middleware.push(...middleware)