Makes generate asynchronous

This commit is contained in:
Sébastien Chopin 2017-05-14 20:21:14 +02:00
parent 93e3fe800d
commit 6a6b978cd1
3 changed files with 28 additions and 27 deletions

View File

@ -86,8 +86,10 @@ export default async function () {
this.routes.push(route) this.routes.push(route)
} }
}) })
let routes = this.routes
while (routes.length) {
let n = 0 let n = 0
for (let route of this.routes) { await Promise.all(routes.splice(0, 500).map(async (route) => {
await waitFor(n++ * self.options.generate.interval) await waitFor(n++ * self.options.generate.interval)
try { try {
var { html, error } = await self.renderRoute(route, { _generate: true }) var { html, error } = await self.renderRoute(route, { _generate: true })
@ -96,14 +98,12 @@ export default async function () {
} }
} catch (err) { } catch (err) {
errors.push({ type: 'unhandled', route, error: err }) errors.push({ type: 'unhandled', route, error: err })
continue
} }
try { try {
var minifiedHtml = minify(html, self.options.generate.minify) var minifiedHtml = minify(html, self.options.generate.minify)
} catch (err) { } catch (err) {
let minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`) let minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`)
errors.push({ type: 'unhandled', route, error: minifyErr }) errors.push({ type: 'unhandled', route, error: minifyErr })
continue
} }
var path = join(route, sep, 'index.html') // /about -> /about/index.html var path = join(route, sep, 'index.html') // /about -> /about/index.html
debug('Generate file: ' + path) debug('Generate file: ' + path)
@ -111,6 +111,7 @@ export default async function () {
// Make sure the sub folders are created // Make sure the sub folders are created
await mkdirp(dirname(path)) await mkdirp(dirname(path))
await writeFile(path, minifiedHtml, 'utf8') await writeFile(path, minifiedHtml, 'utf8')
}))
} }
// Add .nojekyll file to let Github Pages add the _nuxt/ folder // Add .nojekyll file to let Github Pages add the _nuxt/ folder
// https://help.github.com/articles/files-that-start-with-an-underscore-are-missing/ // https://help.github.com/articles/files-that-start-with-an-underscore-are-missing/

View File

@ -21,7 +21,7 @@ export function setAnsiColors (ansiHTML) {
}) })
} }
export function * waitFor (ms) { export async function waitFor (ms) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
setTimeout(resolve, (ms || 0)) setTimeout(resolve, (ms || 0))
}) })

View File

@ -28,9 +28,9 @@ test('setAnsiColors', t => {
t.pass() t.pass()
}) })
test('waitFor', function * (t) { test('waitFor', async (t) => {
let s = Date.now() let s = Date.now()
yield utils.waitFor(100) await utils.waitFor(100)
t.true(Date.now() - s >= 100) t.true(Date.now() - s >= 100)
}) })