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,31 +86,32 @@ export default async function () {
this.routes.push(route)
}
})
let n = 0
for (let route of this.routes) {
await waitFor(n++ * self.options.generate.interval)
try {
var { html, error } = await self.renderRoute(route, { _generate: true })
if (error) {
errors.push({ type: 'handled', route, error })
let routes = this.routes
while (routes.length) {
let n = 0
await Promise.all(routes.splice(0, 500).map(async (route) => {
await waitFor(n++ * self.options.generate.interval)
try {
var { html, error } = await self.renderRoute(route, { _generate: true })
if (error) {
errors.push({ type: 'handled', route, error })
}
} catch (err) {
errors.push({ type: 'unhandled', route, error: err })
}
} catch (err) {
errors.push({ type: 'unhandled', route, error: err })
continue
}
try {
var minifiedHtml = minify(html, self.options.generate.minify)
} catch (err) {
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 })
continue
}
var path = join(route, sep, 'index.html') // /about -> /about/index.html
debug('Generate file: ' + path)
path = join(distPath, path)
// Make sure the sub folders are created
await mkdirp(dirname(path))
await writeFile(path, minifiedHtml, 'utf8')
try {
var minifiedHtml = minify(html, self.options.generate.minify)
} catch (err) {
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 })
}
var path = join(route, sep, 'index.html') // /about -> /about/index.html
debug('Generate file: ' + path)
path = join(distPath, path)
// Make sure the sub folders are created
await mkdirp(dirname(path))
await writeFile(path, minifiedHtml, 'utf8')
}))
}
// Add .nojekyll file to let Github Pages add the _nuxt/ folder
// 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) {
setTimeout(resolve, (ms || 0))
})

View File

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