2017-06-13 17:58:04 +00:00
|
|
|
import Tapable from 'tappable'
|
2017-06-20 11:44:47 +00:00
|
|
|
import chalk from 'chalk'
|
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-16 12:42:45 +00:00
|
|
|
import Options from './options'
|
2017-06-20 11:44:47 +00:00
|
|
|
import Debug from 'debug'
|
|
|
|
|
|
|
|
const debug = Debug('nuxt:')
|
|
|
|
debug.color = 5
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
export default class Nuxt extends Tapable {
|
2017-06-13 22:52:30 +00:00
|
|
|
constructor (_options = {}) {
|
2017-06-11 14:17:36 +00:00
|
|
|
super()
|
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
this.options = Options(_options)
|
2017-06-13 22:52:30 +00:00
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
this.initialized = false
|
|
|
|
this.errorHandler = this.errorHandler.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-06-15 22:38:43 +00:00
|
|
|
this._ready = this.ready()
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 14:59:26 +00:00
|
|
|
async ready () {
|
|
|
|
if (this._ready) {
|
|
|
|
return this._ready
|
2017-06-04 12:08:36 +00:00
|
|
|
}
|
2017-06-13 22:09:03 +00:00
|
|
|
|
2017-06-13 17:58:04 +00:00
|
|
|
// Wait for all components to be ready
|
2017-06-14 18:51:14 +00:00
|
|
|
await this.applyPluginsAsync('beforeInit') // 1- Modules
|
2017-06-15 22:28:08 +00:00
|
|
|
await this.applyPluginsAsync('init') // 2- Builder
|
|
|
|
await this.applyPluginsAsync('afterInit') // 3- Renderer
|
2017-06-13 22:09:03 +00:00
|
|
|
|
2017-06-14 18:51:14 +00:00
|
|
|
this.initialized = true
|
2017-06-04 12:08:36 +00:00
|
|
|
return this
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
listen (port = 3000, host = 'localhost') {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const server = this.renderer.app.listen({ port, host, exclusive: false }, err => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show Open URL
|
|
|
|
let _host = host === '0.0.0.0' ? 'localhost' : host
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('\n' + chalk.bold(chalk.bgBlue.black(' OPEN ') + chalk.blue(` http://${_host}:${port}\n`)))
|
|
|
|
|
|
|
|
// Close server on nuxt close
|
|
|
|
this.plugin('close', () => new Promise((_resolve, _reject) => {
|
|
|
|
server.close(_err => {
|
|
|
|
debug('server closed')
|
|
|
|
if (_err) {
|
|
|
|
return _reject(err)
|
|
|
|
}
|
|
|
|
_resolve()
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:47:31 +00:00
|
|
|
errorHandler /* istanbul ignore next */ () {
|
2017-06-13 22:09:03 +00:00
|
|
|
// Silent
|
|
|
|
if (this.options.errorHandler === false) {
|
|
|
|
return
|
|
|
|
}
|
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-06-14 16:13:43 +00:00
|
|
|
// Default handler
|
2017-06-13 22:09:03 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error.apply(this, arguments)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2017-06-13 19:54:23 +00:00
|
|
|
async close (callback) {
|
2017-06-15 22:19:53 +00:00
|
|
|
await this.applyPluginsAsync('close')
|
2017-06-14 16:13:43 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|