Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add build concurrency control (fix #1819) #2953

Merged
merged 3 commits into from Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/@vuepress/core/lib/node/build/index.js
Expand Up @@ -20,6 +20,7 @@ module.exports = class Build extends EventEmitter {
constructor (context) {
super()
this.context = context
this.maxConcurrency = this.context.options.maxConcurrency
this.outDir = this.context.outDir
}

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

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

readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
Expand Down
4 changes: 4 additions & 0 deletions packages/docs/docs/api/cli.md
Expand Up @@ -54,6 +54,10 @@ Start development server in debug mode.

Start development server in silent mode.

#### --max-concurrency

Set the max concurrency for rendering pages, to prevent OOM when rendering massive docs.

### dev

Start a development server. All options from `vuepress build` are available. And there are several options specifically for dev:
Expand Down
3 changes: 3 additions & 0 deletions packages/docs/docs/zh/api/cli.md
Expand Up @@ -29,6 +29,9 @@ vuepress <command> targetDir [options]
### --silent
以安静模式启动开发服务器。

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

## dev

启动一个开发服务器。来自 `vuepress build` 的所有选项都可用。除此以外,还有几个专门针对 dev 的选项:
Expand Down
1 change: 1 addition & 0 deletions packages/vuepress/lib/registerCoreCommands.js
Expand Up @@ -51,6 +51,7 @@ module.exports = function (cli, options) {
.option('--no-cache', 'clean the cache before build')
.option('--debug', 'build in development mode for debugging')
.option('--silent', 'build static site in silent mode')
.option('--max-concurrency', 'set the max docs concurrently processed when build static site')
.action((sourceDir = '.', commandOptions) => {
const { debug, silent } = commandOptions

Expand Down