2018-03-16 19:52:17 +00:00
|
|
|
import Module from 'module'
|
2018-07-19 11:26:52 +00:00
|
|
|
import { resolve, join } from 'path'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
import enableDestroy from 'server-destroy'
|
2018-03-16 19:11:24 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import fs from 'fs-extra'
|
2018-03-31 16:22:14 +00:00
|
|
|
import consola from 'consola'
|
|
|
|
import chalk from 'chalk'
|
2018-04-05 08:38:54 +00:00
|
|
|
import esm from 'esm'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
import Options from '../common/options'
|
2018-03-31 16:22:14 +00:00
|
|
|
import { sequence } from '../common/utils'
|
2018-03-16 19:11:24 +00:00
|
|
|
import packageJSON from '../../package.json'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
import ModuleContainer from './module'
|
|
|
|
import Renderer from './renderer'
|
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)
|
2017-06-13 22:52:30 +00:00
|
|
|
|
2018-03-31 20:20:14 +00:00
|
|
|
this.readyMessage = null
|
2017-06-14 16:13:43 +00:00
|
|
|
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-13 19:54:23 +00:00
|
|
|
|
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
|
2017-06-20 12:04:20 +00:00
|
|
|
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-04-05 08:38:54 +00:00
|
|
|
// ESM Loader
|
|
|
|
this.esm = esm(module, {})
|
|
|
|
|
2018-08-06 00:12:44 +00:00
|
|
|
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() {
|
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-04 12:08:36 +00:00
|
|
|
}
|
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)
|
|
|
|
|
2017-06-04 12:08:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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})`)
|
2017-10-30 22:15:35 +00:00
|
|
|
try {
|
2018-01-11 13:28:45 +00:00
|
|
|
await sequence(this._hooks[name], fn => fn(...args))
|
2017-10-30 22:15:35 +00:00
|
|
|
} 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 22:15:35 +00:00
|
|
|
}
|
2017-10-30 21:39:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 11:33:15 +00:00
|
|
|
addObjectHooks(hooksObj) {
|
2018-08-06 00:12:44 +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]
|
2018-08-06 00:12:44 +00:00
|
|
|
hooks.forEach(hook => this.hook(name, hook))
|
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-03-28 22:05:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
listen(port = 3000, host = 'localhost') {
|
2018-06-06 16:52:08 +00:00
|
|
|
return this.ready().then(() => new Promise((resolve, reject) => {
|
2018-01-11 13:28:45 +00:00
|
|
|
const server = this.renderer.app.listen(
|
|
|
|
{ port, host, exclusive: false },
|
2018-08-06 00:12:44 +00:00
|
|
|
(err) => {
|
2018-01-11 13:28:45 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:38:15 +00:00
|
|
|
({ address: host, port } = server.address())
|
2018-07-26 13:52:00 +00:00
|
|
|
if (['127.0.0.1', '0.0.0.0'].includes(host)) {
|
|
|
|
host = 'localhost'
|
|
|
|
}
|
|
|
|
|
2018-04-02 11:12:29 +00:00
|
|
|
const listenURL = chalk.underline.blue(`http://${host}:${port}`)
|
|
|
|
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-06 00:12:44 +00:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2018-06-06 16:52:08 +00:00
|
|
|
}))
|
2017-06-20 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 14:27:21 +00:00
|
|
|
resolveModule(path) {
|
2017-06-30 11:19:22 +00:00
|
|
|
try {
|
2018-01-11 18:49:54 +00:00
|
|
|
const resolvedPath = Module._resolveFilename(path, {
|
2018-01-11 13:28:45 +00:00
|
|
|
paths: this.options.modulesDir
|
|
|
|
})
|
2018-07-06 14:27:21 +00:00
|
|
|
|
2017-06-30 11:19:22 +00:00
|
|
|
return resolvedPath
|
2018-01-11 18:49:54 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'MODULE_NOT_FOUND') {
|
2018-07-06 14:27:21 +00:00
|
|
|
return null
|
2018-01-11 18:49:54 +00:00
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
2017-06-30 11:19:22 +00:00
|
|
|
}
|
2018-07-06 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 11:26:52 +00:00
|
|
|
resolveAlias(path) {
|
2018-07-06 14:27:21 +00:00
|
|
|
const modulePath = this.resolveModule(path)
|
|
|
|
|
2018-07-19 11:26:52 +00:00
|
|
|
// Try to resolve it as if it were a regular node_module
|
|
|
|
// Package first. Fixes issue with @<org> scoped packages
|
2018-07-06 14:27:21 +00:00
|
|
|
if (modulePath != null) {
|
|
|
|
return modulePath
|
|
|
|
}
|
2018-01-11 13:28:45 +00:00
|
|
|
|
2018-07-19 11:26:52 +00:00
|
|
|
if (path.indexOf('@@') === 0 || path.indexOf('~~') === 0) {
|
|
|
|
return join(this.options.rootDir, path.substr(2))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.indexOf('@') === 0 || path.indexOf('~') === 0) {
|
|
|
|
return join(this.options.srcDir, path.substr(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(this.options.srcDir, path)
|
|
|
|
}
|
2018-01-11 13:28:45 +00:00
|
|
|
|
2018-07-19 11:26:52 +00:00
|
|
|
resolvePath(path) {
|
2018-07-06 14:27:21 +00:00
|
|
|
const _path = this.resolveAlias(path)
|
2018-01-11 19:43:34 +00:00
|
|
|
|
2018-07-19 11:26:52 +00:00
|
|
|
if (fs.existsSync(_path)) {
|
2018-01-11 19:43:34 +00:00
|
|
|
return _path
|
2017-06-29 16:36:22 +00:00
|
|
|
}
|
2018-01-11 13:28:45 +00:00
|
|
|
|
2018-01-11 19:43:34 +00:00
|
|
|
for (let ext of this.options.extensions) {
|
2018-07-19 11:26:52 +00:00
|
|
|
if (fs.existsSync(_path + '.' + ext)) {
|
2018-01-11 19:43:34 +00:00
|
|
|
return _path + '.' + ext
|
2018-01-11 18:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-11 19:43:34 +00:00
|
|
|
|
|
|
|
throw new Error(`Cannot resolve "${path}" from "${_path}"`)
|
2017-06-29 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 08:38:54 +00:00
|
|
|
requireModule(_path, opts = {}) {
|
2018-07-18 14:39:48 +00:00
|
|
|
const _resolvedPath = this.resolvePath(_path)
|
|
|
|
const m = opts.esm === false ? require(_resolvedPath) : this.esm(_resolvedPath)
|
2018-04-05 08:38:54 +00:00
|
|
|
return (m && m.default) || m
|
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-14 16:13:43 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|