mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
improve build lifecycle
This commit is contained in:
parent
dfa1d915d6
commit
030273db49
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user