fix: modules called before renderer in production

This commit is contained in:
Sébastien Chopin 2017-06-04 14:08:36 +02:00
parent 5682eef2a5
commit f958801fff
5 changed files with 38 additions and 26 deletions

View File

@ -76,23 +76,24 @@ export function options () {
if (this.dev && isUrl(this.options.build.publicPath)) {
this.options.build.publicPath = defaults.publicPath
}
}
export function production () {
// Production, create server-renderer
if (!this.dev) {
webpackStats = {
chunks: false,
children: false,
modules: false,
colors: true
}
const serverConfig = getWebpackServerConfig.call(this)
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')
createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest))
addAppTemplate.call(this)
}
webpackStats = {
chunks: false,
children: false,
modules: false,
colors: true
}
const serverConfig = getWebpackServerConfig.call(this)
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')
createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest))
addAppTemplate.call(this)
}
}

View File

@ -42,6 +42,10 @@ export default async function () {
const s = Date.now()
let errors = []
/*
** Wait for modules to be initialized
*/
await this.ready()
/*
** Set variables
*/
this.options.generate = _.defaultsDeep(this.options.generate, defaults)

View File

@ -19,10 +19,11 @@ class Module {
async ready () {
if (this.initing) {
await this.initing
return
return this
}
// Install all modules in sequence
await sequence(this.options.modules, this.addModule.bind(this))
return this
}
addVendor (vendor) {

View File

@ -124,20 +124,25 @@ class Nuxt {
// Add module integration
this.module = new Module(this)
// Init nuxt.js
this.ready()
// Launch build in development but don't wait for him to be finished
if (this.dev) {
this.build()
}
this._ready = this.ready()
// Return nuxt.js instance
return this
}
async ready () {
if (this._ready) return this
if (this._ready) {
await this._ready
return this
}
// Init modules
await this.module.ready()
this._ready = true
// Launch build in development but don't wait for it to be finished
if (this.dev) {
this.build()
} else {
build.production.call(this)
}
return this
}
close (callback) {

View File

@ -11,6 +11,9 @@ debug.color = 4
setAnsiColors(ansiHTML)
export async function render (req, res) {
// Wait for nuxt.js to be ready
await this.ready()
// Check if project is built for production
if (!this.renderer && !this.dev) {
console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console
process.exit(1)
@ -23,8 +26,6 @@ export async function render (req, res) {
}, 1000)
})
}
// Wait for nuxt.js to be ready
await this.ready()
// Get context
const context = getContext(req, res)
res.statusCode = 200