2017-06-20 11:44:47 +00:00
|
|
|
import chalk from 'chalk'
|
2017-07-30 12:20:58 +00:00
|
|
|
import { Options } from 'common'
|
2017-10-30 21:39:08 +00:00
|
|
|
import { sequence } from 'utils'
|
2017-06-16 12:42:45 +00:00
|
|
|
import ModuleContainer from './module'
|
2017-06-12 20:16:27 +00:00
|
|
|
import Renderer from './renderer'
|
2017-06-20 11:44:47 +00:00
|
|
|
import Debug from 'debug'
|
2017-06-20 13:07:38 +00:00
|
|
|
import enableDestroy from 'server-destroy'
|
2017-07-22 12:30:31 +00:00
|
|
|
import Module from 'module'
|
2017-10-31 11:33:15 +00:00
|
|
|
import { isPlainObject } from 'lodash'
|
2017-06-29 16:36:22 +00:00
|
|
|
import { join, resolve } from 'path'
|
2017-06-20 11:44:47 +00:00
|
|
|
|
|
|
|
const debug = Debug('nuxt:')
|
|
|
|
debug.color = 5
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2017-10-30 21:39:08 +00:00
|
|
|
export default class Nuxt {
|
2017-10-30 17:41:22 +00:00
|
|
|
constructor(options = {}) {
|
|
|
|
this.options = Options.from(options)
|
2017-06-13 22:52:30 +00:00
|
|
|
|
2017-07-22 12:30:31 +00:00
|
|
|
// Paths for resolving requires from `rootDir`
|
|
|
|
this.nodeModulePaths = Module._nodeModulePaths(this.options.rootDir)
|
2017-07-21 23:40:38 +00:00
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
this.initialized = false
|
|
|
|
this.errorHandler = this.errorHandler.bind(this)
|
2017-10-30 21:39:08 +00:00
|
|
|
// Hooks
|
|
|
|
this._hooks = {}
|
|
|
|
this.hook = this.hook.bind(this)
|
2017-06-13 19:54:23 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Create instance of core components
|
2017-06-16 12:42:45 +00:00
|
|
|
this.moduleContainer = new ModuleContainer(this)
|
|
|
|
this.renderer = new Renderer(this)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
|
|
|
// Backward compatibility
|
2017-06-20 12:04:20 +00:00
|
|
|
this.render = this.renderer.app
|
2017-06-11 14:17:36 +00:00
|
|
|
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
|
|
|
|
this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer)
|
|
|
|
|
2017-07-17 19:26:41 +00:00
|
|
|
this._ready = this.ready().catch(this.errorHandler)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async ready() {
|
2017-06-15 14:59:26 +00:00
|
|
|
if (this._ready) {
|
|
|
|
return this._ready
|
2017-06-04 12:08:36 +00:00
|
|
|
}
|
2017-06-13 22:09:03 +00:00
|
|
|
|
2017-10-31 11:33:15 +00:00
|
|
|
// Add hooks
|
|
|
|
if (isPlainObject(this.options.hooks)) {
|
|
|
|
this.addObjectHooks(this.options.hooks)
|
|
|
|
} else if (typeof this.options.hooks === 'function') {
|
2017-10-30 21:39:08 +00:00
|
|
|
this.options.hooks(this.hook)
|
|
|
|
}
|
|
|
|
// Add nuxt modules
|
2017-10-30 17:41:22 +00:00
|
|
|
await this.moduleContainer.ready()
|
|
|
|
await this.renderer.ready()
|
2017-06-13 22:09:03 +00:00
|
|
|
|
2017-06-14 18:51:14 +00:00
|
|
|
this.initialized = true
|
2017-10-30 21:39:08 +00:00
|
|
|
await this.callHook('ready', this)
|
|
|
|
|
2017-06-04 12:08:36 +00:00
|
|
|
return this
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 21:39:08 +00:00
|
|
|
hook(name, fn) {
|
2017-10-31 11:33:15 +00:00
|
|
|
if (!name || typeof fn !== 'function') {
|
|
|
|
return
|
|
|
|
}
|
2017-10-30 21:39:08 +00:00
|
|
|
this._hooks[name] = this._hooks[name] || []
|
|
|
|
this._hooks[name].push(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
async callHook(name, ...args) {
|
|
|
|
if (!this._hooks[name]) {
|
|
|
|
return
|
|
|
|
}
|
2017-10-30 22:15:35 +00:00
|
|
|
debug(`Call ${name} hooks (${this._hooks[name].length})`)
|
|
|
|
try {
|
|
|
|
await sequence(this._hooks[name], (fn) => fn(...args))
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`> Error on hook "${name}":`) // eslint-disable-line no-console
|
|
|
|
console.error(err) // eslint-disable-line no-console
|
|
|
|
}
|
2017-10-30 21:39:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 11:33:15 +00:00
|
|
|
addObjectHooks(hooksObj) {
|
|
|
|
Object.keys(hooksObj).forEach((name) => {
|
|
|
|
let hooks = hooksObj[name]
|
|
|
|
hooks = (Array.isArray(hooks) ? hooks : [hooks])
|
|
|
|
|
|
|
|
hooks.forEach((hook) => {
|
|
|
|
this.hook(name, hook)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
listen(port = 3000, host = 'localhost') {
|
2017-06-20 11:44:47 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-30 21:39:08 +00:00
|
|
|
const server = this.renderer.app.listen({ port, host, exclusive: false }, (err) => {
|
2017-06-20 13:12:33 +00:00
|
|
|
/* istanbul ignore if */
|
2017-06-20 11:44:47 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
|
2017-10-30 14:42:59 +00:00
|
|
|
const _host = host === '0.0.0.0' ? 'localhost' : host
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('\n' + chalk.bgGreen.black(' OPEN ') + chalk.green(` http://${_host}:${port}\n`))
|
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Close server on nuxt close
|
2017-10-30 21:39:08 +00:00
|
|
|
this.hook('close', () => new Promise((resolve, reject) => {
|
2017-06-20 13:07:38 +00:00
|
|
|
// Destroy server by forcing every connection to be closed
|
|
|
|
server.destroy(err => {
|
2017-06-20 11:44:47 +00:00
|
|
|
debug('server closed')
|
2017-06-20 13:12:33 +00:00
|
|
|
/* istanbul ignore if */
|
2017-06-20 12:12:21 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
2017-06-20 11:44:47 +00:00
|
|
|
}
|
2017-06-20 12:12:21 +00:00
|
|
|
resolve()
|
2017-06-20 11:44:47 +00:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
2017-10-30 21:39:08 +00:00
|
|
|
this.callHook('listen', server, { port, host }).then(resolve)
|
2017-06-20 11:44:47 +00:00
|
|
|
})
|
2017-07-17 19:26:41 +00:00
|
|
|
|
2017-06-20 13:07:38 +00:00
|
|
|
// Add server.destroy(cb) method
|
|
|
|
enableDestroy(server)
|
2017-06-20 11:44:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
errorHandler/* istanbul ignore next */() {
|
2017-07-03 11:11:40 +00:00
|
|
|
// Apply plugins
|
|
|
|
// eslint-disable-next-line no-console
|
2017-10-30 21:39:08 +00:00
|
|
|
this.callHook('error', ...arguments).catch(console.error)
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-06-13 22:09:03 +00:00
|
|
|
// Silent
|
|
|
|
if (this.options.errorHandler === false) {
|
|
|
|
return
|
|
|
|
}
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
// Custom errorHandler
|
2017-06-13 22:09:03 +00:00
|
|
|
if (typeof this.options.errorHandler === 'function') {
|
|
|
|
return this.options.errorHandler.apply(this, arguments)
|
|
|
|
}
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
// Default handler
|
2017-06-13 22:09:03 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2017-07-03 11:11:40 +00:00
|
|
|
console.error(...arguments)
|
2017-06-13 22:09:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
resolvePath(path) {
|
2017-06-30 11:19:22 +00:00
|
|
|
// Try to resolve using NPM resolve path first
|
|
|
|
try {
|
2017-07-22 12:30:31 +00:00
|
|
|
let resolvedPath = Module._resolveFilename(path, { paths: this.nodeModulePaths })
|
2017-06-30 11:19:22 +00:00
|
|
|
return resolvedPath
|
|
|
|
} catch (e) {
|
|
|
|
// Just continue
|
|
|
|
}
|
2017-07-04 14:21:41 +00:00
|
|
|
// Shorthand to resolve from project dirs
|
|
|
|
if (path.indexOf('@@') === 0 || path.indexOf('~~') === 0) {
|
|
|
|
return join(this.options.rootDir, path.substr(2))
|
|
|
|
} else if (path.indexOf('@') === 0 || path.indexOf('~') === 0) {
|
2017-06-29 16:36:22 +00:00
|
|
|
return join(this.options.srcDir, path.substr(1))
|
|
|
|
}
|
|
|
|
return resolve(this.options.srcDir, path)
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async close(callback) {
|
2017-10-30 21:39:08 +00:00
|
|
|
await this.callHook('close', this)
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2017-06-20 13:12:33 +00:00
|
|
|
/* istanbul ignore if */
|
2017-06-15 22:19:53 +00:00
|
|
|
if (typeof callback === 'function') {
|
2017-06-16 12:42:45 +00:00
|
|
|
await callback()
|
2017-06-15 22:19:53 +00:00
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
}
|