mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
Merge branch 'dev' of github.com:Atinux/nuxt.js into dev
This commit is contained in:
commit
a90127952a
@ -10,6 +10,7 @@ class Module {
|
||||
constructor (nuxt) {
|
||||
this.nuxt = nuxt
|
||||
this.options = nuxt.options
|
||||
this.modules = []
|
||||
}
|
||||
|
||||
addVendor (vendor) {
|
||||
@ -75,7 +76,14 @@ class Module {
|
||||
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
|
||||
}
|
||||
|
||||
installModule (moduleOpts) {
|
||||
requireModule (moduleOpts) {
|
||||
if (this.modules.indexOf(moduleOpts) !== -1 || this.modules.indexOf(moduleOpts.src) !== -1) {
|
||||
return false
|
||||
}
|
||||
return this.addModule(moduleOpts)
|
||||
}
|
||||
|
||||
addModule (moduleOpts) {
|
||||
/* istanbul ignore if */
|
||||
if (!moduleOpts) {
|
||||
return
|
||||
@ -107,6 +115,8 @@ class Module {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('[Nuxt] Module should be a function', module)
|
||||
}
|
||||
// Add module to this.modules
|
||||
this.modules.push(module)
|
||||
// Call module with `this` context and pass options
|
||||
return new Promise((resolve, reject) => {
|
||||
const result = module.call(this, options, err => {
|
||||
|
@ -118,7 +118,7 @@ class Nuxt {
|
||||
// Add module integration
|
||||
this.module = new Module(this)
|
||||
// Install all modules in sequence and then return `this` instance
|
||||
return utils.sequence(options.modules, this.module.installModule.bind(this.module))
|
||||
return utils.sequence(options.modules, this.module.addModule.bind(this.module))
|
||||
.then(() => this)
|
||||
.catch(/* istanbul ignore next */ (err) => {
|
||||
console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
const http = require('http')
|
||||
const connect = require('connect')
|
||||
const path = require('path')
|
||||
|
||||
class Server {
|
||||
constructor (nuxt) {
|
||||
@ -11,17 +12,31 @@ class Server {
|
||||
this.server = http.createServer(this.app)
|
||||
// Add Middleware
|
||||
this.nuxt.options.serverMiddleware.forEach(m => {
|
||||
if (m instanceof Function) {
|
||||
this.app.use(m)
|
||||
} else if (m && m.path && m.handler) {
|
||||
this.app.use(m.path, m.handler)
|
||||
}
|
||||
this.useMiddleware(m)
|
||||
})
|
||||
// Add default render middleware
|
||||
this.app.use(this.render.bind(this))
|
||||
this.useMiddleware(this.render.bind(this))
|
||||
return this
|
||||
}
|
||||
|
||||
useMiddleware (m) {
|
||||
// Require if needed
|
||||
if (typeof m === 'string') {
|
||||
let src = m
|
||||
// Using ~ shorthand to resolve from project srcDir
|
||||
if (src.indexOf('~') === 0) {
|
||||
src = path.resolve(this.nuxt.options.srcDir, src.substr(1))
|
||||
}
|
||||
// eslint-disable-next-line no-eval
|
||||
m = eval('require')(src)
|
||||
}
|
||||
if (m instanceof Function) {
|
||||
this.app.use(m)
|
||||
} else if (m && m.path && m.handler) {
|
||||
this.app.use(m.path, m.handler)
|
||||
}
|
||||
}
|
||||
|
||||
render (req, res, next) {
|
||||
this.nuxt.render(req, res)
|
||||
return this
|
||||
|
Loading…
Reference in New Issue
Block a user