improve useMiddleware

This commit is contained in:
Pooya Parsa 2017-08-25 17:37:45 +04:30
parent a71ac0f41a
commit f933dd1f32

View File

@ -171,16 +171,25 @@ export default class Renderer extends Tapable {
useMiddleware (m) {
// Resolve
const $m = m
let src
if (typeof m === 'string') {
m = require(this.nuxt.resolvePath(m))
src = this.nuxt.resolvePath(m)
m = require(src)
}
// Handler
if (m && typeof m.handler === 'string') {
m.handler = require(this.nuxt.resolvePath(m.handler))
if (typeof m.handler === 'string') {
src = this.nuxt.resolvePath(m.handler)
m.handler = require(src)
}
// Use middleware
const handler = m.handler || m
const path = (this.options.router.base + (m.path ? m.path : '')).replace(/\/\//g, '/')
// Inject $src and $m to final handler
if (src) handler.$src = src
handler.$m = $m
// Use middleware
this.app.use(path, handler)
}