Nuxt/lib/core/nuxt.js

236 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-03-16 19:52:17 +00:00
import Module from 'module'
import path from 'path'
2018-03-16 16:12:06 +00:00
import Debug from 'debug'
import enableDestroy from 'server-destroy'
2018-03-16 19:11:24 +00:00
import _ from 'lodash'
2018-03-16 16:12:06 +00:00
import chalk from 'chalk'
2018-03-16 19:11:24 +00:00
import fs from 'fs-extra'
import clear from 'clear'
2018-03-16 19:52:17 +00:00
2018-03-16 16:12:06 +00:00
import Options from '../common/options'
import { sequence, printError } from '../common/utils'
2018-03-16 19:11:24 +00:00
import packageJSON from '../../package.json'
2018-03-16 19:52:17 +00:00
import moduleUtil from '../common/module'
2018-03-16 16:12:06 +00:00
import ModuleContainer from './module'
import Renderer from './renderer'
2017-06-20 11:44:47 +00:00
const debug = Debug('nuxt:')
debug.color = 5
2017-06-15 22:19:53 +00:00
const IS_WINDOWS = /^win/.test(process.platform)
const READY_ICON = IS_WINDOWS ? '' : '🚀'
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)
this.readyMessage = null
this.initialized = false
2018-01-11 16:11:50 +00:00
this.onError = this.onError.bind(this)
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)
2017-06-11 14:17:36 +00:00
// Backward compatibility
2018-01-11 16:11:50 +00:00
this.errorHandler = this.onError
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
)
2017-06-11 14:17:36 +00:00
2018-01-11 16:11:50 +00:00
this._ready = this.ready().catch(err => this.onError(err))
2017-06-11 14:17:36 +00:00
}
2017-11-30 10:24:06 +00:00
static get version() {
2018-03-16 19:11:24 +00:00
return packageJSON.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)) {
2017-10-31 11:33:15 +00:00
this.addObjectHooks(this.options.hooks)
} 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
}
2017-10-30 21:39:08 +00:00
this._hooks[name] = this._hooks[name] || []
this._hooks[name].push(fn)
}
2018-01-11 16:11:50 +00:00
onError(err, from = 'Nuxt error') {
// Log error to the console if there is not any error listener
if (!this._hooks['error']) {
printError(err, from)
return
}
// Call error hooks
this.callHook('error', err, from)
}
2017-10-30 21:39:08 +00:00
async callHook(name, ...args) {
if (!this._hooks[name]) {
return
}
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-01-11 16:11:50 +00:00
this.onError(err, name)
}
2017-10-30 21:39:08 +00:00
}
2017-10-31 11:33:15 +00:00
addObjectHooks(hooksObj) {
2018-01-11 13:28:45 +00:00
Object.keys(hooksObj).forEach(name => {
2017-10-31 11:33:15 +00:00
let hooks = hooksObj[name]
2018-01-11 13:28:45 +00:00
hooks = Array.isArray(hooks) ? hooks : [hooks]
2017-10-31 11:33:15 +00:00
2018-01-11 13:28:45 +00:00
hooks.forEach(hook => {
2017-10-31 11:33:15 +00:00
this.hook(name, hook)
})
})
}
showReady(doClear = false) {
if (!this.readyMessage || this.options.test) {
return
}
if (this.options.minimalCLI) {
// eslint-disable-next-line no-console
console.log('[READY] ', this.readyMessage)
} else {
if (doClear) {
clear()
}
process.stdout.write(
'\n' +
chalk.bgGreen.black(' READY ') +
chalk.green(` ${READY_ICON} ${this.readyMessage}\n`)
)
}
}
2017-10-30 17:41:22 +00:00
listen(port = 3000, host = 'localhost') {
2017-06-20 11:44:47 +00:00
return new Promise((resolve, reject) => {
2018-01-11 13:28:45 +00:00
const server = this.renderer.app.listen(
{ port, host, exclusive: false },
err => {
/* istanbul ignore if */
if (err) {
return reject(err)
}
this.readyMessage = `Listening on ${host}:${port}`
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
server.destroy(err => {
debug('server closed')
/* istanbul ignore if */
if (err) {
return reject(err)
}
resolve()
})
})
)
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
})
}
2018-03-16 19:11:24 +00:00
resolveAlias(_path) {
if (_path.indexOf('@@') === 0 || _path.indexOf('~~') === 0) {
return path.join(this.options.rootDir, _path.substr(2))
2018-01-11 19:43:34 +00:00
}
2018-03-16 19:11:24 +00:00
if (_path.indexOf('@') === 0 || _path.indexOf('~') === 0) {
return path.join(this.options.srcDir, _path.substr(1))
2018-01-11 19:43:34 +00:00
}
2018-03-16 19:11:24 +00:00
return path.resolve(this.options.srcDir, _path)
2018-01-11 19:43:34 +00:00
}
2018-03-16 19:11:24 +00:00
resolvePath(_path) {
// Try to resolve using NPM resolve path first
try {
2018-03-16 19:11:24 +00:00
const resolvedPath = Module._resolveFilename(_path, {
2018-01-11 13:28:45 +00:00
paths: this.options.modulesDir
})
return resolvedPath
} catch (error) {
2018-01-29 03:41:56 +00:00
if (error.code !== 'MODULE_NOT_FOUND') {
throw error
}
}
2018-01-11 13:28:45 +00:00
2018-03-16 19:11:24 +00:00
let __path = this.resolveAlias(_path)
2018-01-11 19:43:34 +00:00
2018-03-16 19:11:24 +00:00
if (fs.existsSync(__path)) {
return __path
}
2018-01-11 13:28:45 +00:00
2018-01-11 19:43:34 +00:00
for (let ext of this.options.extensions) {
2018-03-16 20:03:33 +00:00
if (fs.existsSync(__path + '.' + ext)) {
return __path + '.' + ext
}
}
2018-01-11 19:43:34 +00:00
2018-03-16 19:11:24 +00:00
throw new Error(`Cannot resolve "${_path}" from "${__path}"`)
}
2018-03-16 16:12:06 +00:00
requireModule(name) {
2018-03-16 19:11:24 +00:00
return moduleUtil.requireModule(this.resolvePath(name))
2018-03-16 16:12:06 +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
}
}