mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 16:43:55 +00:00
127 lines
3.2 KiB
JavaScript
127 lines
3.2 KiB
JavaScript
import Tapable from 'tappable'
|
|
import * as Utils from './utils'
|
|
import Renderer from './renderer'
|
|
import ModuleContainer from './module-container'
|
|
import Server from './server'
|
|
import defaults from './defaults'
|
|
|
|
export default class Nuxt extends Tapable {
|
|
constructor (_options = {}) {
|
|
super()
|
|
|
|
this.options = defaults(_options)
|
|
|
|
this.initialized = false
|
|
this.errorHandler = this.errorHandler.bind(this)
|
|
|
|
// Create instance of core components
|
|
this.moduleContainer = new Nuxt.ModuleContainer(this)
|
|
this.renderer = new Nuxt.Renderer(this)
|
|
|
|
// 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)
|
|
|
|
this._init = this.init().catch(this.errorHandler)
|
|
}
|
|
|
|
async init () {
|
|
if (this._init) {
|
|
return this._init
|
|
}
|
|
|
|
// Call to build on dev
|
|
if (this.options.dev) {
|
|
this.builder.build().catch(this.errorHandler)
|
|
}
|
|
|
|
// Wait for all components to be ready
|
|
await this.applyPluginsAsync('beforeInit')
|
|
await this.applyPluginsAsync('init')
|
|
this.initialized = true
|
|
this.applyPluginsAsync('afterInit').catch(this.errorHandler)
|
|
|
|
return this
|
|
}
|
|
|
|
get builder () {
|
|
if (this._builder) {
|
|
return this._builder
|
|
}
|
|
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
|
|
}
|
|
|
|
build () {
|
|
return this.builder.build.apply(this.builder, arguments)
|
|
}
|
|
|
|
generate () {
|
|
return this.generator.generate.apply(this.generator, arguments)
|
|
}
|
|
|
|
errorHandler () {
|
|
// Silent
|
|
if (this.options.errorHandler === false) {
|
|
return
|
|
}
|
|
// Custom errorHandler
|
|
if (typeof this.options.errorHandler === 'function') {
|
|
return this.options.errorHandler.apply(this, arguments)
|
|
}
|
|
// Default handler
|
|
// eslint-disable-next-line no-console
|
|
console.error.apply(this, arguments)
|
|
process.exit(1)
|
|
}
|
|
|
|
async close (callback) {
|
|
let promises = []
|
|
/* istanbul ignore if */
|
|
if (this.webpackDevMiddleware) {
|
|
const p = new Promise((resolve, reject) => {
|
|
this.webpackDevMiddleware.close(() => resolve())
|
|
})
|
|
promises.push(p)
|
|
}
|
|
/* istanbul ignore if */
|
|
if (this.webpackServerWatcher) {
|
|
const p = new Promise((resolve, reject) => {
|
|
this.webpackServerWatcher.close(() => resolve())
|
|
})
|
|
promises.push(p)
|
|
}
|
|
/* istanbul ignore if */
|
|
if (this.filesWatcher) {
|
|
this.filesWatcher.close()
|
|
}
|
|
/* istanbul ignore if */
|
|
if (this.customFilesWatcher) {
|
|
this.customFilesWatcher.close()
|
|
}
|
|
|
|
promises.push(this.applyPluginsAsync('close'))
|
|
|
|
return Promise.all(promises).then(() => {
|
|
if (typeof callback === 'function') callback()
|
|
})
|
|
}
|
|
}
|
|
|
|
// Add core components to Nuxt class
|
|
Nuxt.Utils = Utils
|
|
Nuxt.Renderer = Renderer
|
|
Nuxt.ModuleContainer = ModuleContainer
|
|
Nuxt.Server = Server
|