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) this.routes.push(route)
} }
}) })
let n = 0 let routes = this.routes
for (let route of this.routes) { while (routes.length) {
await waitFor(n++ * self.options.generate.interval) let n = 0
try { await Promise.all(routes.splice(0, 500).map(async (route) => {
var { html, error } = await self.renderRoute(route, { _generate: true }) await waitFor(n++ * self.options.generate.interval)
if (error) { try {
errors.push({ type: 'handled', route, error }) 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) { try {
errors.push({ type: 'unhandled', route, error: err }) var minifiedHtml = minify(html, self.options.generate.minify)
continue } catch (err) {
} let minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`)
try { errors.push({ type: 'unhandled', route, error: minifyErr })
var minifiedHtml = minify(html, self.options.generate.minify) }
} catch (err) { var path = join(route, sep, 'index.html') // /about -> /about/index.html
let minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`) debug('Generate file: ' + path)
errors.push({ type: 'unhandled', route, error: minifyErr }) path = join(distPath, path)
continue // Make sure the sub folders are created
} await mkdirp(dirname(path))
var path = join(route, sep, 'index.html') // /about -> /about/index.html await writeFile(path, minifiedHtml, 'utf8')
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 // 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)
}) })