mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 17:43:59 +00:00
a616c09b22
* feat(test): enable tests in packages * fix: wait error hook * test: entry and hookable in core * fix(test): options snapshot in windows * refactor(test): simpilify jest.fn * test: module in core * test: complete module test * test: add test for resolver in core * test: update config snapshot * test: nuxt in core module
87 lines
2.1 KiB
JavaScript
87 lines
2.1 KiB
JavaScript
|
|
import isPlainObject from 'lodash/isPlainObject'
|
|
import consola from 'consola'
|
|
|
|
import { defineAlias } from '@nuxt/utils'
|
|
import { getNuxtConfig } from '@nuxt/config'
|
|
import { Server } from '@nuxt/server'
|
|
|
|
import { version } from '../package.json'
|
|
|
|
import ModuleContainer from './module'
|
|
import Hookable from './hookable'
|
|
import Resolver from './resolver'
|
|
|
|
export default class Nuxt extends Hookable {
|
|
constructor(options = {}) {
|
|
super()
|
|
|
|
// Assign options and apply defaults
|
|
this.options = getNuxtConfig(options)
|
|
|
|
// Create instance of core components
|
|
this.resolver = new Resolver(this)
|
|
this.moduleContainer = new ModuleContainer(this)
|
|
this.server = new Server(this)
|
|
|
|
// Deprecated hooks
|
|
this._deprecatedHooks = {
|
|
'render:context': 'render:routeContext', // #3773
|
|
'showReady': 'webpack:done' // Workaround to deprecate showReady
|
|
}
|
|
|
|
// Add Legacy aliases
|
|
defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
|
|
defineAlias(this, this.resolver, ['resolveAlias', 'resolvePath'])
|
|
this.renderer = this.server
|
|
this.render = this.server.app
|
|
this.showReady = () => { this.callHook('webpack:done') }
|
|
|
|
// Wait for Nuxt to be ready
|
|
this.initialized = false
|
|
this._ready = this.ready().catch((err) => {
|
|
consola.fatal(err)
|
|
})
|
|
}
|
|
|
|
static get version() {
|
|
return (global.__NUXT && global.__NUXT.version) || `v${version}`
|
|
}
|
|
|
|
async ready() {
|
|
if (this._ready) {
|
|
return this._ready
|
|
}
|
|
|
|
// Add hooks
|
|
if (isPlainObject(this.options.hooks)) {
|
|
this.addHooks(this.options.hooks)
|
|
} else if (typeof this.options.hooks === 'function') {
|
|
this.options.hooks(this.hook)
|
|
}
|
|
|
|
// Await for modules
|
|
await this.moduleContainer.ready()
|
|
|
|
// Await for server to be ready
|
|
await this.server.ready()
|
|
|
|
this.initialized = true
|
|
|
|
// Call ready hook
|
|
await this.callHook('ready', this)
|
|
|
|
return this
|
|
}
|
|
|
|
async close(callback) {
|
|
await this.callHook('close', this)
|
|
|
|
if (typeof callback === 'function') {
|
|
await callback()
|
|
}
|
|
|
|
this.clearHooks()
|
|
}
|
|
}
|