2020-08-17 09:13:46 +00:00
|
|
|
import type { IncomingHttpHeaders } from 'http'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
|
|
|
import isPlainObject from 'lodash/isPlainObject'
|
|
|
|
import consola from 'consola'
|
2020-07-30 23:40:16 +00:00
|
|
|
import Hookable from 'hookable'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2020-08-04 10:19:42 +00:00
|
|
|
import { getNuxtConfig, Configuration, NormalizedConfiguration } from 'src/config'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
|
|
|
import { version } from '../../package.json'
|
|
|
|
|
|
|
|
import ModuleContainer from './module'
|
|
|
|
import Resolver from './resolver'
|
2021-01-22 20:57:09 +00:00
|
|
|
import { initNitro } from './nitro'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2020-08-02 15:50:35 +00:00
|
|
|
declare global {
|
2020-08-17 19:12:34 +00:00
|
|
|
namespace NodeJS {
|
|
|
|
interface Global {
|
|
|
|
__NUXT_DEV__: boolean
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 13:02:35 +00:00
|
|
|
export default class Nuxt extends Hookable {
|
2020-07-30 23:40:16 +00:00
|
|
|
_ready?: Promise<this>
|
|
|
|
_initCalled?: boolean
|
|
|
|
|
2020-08-17 09:13:46 +00:00
|
|
|
error?: Error & { statusCode?: number, headers?: IncomingHttpHeaders }
|
2020-08-02 15:50:35 +00:00
|
|
|
options: NormalizedConfiguration
|
2020-07-30 23:40:16 +00:00
|
|
|
resolver: Resolver
|
|
|
|
moduleContainer: ModuleContainer
|
2021-01-18 11:49:50 +00:00
|
|
|
server?: any
|
|
|
|
renderer?: any
|
|
|
|
render?: any['app']
|
2020-07-30 23:40:16 +00:00
|
|
|
showReady?: () => void
|
|
|
|
|
2020-08-04 10:26:22 +00:00
|
|
|
constructor (options: Configuration = {}) {
|
2020-07-02 13:02:35 +00:00
|
|
|
super(consola)
|
|
|
|
|
|
|
|
// Assign options and apply defaults
|
|
|
|
this.options = getNuxtConfig(options)
|
|
|
|
|
|
|
|
// Create instance of core components
|
|
|
|
this.resolver = new Resolver(this)
|
|
|
|
this.moduleContainer = new ModuleContainer(this)
|
|
|
|
|
|
|
|
// Call ready
|
|
|
|
if (this.options._ready !== false) {
|
|
|
|
this.ready().catch((err) => {
|
|
|
|
consola.fatal(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 10:26:22 +00:00
|
|
|
static get version () {
|
2020-07-02 13:02:35 +00:00
|
|
|
return `v${version}` + (global.__NUXT_DEV__ ? '-development' : '')
|
|
|
|
}
|
|
|
|
|
2020-08-04 10:26:22 +00:00
|
|
|
ready () {
|
2020-07-02 13:02:35 +00:00
|
|
|
if (!this._ready) {
|
|
|
|
this._ready = this._init()
|
|
|
|
}
|
|
|
|
return this._ready
|
|
|
|
}
|
|
|
|
|
2020-08-04 10:26:22 +00:00
|
|
|
async _init () {
|
2020-07-02 13:02:35 +00:00
|
|
|
if (this._initCalled) {
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
this._initCalled = true
|
|
|
|
|
|
|
|
// Add hooks
|
2020-08-02 15:50:35 +00:00
|
|
|
if (this.options.hooks instanceof Function) {
|
2020-07-02 13:02:35 +00:00
|
|
|
this.options.hooks(this.hook)
|
2020-08-02 15:50:35 +00:00
|
|
|
} else if (isPlainObject(this.options.hooks)) {
|
|
|
|
this.addHooks(this.options.hooks)
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 20:57:09 +00:00
|
|
|
// Await for server
|
|
|
|
await initNitro(this)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-02-18 10:47:23 +00:00
|
|
|
// Await for modules
|
|
|
|
await this.moduleContainer.ready()
|
|
|
|
|
2020-07-02 13:02:35 +00:00
|
|
|
// Call ready hook
|
|
|
|
await this.callHook('ready', this)
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2020-08-04 10:26:22 +00:00
|
|
|
async close (callback?: () => any | Promise<any>) {
|
2020-07-02 13:02:35 +00:00
|
|
|
await this.callHook('close', this)
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
await callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|