2017-06-13 17:58:04 +00:00
|
|
|
import Tapable from 'tappable'
|
2017-06-15 14:53:00 +00:00
|
|
|
import Builder from './builder'
|
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'
|
2017-06-14 16:13:43 +00:00
|
|
|
import defaults from './defaults'
|
2016-11-07 01:34:58 +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-14 16:13:43 +00:00
|
|
|
this.options = defaults(_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-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 14:59:26 +00:00
|
|
|
this._ready = this.ready().catch(this.errorHandler)
|
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
|
|
|
|
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-14 16:13:43 +00:00
|
|
|
get builder () {
|
|
|
|
if (this._builder) {
|
|
|
|
return this._builder
|
|
|
|
}
|
2017-06-15 14:53:00 +00:00
|
|
|
// const Builder = require('./builder').default
|
2017-06-14 16:13:43 +00:00
|
|
|
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
|
|
|
|
}
|
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) {
|
2016-11-07 01:34:58 +00:00
|
|
|
let promises = []
|
2016-12-21 14:27:30 +00:00
|
|
|
/* istanbul ignore if */
|
2016-11-07 01:34:58 +00:00
|
|
|
if (this.webpackDevMiddleware) {
|
|
|
|
const p = new Promise((resolve, reject) => {
|
|
|
|
this.webpackDevMiddleware.close(() => resolve())
|
|
|
|
})
|
|
|
|
promises.push(p)
|
|
|
|
}
|
2016-12-21 14:27:30 +00:00
|
|
|
/* istanbul ignore if */
|
2016-11-07 01:34:58 +00:00
|
|
|
if (this.webpackServerWatcher) {
|
|
|
|
const p = new Promise((resolve, reject) => {
|
|
|
|
this.webpackServerWatcher.close(() => resolve())
|
|
|
|
})
|
|
|
|
promises.push(p)
|
|
|
|
}
|
2016-12-21 14:27:30 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-21 17:18:48 +00:00
|
|
|
if (this.filesWatcher) {
|
|
|
|
this.filesWatcher.close()
|
2016-11-10 02:38:11 +00:00
|
|
|
}
|
2017-05-12 08:27:59 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (this.customFilesWatcher) {
|
|
|
|
this.customFilesWatcher.close()
|
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
|
|
|
promises.push(this.applyPluginsAsync('close'))
|
|
|
|
|
2017-05-18 08:45:15 +00:00
|
|
|
return Promise.all(promises).then(() => {
|
2016-11-07 01:34:58 +00:00
|
|
|
if (typeof callback === 'function') callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
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
|