Add generate.interval

This commit is contained in:
Sebastien Chopin 2017-04-25 15:00:42 +02:00
parent 296ea8ad4e
commit 10d18bc454
2 changed files with 5 additions and 19 deletions

View File

@ -5,7 +5,7 @@ import co from 'co'
import pify from 'pify' import pify from 'pify'
import _ from 'lodash' import _ from 'lodash'
import { resolve, join, dirname, sep } from 'path' import { resolve, join, dirname, sep } from 'path'
import { isUrl, promisifyRoute } from './utils' import { isUrl, promisifyRoute, waitFor } from './utils'
import { minify } from 'html-minifier' import { minify } from 'html-minifier'
const debug = require('debug')('nuxt:generate') const debug = require('debug')('nuxt:generate')
const copy = pify(fs.copy) const copy = pify(fs.copy)
@ -16,6 +16,7 @@ const mkdirp = pify(fs.mkdirp)
const defaults = { const defaults = {
dir: 'dist', dir: 'dist',
routes: [], routes: [],
interval: 0,
minify: { minify: {
collapseBooleanAttributes: true, collapseBooleanAttributes: true,
collapseWhitespace: true, collapseWhitespace: true,
@ -89,28 +90,13 @@ export default function () {
this.routes.push(route) this.routes.push(route)
} }
}) })
var n = 0
function timer () {
let promise = new Promise(
(resolve, reject) => {
setTimeout( () => {
resolve()
}, self.options.generate.wait * n)
}
)
n++
return promise
}
let routes = this.routes let routes = this.routes
return co(function * () { return co(function * () {
while (routes.length) { while (routes.length) {
let n = 0
yield routes.splice(0, 500).map((route) => { yield routes.splice(0, 500).map((route) => {
return co(function * () { return co(function * () {
if(self.options.generate.wait) { yield waitFor(n++ * self.options.generate.interval)
yield timer()
}
var { html } = yield self.renderRoute(route, { _generate: true }) var { html } = yield self.renderRoute(route, { _generate: true })
html = minify(html, self.options.generate.minify) html = minify(html, self.options.generate.minify)
var path = join(route, sep, 'index.html') // /about -> /about/index.html var path = join(route, sep, 'index.html') // /about -> /about/index.html

View File

@ -5,6 +5,6 @@ module.exports = {
'/users/2', '/users/2',
'/users/3' '/users/3'
], ],
wait : 1000 interval: 200
} }
} }