2018-10-30 20:42:53 +00:00
|
|
|
|
2018-10-18 15:43:44 +00:00
|
|
|
import isPlainObject from 'lodash/isPlainObject'
|
2018-03-31 16:22:14 +00:00
|
|
|
import consola from 'consola'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-12-22 21:05:13 +00:00
|
|
|
import { defineAlias } from '@nuxt/utils'
|
2018-10-30 20:42:53 +00:00
|
|
|
import { getNuxtConfig } from '@nuxt/config'
|
|
|
|
import { Server } from '@nuxt/server'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-10-17 21:28:25 +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'
|
2018-12-22 21:05:13 +00:00
|
|
|
import Hookable from './hookable'
|
2018-10-17 21:28:25 +00:00
|
|
|
import Resolver from './resolver'
|
2017-06-20 11:44:47 +00:00
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
export default class Nuxt extends Hookable {
|
2017-10-30 17:41:22 +00:00
|
|
|
constructor(options = {}) {
|
2018-10-30 20:42:53 +00:00
|
|
|
super()
|
2017-12-13 01:09:38 +00:00
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Assign options and apply defaults
|
|
|
|
this.options = getNuxtConfig(options)
|
2017-06-13 19:54:23 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Create instance of core components
|
2018-10-17 21:28:25 +00:00
|
|
|
this.resolver = new Resolver(this)
|
2018-10-30 20:42:53 +00:00
|
|
|
this.moduleContainer = new ModuleContainer(this)
|
|
|
|
|
|
|
|
// Deprecated hooks
|
|
|
|
this._deprecatedHooks = {
|
2019-02-08 10:05:01 +00:00
|
|
|
'render:context': 'render:routeContext',
|
|
|
|
'render:routeContext': 'vue-renderer:afterRender',
|
2018-11-08 09:15:56 +00:00
|
|
|
'showReady': 'webpack:done' // Workaround to deprecate showReady
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2018-10-30 20:42:53 +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') }
|
2018-04-05 08:38:54 +00:00
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Init server
|
|
|
|
if (this.options.server !== false) {
|
|
|
|
this._initServer()
|
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 10:24:06 +00:00
|
|
|
static get version() {
|
2019-01-16 17:53:14 +00:00
|
|
|
return (global.__NUXT && global.__NUXT.version) || `v${version}`
|
2017-11-30 10:24:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
ready() {
|
|
|
|
if (!this._ready) {
|
|
|
|
this._ready = this._init().catch((err) => {
|
|
|
|
consola.fatal(err)
|
|
|
|
})
|
2017-06-04 12:08:36 +00:00
|
|
|
}
|
2019-03-08 20:43:23 +00:00
|
|
|
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
|
2018-10-18 15:43:44 +00:00
|
|
|
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
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Await for server to be ready
|
2019-03-08 20:43:23 +00:00
|
|
|
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)
|
|
|
|
|
2017-06-04 12:08:36 +00:00
|
|
|
return this
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:43:23 +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'])
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async close(callback) {
|
2017-10-30 21:39:08 +00:00
|
|
|
await this.callHook('close', this)
|
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
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
|
|
|
this.clearHooks()
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
}
|