Nuxt/packages/nuxt3/src/core/nuxt.ts

129 lines
3.0 KiB
TypeScript
Raw Normal View History

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:06:44 +00:00
import { defineAlias } from 'src/utils'
2020-08-04 10:19:42 +00:00
import { getNuxtConfig, Configuration, NormalizedConfiguration } from 'src/config'
2020-08-04 10:06:44 +00:00
import { Server } from 'src/server'
2020-07-02 13:02:35 +00:00
import { version } from '../../package.json'
import ModuleContainer from './module'
import Resolver from './resolver'
2020-08-02 15:50:35 +00:00
declare global {
var __NUXT_DEV__: boolean
}
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-02 15:50:35 +00:00
options: NormalizedConfiguration
2020-07-30 23:40:16 +00:00
resolver: Resolver
moduleContainer: ModuleContainer
server?: Server
renderer?: Server
render?: Server['app']
showReady?: () => void
2020-08-02 15:50:35 +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)
// Deprecated hooks
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'
})
// Add Legacy aliases
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)
})
}
}
2020-08-02 15:50:35 +00:00
static get version() {
2020-07-02 13:02:35 +00:00
return `v${version}` + (global.__NUXT_DEV__ ? '-development' : '')
}
2020-08-02 15:50:35 +00:00
ready() {
2020-07-02 13:02:35 +00:00
if (!this._ready) {
this._ready = this._init()
}
return this._ready
}
2020-08-02 15:50:35 +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
}
// Await for modules
await this.moduleContainer.ready()
// Await for server to be ready
if (this.server) {
await this.server.ready()
}
// Call ready hook
await this.callHook('ready', this)
return this
}
2020-08-02 15:50:35 +00:00
_initServer() {
2020-07-02 13:02:35 +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'])
}
2020-08-02 15:50:35 +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()
}
}
}