Nuxt/packages/core/src/nuxt.js

233 lines
5.7 KiB
JavaScript
Raw Normal View History

import https from 'https'
2018-03-16 16:12:06 +00:00
import enableDestroy from 'server-destroy'
2018-03-16 19:11:24 +00:00
import _ from 'lodash'
2018-03-31 16:22:14 +00:00
import consola from 'consola'
import chalk from 'chalk'
import ip from 'ip'
2018-03-16 19:52:17 +00:00
import { Options, sequence } from '@nuxt/common'
2018-03-16 19:52:17 +00:00
import { version } from '../package.json'
2018-03-16 16:12:06 +00:00
import ModuleContainer from './module'
import Renderer from './renderer'
import Resolver from './resolver'
2017-06-20 11:44:47 +00:00
2018-03-16 16:12:06 +00:00
export default class Nuxt {
2017-10-30 17:41:22 +00:00
constructor(options = {}) {
this.options = Options.from(options)
2018-03-31 20:20:14 +00:00
this.readyMessage = null
this.initialized = false
2017-12-13 01:09:38 +00:00
2017-10-30 21:39:08 +00:00
// Hooks
this._hooks = {}
this.hook = this.hook.bind(this)
2017-06-11 14:17:36 +00:00
// Create instance of core components
2017-06-16 12:42:45 +00:00
this.moduleContainer = new ModuleContainer(this)
this.renderer = new Renderer(this)
this.resolver = new Resolver(this)
2017-06-11 14:17:36 +00:00
// Backward compatibility
this.render = this.renderer.app
2017-06-11 14:17:36 +00:00
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
2018-01-11 13:28:45 +00:00
this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(
this.renderer
)
this._ready = this.ready().catch((err) => {
2018-04-01 20:20:46 +00:00
consola.fatal(err)
2018-03-31 16:22:14 +00:00
})
2017-06-11 14:17:36 +00:00
}
2017-11-30 10:24:06 +00:00
static get version() {
return version
2017-11-30 10:24:06 +00:00
}
2017-10-30 17:41:22 +00:00
async ready() {
2017-06-15 14:59:26 +00:00
if (this._ready) {
return this._ready
}
2017-06-13 22:09:03 +00:00
2017-10-31 11:33:15 +00:00
// Add hooks
2018-03-16 19:11:24 +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
// Await for renderer to be ready
2017-10-30 17:41:22 +00:00
await this.renderer.ready()
2017-06-13 22:09:03 +00:00
2017-06-14 18:51:14 +00:00
this.initialized = true
2018-01-11 13:28:45 +00:00
// Call ready hook
2017-10-30 21:39:08 +00:00
await this.callHook('ready', this)
return this
2016-11-07 01:34:58 +00:00
}
2017-10-30 21:39:08 +00:00
hook(name, fn) {
2017-10-31 11:33:15 +00:00
if (!name || typeof fn !== 'function') {
return
}
if (name === 'render:context') {
name = 'render:routeContext'
consola.warn('render:context hook has been deprecated, please use render:routeContext')
}
2017-10-30 21:39:08 +00:00
this._hooks[name] = this._hooks[name] || []
this._hooks[name].push(fn)
}
async callHook(name, ...args) {
if (!this._hooks[name]) {
return
}
2018-04-01 20:20:46 +00:00
consola.debug(`Call ${name} hooks (${this._hooks[name].length})`)
try {
2018-01-11 13:28:45 +00:00
await sequence(this._hooks[name], fn => fn(...args))
} catch (err) {
2018-04-01 20:20:46 +00:00
consola.error(err)
2018-03-31 16:22:14 +00:00
this.callHook('error', err)
}
2017-10-30 21:39:08 +00:00
}
2018-08-15 11:48:34 +00:00
clearHook(name) {
if (name) {
delete this._hooks[name]
}
}
2018-08-20 14:20:45 +00:00
flatHooks(configHooks, hooks = {}, parentName) {
Object.keys(configHooks).forEach((key) => {
const subHook = configHooks[key]
const name = parentName ? `${parentName}:${key}` : key
if (typeof subHook === 'object' && subHook !== null) {
this.flatHooks(subHook, hooks, name)
} else {
2018-08-20 14:20:45 +00:00
hooks[name] = subHook
}
})
2018-08-20 14:20:45 +00:00
return hooks
}
2018-08-20 14:20:45 +00:00
addHooks(configHooks) {
const hooks = this.flatHooks(configHooks)
Object.keys(hooks).filter(Boolean).forEach((key) => {
[].concat(hooks[key]).forEach(h => this.hook(key, h))
2017-10-31 11:33:15 +00:00
})
}
2018-03-31 20:20:14 +00:00
showReady(clear = true) {
if (!this.readyMessage) {
return
}
2018-04-01 20:20:46 +00:00
consola.ready({
2018-03-31 16:22:14 +00:00
message: this.readyMessage,
badge: true,
clear
})
2018-08-16 09:36:54 +00:00
this.readyMessage = null
}
listen(port, host, socket) {
return this.ready().then(() => new Promise((resolve, reject) => {
if (!socket && typeof this.options.server.socket === 'string') {
socket = this.options.server.socket
}
const args = { exclusive: false }
if (socket) {
args.path = socket
} else {
args.port = port || this.options.server.port
args.host = host || this.options.server.host
}
let appServer
const isHttps = Boolean(this.options.server.https)
if (isHttps) {
let httpsOptions
if (this.options.server.https === true) {
httpsOptions = {}
} else {
httpsOptions = this.options.server.https
}
appServer = https.createServer(httpsOptions, this.renderer.app)
} else {
appServer = this.renderer.app
}
const server = appServer.listen(
args,
(err) => {
2018-01-11 13:28:45 +00:00
/* istanbul ignore if */
if (err) {
return reject(err)
}
let listenURL
if (!socket) {
({ address: host, port } = server.address())
if (host === '127.0.0.1') {
host = 'localhost'
} else if (host === '0.0.0.0') {
host = ip.address()
}
listenURL = chalk.underline.blue(`http${isHttps ? 's' : ''}://${host}:${port}`)
this.readyMessage = `Listening on ${listenURL}`
} else {
listenURL = chalk.underline.blue(`unix+http://${socket}`)
this.readyMessage = `Listening on ${listenURL}`
}
2018-01-11 13:28:45 +00:00
// Close server on nuxt close
this.hook(
'close',
() =>
new Promise((resolve, reject) => {
// Destroy server by forcing every connection to be closed
2018-08-15 11:48:34 +00:00
server.listening && server.destroy((err) => {
2018-04-01 20:20:46 +00:00
consola.debug('server closed')
2018-01-11 13:28:45 +00:00
/* istanbul ignore if */
if (err) {
return reject(err)
}
resolve()
})
})
)
if (socket) {
this.callHook('listen', server, { path: socket }).then(resolve)
} else {
this.callHook('listen', server, { port, host }).then(resolve)
}
2017-06-20 11:44:47 +00:00
}
2018-01-11 13:28:45 +00:00
)
2017-07-17 19:26:41 +00:00
2017-06-20 13:07:38 +00:00
// Add server.destroy(cb) method
enableDestroy(server)
}))
2017-06-20 11:44:47 +00:00
}
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-20 13:12:33 +00:00
/* istanbul ignore if */
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
}
2016-11-07 01:34:58 +00:00
}
}