From 030273db498e058a7d8423f30d7fe08f29fb340c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 14 Jun 2017 00:01:30 +0430 Subject: [PATCH] improve build lifecycle --- bin/nuxt-start | 10 +--------- lib/builder.js | 35 +++++++++++++++++++++-------------- lib/nuxt.js | 2 +- lib/renderer.js | 13 ------------- 4 files changed, 23 insertions(+), 37 deletions(-) diff --git a/bin/nuxt-start b/bin/nuxt-start index 970c87d7fd..d1d6a0b16c 100755 --- a/bin/nuxt-start +++ b/bin/nuxt-start @@ -4,7 +4,6 @@ var fs = require('fs') var parseArgs = require('minimist') var Nuxt = require('../') var resolve = require('path').resolve -var join = require('path').join var argv = parseArgs(process.argv.slice(2), { alias: { @@ -56,14 +55,7 @@ if (typeof options.rootDir !== 'string') { } options.dev = false // Force production mode (no webpack middleware called) -var buildDir = join(options.rootDir, (options.buildDir || '.nuxt'), 'dist', 'server-bundle.json') -// Check if project is built for production -if (!fs.existsSync(buildDir)) { - console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console - process.exit(1) -} - var nuxt = module.exports = new Nuxt(options) var port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port var host = argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host -var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host) +module.exports = nuxt.server = new nuxt.Server(nuxt).listen(port, host) diff --git a/lib/builder.js b/lib/builder.js index a659aab4d0..ad5bd872f1 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -52,19 +52,25 @@ export default class Builder extends Tapable { colors: true } - // Register build hook on init when in dev mode - this.nuxt.plugin('init', () => { - // Don't await for builder in dev (faster startup) - this.build().catch(err => { - // eslint-disable-next-line no-console - console.error(err) - }) + // Register lifecycle hooks + this.nuxt.plugin('afterInit', () => { + if (this.nuxt.dev) { + // But don't await for builder on dev (faster startup) + this.build().catch(err => { + // eslint-disable-next-line no-console + console.error(err) + }) + } else { + return this.production() + } }) this._buildStatus = STATUS.INITIAL } async build () { + // Ensure nuxt initialized + await this.nuxt.init() // Avoid calling this method multiple times if (this._buildStatus === STATUS.BUILD_DONE) { return this @@ -111,7 +117,7 @@ export default class Builder extends Tapable { return this } - production () { + async production () { // Avoid calling this method multiple times if (this._buildStatus === STATUS.BUILD_DONE) { return this @@ -120,13 +126,14 @@ export default class Builder extends Tapable { const serverConfig = this.getWebpackServerConfig() const bundlePath = join(serverConfig.output.path, 'server-bundle.json') const manifestPath = join(serverConfig.output.path, 'client-manifest.json') - if (fs.existsSync(bundlePath) && fs.existsSync(manifestPath)) { - const bundle = fs.readFileSync(bundlePath, 'utf8') - const manifest = fs.readFileSync(manifestPath, 'utf8') - this.createRenderer(JSON.parse(bundle), JSON.parse(manifest)) - this.addAppTemplate() + if (!fs.existsSync(bundlePath) || !fs.existsSync(manifestPath)) { + console.error('[warning] No build files found, trying to build for production') // eslint-disable-line no-console + await this.build() } - // Flag to set that building is done + const bundle = fs.readFileSync(bundlePath, 'utf8') + const manifest = fs.readFileSync(manifestPath, 'utf8') + this.createRenderer(JSON.parse(bundle), JSON.parse(manifest)) + this.addAppTemplate() this._buildStatus = STATUS.BUILD_DONE return this } diff --git a/lib/nuxt.js b/lib/nuxt.js index d8c7c087a5..5e96727f77 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -103,7 +103,7 @@ export default class Nuxt extends Tapable { // Wait for all components to be ready await this.applyPluginsAsync('beforeInit') await this.applyPluginsAsync('init') - await this.applyPluginsAsync('afterInit') + this.applyPluginsAsync('afterInit') this.initialized = true return this } diff --git a/lib/renderer.js b/lib/renderer.js index 88511d9eba..fae6fec069 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -19,11 +19,6 @@ export default class Renderer extends Tapable { } async render (req, res) { - // Check if project is built for production - if (!this.nuxt.builder.renderer && !this.options.dev) { - console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console - process.exit(1) - } /* istanbul ignore if */ if (!this.nuxt.builder.renderer || !this.nuxt.builder.appTemplate) { return new Promise((resolve) => { @@ -120,8 +115,6 @@ export default class Renderer extends Tapable { } async renderRoute (url, context = {}) { - // Wait for nuxt.js to be ready - await this.nuxt.init() // Log rendered url debug(`Rendering url ${url}`) // Add url and isSever to the context @@ -156,12 +149,6 @@ export default class Renderer extends Tapable { } async renderAndGetWindow (url, opts = {}) { - if (!this.ready) { - // Wait for nuxt.js to be ready - await this.nuxt._init - this.ready = true - } - /* istanbul ignore if */ if (!jsdom) { try {