mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
rewrote generate.js to async/await instead of coroutines
This commit is contained in:
parent
892543eb1d
commit
40a167258b
@ -1,7 +1,6 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
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'
|
||||||
@ -39,7 +38,7 @@ const defaults = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function () {
|
export default async function () {
|
||||||
const s = Date.now()
|
const s = Date.now()
|
||||||
let errors = []
|
let errors = []
|
||||||
/*
|
/*
|
||||||
@ -51,38 +50,34 @@ export default function () {
|
|||||||
var srcBuiltPath = resolve(this.dir, '.nuxt', 'dist')
|
var srcBuiltPath = resolve(this.dir, '.nuxt', 'dist')
|
||||||
var distPath = resolve(this.dir, this.options.generate.dir)
|
var distPath = resolve(this.dir, this.options.generate.dir)
|
||||||
var distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
|
var distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
|
||||||
return co(function * () {
|
|
||||||
/*
|
/*
|
||||||
** Launch build process
|
** Launch build process
|
||||||
*/
|
*/
|
||||||
yield self.build()
|
await self.build()
|
||||||
/*
|
/*
|
||||||
** Clean destination folder
|
** Clean destination folder
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
yield remove(distPath)
|
await remove(distPath)
|
||||||
debug('Destination folder cleaned')
|
debug('Destination folder cleaned')
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
/*
|
/*
|
||||||
** Copy static and built files
|
** Copy static and built files
|
||||||
*/
|
*/
|
||||||
if (fs.existsSync(srcStaticPath)) {
|
if (fs.existsSync(srcStaticPath)) {
|
||||||
yield copy(srcStaticPath, distPath)
|
await copy(srcStaticPath, distPath)
|
||||||
}
|
}
|
||||||
yield copy(srcBuiltPath, distNuxtPath)
|
await copy(srcBuiltPath, distNuxtPath)
|
||||||
debug('Static & build files copied')
|
debug('Static & build files copied')
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
// Resolve config.generate.routes promises before generating the routes
|
// Resolve config.generate.routes promises before generating the routes
|
||||||
return promisifyRoute(this.options.generate.routes || [])
|
try {
|
||||||
.catch((e) => {
|
var generateRoutes = await promisifyRoute(this.options.generate.routes || [])
|
||||||
|
} catch(e) {
|
||||||
console.error('Could not resolve routes') // eslint-disable-line no-console
|
console.error('Could not resolve routes') // eslint-disable-line no-console
|
||||||
console.error(e) // eslint-disable-line no-console
|
console.error(e) // eslint-disable-line no-console
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
throw e // eslint-disable-line no-unreachable
|
throw e // eslint-disable-line no-unreachable
|
||||||
})
|
}
|
||||||
})
|
|
||||||
.then((generateRoutes) => {
|
|
||||||
/*
|
/*
|
||||||
** Generate html files from routes
|
** Generate html files from routes
|
||||||
*/
|
*/
|
||||||
@ -91,47 +86,38 @@ export default function () {
|
|||||||
this.routes.push(route)
|
this.routes.push(route)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
let routes = this.routes
|
|
||||||
return co(function * () {
|
|
||||||
while (routes.length) {
|
|
||||||
let n = 0
|
let n = 0
|
||||||
yield routes.splice(0, 500).map((route) => {
|
for(let route of this.routes) {
|
||||||
return co(function * () {
|
|
||||||
yield waitFor(n++ * self.options.generate.interval)
|
await waitFor(n++ * self.options.generate.interval)
|
||||||
try {
|
try {
|
||||||
var { html, error } = yield self.renderRoute(route, { _generate: true })
|
var { html, error } = await self.renderRoute(route, { _generate: true })
|
||||||
if (error) {
|
if (error) {
|
||||||
errors.push({ type: 'handled', route, error })
|
errors.push({ type: 'handled', route, error })
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
errors.push({ type: 'unhandled', route, error: err })
|
errors.push({ type: 'unhandled', route, error: err })
|
||||||
return
|
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 })
|
||||||
return
|
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)
|
||||||
path = join(distPath, path)
|
path = join(distPath, path)
|
||||||
// Make sure the sub folders are created
|
// Make sure the sub folders are created
|
||||||
yield mkdirp(dirname(path))
|
await mkdirp(dirname(path))
|
||||||
yield writeFile(path, minifiedHtml, 'utf8')
|
await writeFile(path, minifiedHtml, 'utf8')
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
})
|
|
||||||
.then((pages) => {
|
|
||||||
// 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/
|
||||||
const nojekyllPath = resolve(distPath, '.nojekyll')
|
const nojekyllPath = resolve(distPath, '.nojekyll')
|
||||||
return writeFile(nojekyllPath, '')
|
writeFile(nojekyllPath, '')
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
const duration = Math.round((Date.now() - s) / 100) / 10
|
const duration = Math.round((Date.now() - s) / 100) / 10
|
||||||
debug(`HTML Files generated in ${duration}s`)
|
debug(`HTML Files generated in ${duration}s`)
|
||||||
|
|
||||||
@ -145,6 +131,4 @@ export default function () {
|
|||||||
})
|
})
|
||||||
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
||||||
}
|
}
|
||||||
return this
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user