Skip to content

Commit 06ef5ec

Browse files
committed
perf(): limit number of static html pages rendered to 50 active
1 parent 0379421 commit 06ef5ec

File tree

1 file changed

+14
-17
lines changed
  • packages/@vuepress/core/lib/node/build

1 file changed

+14
-17
lines changed

packages/@vuepress/core/lib/node/build/index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const createServerConfig = require('../webpack/createServerConfig')
1111
const { createBundleRenderer } = require('vue-server-renderer')
1212
const { normalizeHeadTag, applyUserWebpackConfig } = require('../util/index')
1313
const { version } = require('../../../package')
14+
const pLimit = require('p-limit')
1415

1516
/**
1617
* Expose Build Process Class.
@@ -91,8 +92,10 @@ module.exports = class Build extends EventEmitter {
9192
// render pages
9293
logger.wait('Rendering static HTML...')
9394

95+
// Use p-limit to throttle number of files done at the same time
96+
const limit = pLimit(RENDER_LIMIT)
9497
const pagePaths = await Promise.all(
95-
this.context.pages.map(page => this.renderPage(page))
98+
this.context.pages.map(page => limit(() => this.renderPage(page)))
9699
)
97100

98101
readline.clearLine(process.stdout, 0)
@@ -145,32 +148,26 @@ module.exports = class Build extends EventEmitter {
145148
version
146149
}
147150

151+
const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '')
152+
const filePath = path.resolve(this.outDir, filename)
148153
try {
149-
const readable = await this.renderer.renderToStream(context)
150-
const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '')
151-
const filePath = path.resolve(this.outDir, filename)
154+
const readable = this.renderer.renderToStream(context)
152155
await fs.ensureDir(path.dirname(filePath))
153-
return pipe(filePath, readable)
156+
await pipe(filePath, readable)
154157
} catch (e) {
155158
console.error(logger.error(chalk.red(`Error rendering ${pagePath}:`), false))
156159
throw e
157160
}
161+
return filePath
158162
}
159163
}
160164

161-
/**
162-
* Pipes rendered static HTML to a file
163-
*
164-
* @param {string} filePath
165-
* @param {Stream.Readable} readable
166-
* @returns {Promise<void>}
167-
*/
168-
function pipe(filePath, readable) {
165+
function pipe(filePath, stream) {
169166
return new Promise((resolve, reject) => {
170-
const outStream = fs.createWriteStream(filePath);
171-
readable.pipe(outStream);
172-
outStream.on('finish', resolve(filePath));
173-
readable.on('error', reject);
167+
const outStream = fs.createWriteStream(filePath)
168+
stream.pipe(outStream)
169+
outStream.on('finish', resolve)
170+
stream.on('error', reject)
174171
});
175172
}
176173

0 commit comments

Comments
 (0)