fix(bridge): normalize strings in vite middleware template (#2319)

This commit is contained in:
Daniel Roe 2021-12-10 14:12:21 +00:00 committed by GitHub
parent 0d85c9ef77
commit 3e82f0f2cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,10 +14,16 @@ export const middlewareTemplate = {
src: '', src: '',
getContents (ctx: TemplateContext) { getContents (ctx: TemplateContext) {
const { dir, router: { middleware }, srcDir } = ctx.nuxt.options const { dir, router: { middleware }, srcDir } = ctx.nuxt.options
const _middleware = ((typeof middleware !== 'undefined' && middleware) || []).map(m => ({ 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), filePath: resolve(srcDir, dir.middleware, m.src),
id: m.name || m.src.replace(/[\\/]/g, '/').replace(/\.(js|ts)$/, '') id: m.name || m.src.replace(/[\\/]/g, '/').replace(/\.(js|ts)$/, '')
})) }
})
return `${_middleware.map(m => `import $${hash(m.id)} from '${m.filePath}'`).join('\n')} return `${_middleware.map(m => `import $${hash(m.id)} from '${m.filePath}'`).join('\n')}
const middleware = { const middleware = {
${_middleware.map(m => ` ['${m.id}']: $${hash(m.id)}`).join(',\n')} ${_middleware.map(m => ` ['${m.id}']: $${hash(m.id)}`).join(',\n')}