Nuxt/packages/core/src/nuxt.js

116 lines
2.7 KiB
JavaScript
Raw Normal View History

import isPlainObject from 'lodash/isPlainObject'
2018-03-31 16:22:14 +00:00
import consola from 'consola'
2019-08-21 19:46:40 +00:00
import Hookable from 'hable'
2018-03-16 19:52:17 +00:00
2018-12-22 21:05:13 +00:00
import { defineAlias } from '@nuxt/utils'
import { getNuxtConfig } from '@nuxt/config'
import { Server } from '@nuxt/server'
2018-03-16 19:52:17 +00:00
import { version } from '../package.json'
2018-12-22 21:05:13 +00:00
2018-03-16 16:12:06 +00:00
import ModuleContainer from './module'
import Resolver from './resolver'
2017-06-20 11:44:47 +00:00
export default class Nuxt extends Hookable {
constructor (options = {}) {
2019-08-21 19:46:40 +00:00
super(consola)
2017-12-13 01:09:38 +00:00
// Assign options and apply defaults
this.options = getNuxtConfig(options)
2017-06-11 14:17:36 +00:00
// Create instance of core components
this.resolver = new Resolver(this)
this.moduleContainer = new ModuleContainer(this)
// Deprecated hooks
2019-08-21 19:46:40 +00:00
this.deprecateHooks({
// #3294 - 7514db73b25c23b8c14ebdafbb4e129ac282aabd
'render:context': {
to: '_render:context',
message: '`render:context(nuxt)` is deprecated, Please use `vue-renderer:ssr:context(context)`'
},
// #3773
'render:routeContext': {
to: '_render:context',
message: '`render:routeContext(nuxt)` is deprecated, Please use `vue-renderer:ssr:context(context)`'
},
showReady: 'webpack:done'
2019-08-21 19:46:40 +00:00
})
2017-06-11 14:17:36 +00:00
// Add Legacy aliases
2018-11-08 09:15:56 +00:00
defineAlias(this, this.resolver, ['resolveAlias', 'resolvePath'])
this.showReady = () => { this.callHook('webpack:done') }
// Init server
if (this.options.server !== false) {
this._initServer()
}
// Call ready
if (this.options._ready !== false) {
this.ready().catch((err) => {
consola.fatal(err)
})
}
2017-06-11 14:17:36 +00:00
}
static get version () {
return `v${version}` + (global.__NUXT_DEV__ ? '-development' : '')
2017-11-30 10:24:06 +00:00
}
ready () {
if (!this._ready) {
this._ready = this._init()
}
return this._ready
}
async _init () {
if (this._initCalled) {
return this
}
this._initCalled = true
2017-06-13 22:09:03 +00:00
2017-10-31 11:33:15 +00:00
// Add hooks
if (isPlainObject(this.options.hooks)) {
2018-08-20 14:20:45 +00:00
this.addHooks(this.options.hooks)
2017-10-31 11:33:15 +00:00
} else if (typeof this.options.hooks === 'function') {
2017-10-30 21:39:08 +00:00
this.options.hooks(this.hook)
}
2018-01-11 13:28:45 +00:00
// Await for modules
2017-10-30 17:41:22 +00:00
await this.moduleContainer.ready()
2018-01-11 13:28:45 +00:00
// Await for server to be ready
if (this.server) {
await this.server.ready()
}
2018-01-11 13:28:45 +00:00
// Call ready hook
2017-10-30 21:39:08 +00:00
await this.callHook('ready', this)
return this
2016-11-07 01:34:58 +00:00
}
_initServer () {
if (this.server) {
return
}
this.server = new Server(this)
this.renderer = this.server
this.render = this.server.app
defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
}
async close (callback) {
2017-10-30 21:39:08 +00:00
await this.callHook('close', this)
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
}
this.clearHooks()
2016-11-07 01:34:58 +00:00
}
}