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)) { if (this.dev && isUrl(this.options.build.publicPath)) {
this.options.build.publicPath = defaults.publicPath this.options.build.publicPath = defaults.publicPath
} }
}
export function production () {
// Production, create server-renderer // Production, create server-renderer
if (!this.dev) { webpackStats = {
webpackStats = { chunks: false,
chunks: false, children: false,
children: false, modules: false,
modules: false, colors: true
colors: true }
} const serverConfig = getWebpackServerConfig.call(this)
const serverConfig = getWebpackServerConfig.call(this) const bundlePath = join(serverConfig.output.path, 'server-bundle.json')
const bundlePath = join(serverConfig.output.path, 'server-bundle.json') const manifestPath = join(serverConfig.output.path, 'client-manifest.json')
const manifestPath = join(serverConfig.output.path, 'client-manifest.json') if (fs.existsSync(bundlePath) && fs.existsSync(manifestPath)) {
if (fs.existsSync(bundlePath) && fs.existsSync(manifestPath)) { const bundle = fs.readFileSync(bundlePath, 'utf8')
const bundle = fs.readFileSync(bundlePath, 'utf8') const manifest = fs.readFileSync(manifestPath, 'utf8')
const manifest = fs.readFileSync(manifestPath, 'utf8') createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest))
createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest)) addAppTemplate.call(this)
addAppTemplate.call(this)
}
} }
} }

View File

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

View File

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

View File

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

View File

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