imrpove CLI experience for OPEN url

This commit is contained in:
Pooya Parsa 2017-08-19 14:59:41 +04:30
parent 6c4803a840
commit a585d8cb35
2 changed files with 38 additions and 14 deletions

View File

@ -32,6 +32,9 @@ export default class Nuxt extends Tapable {
this.renderRoute = this.renderer.renderRoute.bind(this.renderer) this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer) this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer)
// Default Show Open if Nuxt is not listening
this.showOpen = () => {}
this._ready = this.ready().catch(this.errorHandler) this._ready = this.ready().catch(this.errorHandler)
} }
@ -49,6 +52,13 @@ export default class Nuxt extends Tapable {
} }
listen (port = 3000, host = 'localhost') { listen (port = 3000, host = 'localhost') {
// Update showOpen
this.showOpen = () => {
const _host = host === '0.0.0.0' ? 'localhost' : host
// eslint-disable-next-line no-console
console.log('\n' + chalk.bgGreen.black(' OPEN ') + chalk.green(` http://${_host}:${port}\n`))
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const server = this.renderer.app.listen({ port, host, exclusive: false }, err => { const server = this.renderer.app.listen({ port, host, exclusive: false }, err => {
/* istanbul ignore if */ /* istanbul ignore if */
@ -56,11 +66,6 @@ export default class Nuxt extends Tapable {
return reject(err) return reject(err)
} }
// Show Open URL
let _host = host === '0.0.0.0' ? 'localhost' : host
// eslint-disable-next-line no-console
console.log('\n' + chalk.bold(chalk.bgGreen.white(' OPEN ') + chalk.green(` http://${_host}:${port}\n`)))
// Close server on nuxt close // Close server on nuxt close
this.plugin('close', () => new Promise((resolve, reject) => { this.plugin('close', () => new Promise((resolve, reject) => {
// Destroy server by forcing every connection to be closed // Destroy server by forcing every connection to be closed

View File

@ -124,22 +124,41 @@ export default class Renderer extends Tapable {
get isReady () { get isReady () {
if (this.noSSR) { if (this.noSSR) {
return this.resources.spaTemplate return Boolean(this.resources.spaTemplate)
} }
return this.bundleRenderer && this.resources.ssrTemplate
return Boolean(this.bundleRenderer && this.resources.ssrTemplate)
}
get isResourcesAvailable () {
// Required for both
if (!this.resources.clientManifest) {
return false
}
// Required for SPA rendering
if (this.noSSR) {
return Boolean(this.resources.spaTemplate)
}
// Required for bundle renderer
return Boolean(this.resources.ssrTemplate && this.resources.serverBundle)
} }
createRenderer () { createRenderer () {
// Create Meta Renderer // Ensure resources are available
this.metaRenderer = new MetaRenderer(this.nuxt, this) if (!this.isResourcesAvailable) {
// Check if SSR is disabled
if (this.noSSR) {
return return
} }
// If resources are not yet provided // Create Meta Renderer
if (!this.resources.serverBundle || !this.resources.clientManifest) { this.metaRenderer = new MetaRenderer(this.nuxt, this)
// Show Open URL
this.nuxt.showOpen()
// Skip following steps if noSSR mode
if (this.noSSR) {
return return
} }