bug & compatibility fixes

This commit is contained in:
pimlie 2017-10-16 10:47:56 +02:00
parent 1765cca125
commit 5f5325c528
2 changed files with 21 additions and 15 deletions

View File

@ -5,6 +5,7 @@ process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
const fs = require('fs')
const parseArgs = require('minimist')
const debug = require('debug')('nuxt:generate')
const { Nuxt, Builder, Generator } = require('../')
const resolve = require('path').resolve
@ -13,13 +14,13 @@ const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
c: 'config-file',
j: 'workers'
w: 'workers',
s: 'spa',
u: 'universal'
},
boolean: ['h', 's', 'u'],
string: ['c'],
number: ['j'],
number: ['w'],
default: {
c: 'nuxt.config.js'
}
@ -34,8 +35,8 @@ if (argv.help) {
Options
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
--help, -h Displays this message
-j, --workers [NUM] How many workers should be started
(default: # cpus or 1 if omitted)
--workers,-w [NUM] How many workers should be started
(default: # cpus or 1 if omitted)
--spa Launch in SPA mode
--universal Launch in Universal mode (default)
`)
@ -190,7 +191,7 @@ if (numWorkers <= 1) {
// print report
let errors = []
for (let pid in watchdog) {
let worker_msg = `Worker ${pid} generated ${watchdog[pid].routes} routes in ${watchdog[pid].duration}ms`
let worker_msg = `Worker ${pid} generated ${watchdog[pid].routes} routes in ${Math.round(watchdog[pid].duration/100)/10}s`
if (watchdog[pid].errors.length) {
worker_msg += ` with ${watchdog[pid].errors.length} errors`
@ -199,8 +200,10 @@ if (numWorkers <= 1) {
debug(worker_msg)
}
generator.postGenerate()
duration = process.hrtime(start)
duration = Math.round((duration[0] * 1E9 + duration[1]) / 1E6)
duration = Math.round((duration[0] * 1E9 + duration[1]) / 1E8)/10
generator.printReport(duration, errors)
}

View File

@ -26,10 +26,10 @@ export default class Generator extends Tapable {
async generate ({ build = true, init = true } = {}) {
const s = Date.now()
await this.initiate({ build: build, init: init })
const routes = await this.initRoutes()
const errors = await this.generateRoutes(routes)
this.postGenerate()
const duration = Math.round((Date.now() - s) / 100) / 10
this.printReport(duration, errors)
@ -56,7 +56,9 @@ export default class Generator extends Tapable {
async initiate ({ build = true, init = true } = {}) {
// Add flag to set process.static
this.builder.forGenerate()
if (build) {
this.builder.forGenerate()
}
// Wait for nuxt be ready
await this.nuxt.ready()
@ -79,7 +81,7 @@ export default class Generator extends Tapable {
let generateRoutes = []
if (this.options.router.mode !== 'hash') {
try {
console.log('Generating routes') // eslint-disable-line no-console
debug('Generating routes') // eslint-disable-line no-console
generateRoutes = await promisifyRoute(this.options.generate.routes || [])
await this.applyPluginsAsync('generateRoutes', { generator: this, generateRoutes })
} catch (e) {
@ -108,19 +110,20 @@ export default class Generator extends Tapable {
await waitFor(n++ * this.options.generate.interval)
await this.generateRoute({ route, payload, errors })
}))
break // TODO ?
}
await this.applyPluginsAsync('generated', this)
return errors
}
async postGenerate() {
// Copy /index.html to /200.html for surge SPA
// https://surge.sh/help/adding-a-200-page-for-client-side-routing
const _200Path = join(this.distPath, '200.html')
if (!existsSync(_200Path)) {
await copy(join(this.distPath, 'index.html'), _200Path)
}
await this.applyPluginsAsync('generated', this)
return errors
}
async initDist () {
@ -130,7 +133,7 @@ export default class Generator extends Tapable {
// Copy static and built files
/* istanbul ignore if */
if (fs.existsSync(this.staticRoutes)) {
if (existsSync(this.staticRoutes)) {
await copy(this.staticRoutes, this.distPath)
}
await copy(this.srcBuiltPath, this.distNuxtPath)