Optimise generate out of memory when +10000 paths

This commit is contained in:
Sébastien Chopin 2016-12-08 18:07:14 +01:00
parent 8db6964dc5
commit ac2250c281

View File

@ -19,6 +19,7 @@ const defaults = {
}
module.exports = function () {
const s = Date.now()
/*
** Update loaders config to add router.base path
*/
@ -66,9 +67,8 @@ module.exports = function () {
/*
** Generate html files from routes
*/
var promises = []
let routes = []
this.routes.forEach((route) => {
let subRoutes = []
if (route.path.includes(':') || route.path.includes('*')) {
const routeParams = this.options.generate.routeParams[route.path]
if (!routeParams) {
@ -76,29 +76,28 @@ module.exports = function () {
return process.exit(1)
}
const toPath = pathToRegexp.compile(route.path)
routeParams.forEach((params) => {
const newRoute = Object.assign({}, route, { path: toPath(params) })
subRoutes.push(newRoute)
})
routes = routes.concat(routeParams.map((params) => {
return Object.assign({}, route, { path: toPath(params) })
}))
} else {
subRoutes.push(route)
routes.push(route)
}
subRoutes.forEach((route) => {
var promise = this.renderRoute(route.path)
.then(({ html }) => {
var path = join(route.path, sep, 'index.html') // /about -> /about/index.html
debug('Generate file: ' + path)
path = join(distPath, path)
// Make sure the sub folders are created
})
return co(function * () {
while (routes.length) {
yield routes.splice(0, 500).map((route) => {
return co(function * () {
const { html } = yield self.renderRoute(route.path)
var path = join(route.path, sep, 'index.html') // /about -> /about/index.html
debug('Generate file: ' + path)
path = join(distPath, path)
// Make sure the sub folders are created
yield mkdirp(dirname(path))
yield writeFile(path, html, 'utf8')
})
})
promises.push(promise)
})
}
})
return Promise.all(promises)
})
.then((pages) => {
// Add .nojekyll file to let Github Pages add the _nuxt/ folder
@ -107,7 +106,9 @@ module.exports = function () {
return writeFile(nojekyllPath, '')
})
.then(() => {
debug('HTML Files generated')
const duration = Math.round((Date.now() - s) / 100) / 10
debug(`HTML Files generated in ${duration}s`)
return this
})
}