From 3e82f0f2cb0c815af898ad78118104a476ee1f8e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 10 Dec 2021 14:12:21 +0000 Subject: [PATCH] fix(bridge): normalize strings in vite middleware template (#2319) --- packages/bridge/src/vite/templates.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/bridge/src/vite/templates.ts b/packages/bridge/src/vite/templates.ts index f586c3a9af..845980bc7e 100644 --- a/packages/bridge/src/vite/templates.ts +++ b/packages/bridge/src/vite/templates.ts @@ -14,10 +14,16 @@ export const middlewareTemplate = { src: '', getContents (ctx: TemplateContext) { const { dir, router: { middleware }, srcDir } = ctx.nuxt.options - const _middleware = ((typeof middleware !== 'undefined' && middleware) || []).map(m => ({ - filePath: resolve(srcDir, dir.middleware, m.src), - id: m.name || m.src.replace(/[\\/]/g, '/').replace(/\.(js|ts)$/, '') - })) + const _middleware = ((typeof middleware !== 'undefined' && middleware) || []).map((m) => { + // Normalize string middleware + if (typeof m === 'string') { + m = { src: m } + } + return { + filePath: resolve(srcDir, dir.middleware, m.src), + id: m.name || m.src.replace(/[\\/]/g, '/').replace(/\.(js|ts)$/, '') + } + }) return `${_middleware.map(m => `import $${hash(m.id)} from '${m.filePath}'`).join('\n')} const middleware = { ${_middleware.map(m => ` ['${m.id}']: $${hash(m.id)}`).join(',\n')}