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'
|
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'
|
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-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 {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (options = {}) {
|
2019-08-21 19:46:40 +00:00
|
|
|
super(consola)
|
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
|
2019-08-21 19:46:40 +00:00
|
|
|
this.deprecateHooks({
|
2020-02-25 16:15:40 +00:00
|
|
|
// #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)`'
|
|
|
|
},
|
2020-07-16 15:10:54 +00:00
|
|
|
showReady: 'webpack:done',
|
|
|
|
// Introduced in 2.13
|
|
|
|
'export:done': 'generate:done',
|
|
|
|
'export:before': 'generate:before',
|
|
|
|
'export:extendRoutes': 'generate:extendRoutes',
|
|
|
|
'export:distRemoved': 'generate:distRemoved',
|
|
|
|
'export:distCopied': 'generate:distCopied',
|
|
|
|
'export:route': 'generate:route',
|
|
|
|
'export:routeFailed': 'generate:routeFailed',
|
|
|
|
'export:page': 'generate:page',
|
|
|
|
'export:routeCreated': 'generate:routeCreated'
|
2019-08-21 19:46:40 +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()
|
|
|
|
}
|
2019-03-30 18:53:56 +00:00
|
|
|
|
|
|
|
// Call ready
|
|
|
|
if (this.options._ready !== false) {
|
|
|
|
this.ready().catch((err) => {
|
|
|
|
consola.fatal(err)
|
|
|
|
})
|
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
static get version () {
|
2020-06-24 08:23:29 +00:00
|
|
|
return `v${version}` + (global.__NUXT_DEV__ ? '-development' : '')
|
2017-11-30 10:24:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
ready () {
|
2019-03-08 20:43:23 +00:00
|
|
|
if (!this._ready) {
|
2019-03-30 18:53:56 +00:00
|
|
|
this._ready = this._init()
|
2017-06-04 12:08:36 +00:00
|
|
|
}
|
2019-03-08 20:43:23 +00:00
|
|
|
return this._ready
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async _init () {
|
2019-03-08 20:43:23 +00:00
|
|
|
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-07-10 10:45:49 +00:00
|
|
|
_initServer () {
|
2019-03-08 20:43:23 +00:00
|
|
|
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'])
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +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
|
|
|
}
|
|
|
|
}
|