Skip to content

Commit 2f9a394

Browse files
troyeaglegenefy
and
genefy
authoredJan 15, 2022
feat: add build concurrency control (close: #1819) (#2953)
* feat: add build concurrency control * fix: rm comment * feat: docs Co-authored-by: genefy <ziheng.dzh@alibaba-inc.com>
1 parent 7cdb9a0 commit 2f9a394

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed
 

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = class Build extends EventEmitter {
2020
constructor (context) {
2121
super()
2222
this.context = context
23+
this.maxConcurrency = this.context.options.maxConcurrency
2324
this.outDir = this.context.outDir
2425
}
2526

@@ -91,9 +92,18 @@ module.exports = class Build extends EventEmitter {
9192
// render pages
9293
logger.wait('Rendering static HTML...')
9394

94-
const pagePaths = await Promise.all(
95-
this.context.pages.map(page => this.renderPage(page))
96-
)
95+
// If maxConcurrency is set, instead of rendering all pages concurrently,
96+
// build task would render pages by smaller group to prevent OOM.
97+
if (this.maxConcurrency) logger.info(`max concurrency set: ${this.maxConcurrency}`)
98+
const pagePaths = []
99+
const maxConcurrency = this.maxConcurrency || 100000
100+
for (let i = 0; i < this.context.pages.length; i += maxConcurrency) {
101+
const segmentPaths = await Promise.all(
102+
this.context.pages.slice(i, i + maxConcurrency)
103+
.map(page => this.renderPage(page))
104+
)
105+
pagePaths.push(...segmentPaths)
106+
}
97107

98108
readline.clearLine(process.stdout, 0)
99109
readline.cursorTo(process.stdout, 0)

‎packages/docs/docs/api/cli.md

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Start development server in debug mode.
5454

5555
Start development server in silent mode.
5656

57+
#### --max-concurrency
58+
59+
Set the max concurrency for rendering pages, to prevent OOM when rendering massive docs.
60+
5761
### dev
5862

5963
Start a development server. All options from `vuepress build` are available. And there are several options specifically for dev:

‎packages/docs/docs/zh/api/cli.md

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ vuepress <command> targetDir [options]
2929
### --silent
3030
以安静模式启动开发服务器。
3131

32+
### --max-concurrency
33+
设置渲染文档的最大并发量,当渲染大量文档,可能造成内存溢出时使用
34+
3235
## dev
3336

3437
启动一个开发服务器。来自 `vuepress build` 的所有选项都可用。除此以外,还有几个专门针对 dev 的选项:

‎packages/vuepress/lib/registerCoreCommands.js

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = function (cli, options) {
5151
.option('--no-cache', 'clean the cache before build')
5252
.option('--debug', 'build in development mode for debugging')
5353
.option('--silent', 'build static site in silent mode')
54+
.option('--max-concurrency', 'set the max docs concurrently processed when build static site')
5455
.action((sourceDir = '.', commandOptions) => {
5556
const { debug, silent } = commandOptions
5657

0 commit comments

Comments
 (0)
Please sign in to comment.