From ac2250c2811aae44979ab71eb753fc2ca916e51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Thu, 8 Dec 2016 18:07:14 +0100 Subject: [PATCH] Optimise generate out of memory when +10000 paths --- lib/generate.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/generate.js b/lib/generate.js index 7d00286663..8b38b4ebfd 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -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 }) }