Nuxt/lib/nuxt.js

126 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-06-13 17:58:04 +00:00
import Tapable from 'tappable'
2017-06-15 22:19:53 +00:00
import chalk from 'chalk'
2017-06-12 20:32:34 +00:00
import * as Utils from './utils'
2017-06-12 20:16:27 +00:00
import Renderer from './renderer'
import ModuleContainer from './module-container'
import Server from './server'
import defaults from './defaults'
2016-11-07 01:34:58 +00:00
2017-06-15 22:19:53 +00:00
const defaultHost = process.env.HOST || process.env.npm_package_config_nuxt_host || 'localhost'
const defaultPort = process.env.PORT || process.env.npm_package_config_nuxt_port || '3000'
2017-06-11 14:17:36 +00:00
export default class Nuxt extends Tapable {
constructor (_options = {}) {
2017-06-11 14:17:36 +00:00
super()
this.options = defaults(_options)
this.initialized = false
this.errorHandler = this.errorHandler.bind(this)
2017-06-11 14:17:36 +00:00
// Create instance of core components
2017-06-13 17:58:04 +00:00
this.moduleContainer = new Nuxt.ModuleContainer(this)
2017-06-12 20:16:27 +00:00
this.renderer = new Nuxt.Renderer(this)
2017-06-11 14:17:36 +00:00
// Backward compatibility
this.render = this.renderer.render.bind(this.renderer)
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer)
2017-06-14 18:51:14 +00:00
// Builder is lazy loaded, so register plugin here
this.plugin('init', async () => {
// Call to build on dev
if (this.options.dev) {
this.builder.build().catch(this.errorHandler)
}
// If explicitly runBuild required
if (this.options.runBuild) {
await this.builder.build()
}
})
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-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
return this
2016-11-07 01:34:58 +00:00
}
get builder () {
if (this._builder) {
return this._builder
}
2017-06-15 22:19:53 +00:00
const Builder = require('./builder').default
this._builder = new Builder(this)
return this._builder
}
get generator () {
if (this._generator) {
return this._generator
}
const Generator = require('./generator').default
this._generator = new Generator(this)
return this._generator
}
generate () {
return this.generator.generate.apply(this.generator, arguments)
}
2017-06-13 22:09:03 +00:00
errorHandler () {
// Silent
if (this.options.errorHandler === false) {
return
}
// Custom errorHandler
2017-06-13 22:09:03 +00:00
if (typeof this.options.errorHandler === 'function') {
return this.options.errorHandler.apply(this, arguments)
}
// 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-15 22:19:53 +00:00
serverReady ({ host = defaultHost, port = defaultPort } = {}) {
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`)))
return this.applyPluginsAsync('serverReady').catch(this.errorHandler)
}
async close (callback) {
2017-06-15 22:19:53 +00:00
// Call for close
await this.applyPluginsAsync('close')
2017-06-15 22:19:53 +00:00
// Remove all references
delete this._generator
delete this._builder
2017-06-15 22:19:53 +00:00
this.initialized = false
if (typeof callback === 'function') {
callback()
}
2016-11-07 01:34:58 +00:00
}
}
2017-06-12 20:16:27 +00:00
// Add core components to Nuxt class
Nuxt.Utils = Utils
Nuxt.Renderer = Renderer
Nuxt.ModuleContainer = ModuleContainer
Nuxt.Server = Server