Skip to content

feat(app-tools): compile the server environment in a worker with performance.parallelBuild - #9288

Open
keepview wants to merge 12 commits into
mainfrom
feat/plugin-parallel-build
Open

keepview wants to merge 12 commits into
mainfrom
feat/plugin-parallel-build

Conversation

@keepview

@keepview keepview commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

An application whose build has a server environment (SSR or SSG enabled) compiles its client and server environments with two Rspack compilers in one Node.js process. They share one main thread and one V8 heap, so JavaScript loaders, plugin hooks and garbage collection of one compiler stall the other.

This PR adds the experimental option performance.parallelBuild to @modern-js/app-tools. When it is on, a production build keeps planning (entries, routes, generated code) and every after-build step (server/ and api/ compilation, SSG, deploy) in the main process, where they run once. Only the Rspack compile of the server environment moves to a worker process: the main builder is created without that environment, and the worker creates a builder for it from the planning results sent by the main process. Both processes write to the final dist, so the output is identical to a single-process build. The worker initialises the CLI because functions in the build configuration cannot be sent to another process; planning hooks and build/deploy hooks do not run there. The plain-data parts of the configuration that both builds share are compared between the two processes and the build fails if they differ.

The option is off by default and has no sub-options. Without it the build takes the same code path as before. dev, --watch, --analyze, Rsdoctor and RSC applications keep the single process.

An earlier revision of this PR implemented the idea as a separate plugin that re-ran the CLI in a child process; that package is removed.

It also fixes a concurrency bug in @modern-js/server-utils: the server-directory compile wrote its generated tsconfig to one fixed path, so two compiles of the same app running at the same time could read each other's outDir.

构建中包含 server 环境的应用(开启 SSR 或 SSG),其 client 与 server 两个环境由同一进程里的两个 Rspack 编译器编译,共用主线程和 V8 堆。本 PR 为 @modern-js/app-tools 新增实验性配置 performance.parallelBuild:开启后规划与构建后的步骤仍只在主进程执行一次,只有 server 环境的 Rspack 编译交给 worker 进程,两个进程直接写入最终 dist,产物与单进程一致。默认关闭、无子选项,不开启时代码路径不变;dev、watch、analyze、Rsdoctor 与 RSC 保持单进程。同时修复 @modern-js/server-utils 临时 tsconfig 固定路径导致的并发问题。

Related Links

Validation Evidence

  • New integration test tests/integration/ssr/tests/parallel-build.test.ts: builds an existing app that has a server environment (the SSR base app) with and without the option and asserts the emitted files are byte-identical, the worker was used, and the server directory was compiled once.
  • Temporary check: with the option forced on for every app, the whole integration suite passed (482 passed, 0 failed, 32 skipped).
  • pnpm --filter @modern-js/app-tools test: 29 passed. pnpm lint, pnpm check, pnpm check-changeset, type check of app-tools: pass.
  • Benchmark on generated apps with a server environment, measured on the earlier revision: with a JavaScript loader on every module, 10k modules per compiler 62.0 s to 45.2 s and 20k modules 120.1 s to 86.2 s; default toolchain 20k modules 62.5 s to 56.6 s; process-tree peak memory lower in all of these. A rerun on this revision is in progress and the docs table will be updated.

Retained Tests and Exceptions

  • Retained: the integration test above, added to the existing ssr base app (no new fixture). It protects the public contract of the option: identical output and single execution of after-build steps.
  • No widened exports, no governance exceptions.

Checklist

  • I have added a changeset via pnpm run change if published package behavior changes.
  • I have updated the documentation where needed.
  • I have recorded validation evidence and the rationale for retained tests or exceptions.

Co-Authored-By: Riff

…two processes

Add @modern-js/plugin-parallel-build. When enabled, the main process builds
the client environment while a child process builds the server environment
into a staging directory; the server bundles are moved into dist once both
finish. The build stays single-process for dev, watch, analyze, Rsdoctor,
SSG, RSC, extra environments and, in auto mode, low memory.

The server compile wrote its generated tsconfig to one fixed path, so two
compiles of the same app running at the same time could read each other's
outDir and emit into the wrong directory. The file name now carries a hash
of the output directory.

Co-Authored-By: Riff
@changeset-bot

changeset-bot Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 52e92f7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 116 packages
Name Type
@modern-js/app-tools Minor
@modern-js/server-utils Minor
@modern-js/adapter-rstest Minor
@modern-js/plugin-bff Minor
@modern-js/plugin-ssg Minor
@modern-js/plugin-styled-components Minor
@modern-js/storybook-addon Minor
@modern-js/plugin-i18n Minor
@modern-js/image Minor
@modern-js/runtime Minor
@modern-js/plugin-polyfill Minor
@modern-js/mcp-apps Minor
app-document Patch
async-entry-test Patch
tmp Patch
bff-api-app Patch
bff-client-app Patch
bff-indep-client-app Patch
bff-hono Patch
integration-clean-dist-path Patch
integration-custom-dist-path Patch
custom-file-system-entry Patch
integration-custom-template Patch
deploy Patch
dev-server Patch
integration-disable-html Patch
entries-app-builder Patch
app-custom-entries Patch
app-custom-routes-runtime Patch
app-custom Patch
app-entry Patch
app-route Patch
app-entry-server Patch
i18n-app-csr-html-lang Patch
i18n-app Patch
i18n-app-ssr-html-lang Patch
i18n-app-ssr Patch
i18n-custom-i18n-wrapper Patch
i18n-mf-app-provider Patch
i18n-mf-component-provider Patch
i18n-mf-consumer Patch
i18n-routes Patch
i18n-routes-ssr Patch
@integration-test/image-component Patch
main-entry-name Patch
mcp-apps-integration Patch
nonce Patch
pure-esm-project Patch
react-compiler-test Patch
routes-match Patch
routes Patch
app-rsbuild-hooks Patch
rsc-csr-app Patch
rsc-csr-routes Patch
rsc-ssr-app Patch
rsc-ssr-routes Patch
basic-app-rstest-browser Patch
basic-app-rstest Patch
runtime-custom-plugin Patch
runtime-custom-config-plugin Patch
select-mul-entry-test Patch
select-one-entry-test Patch
server-esm-tsx Patch
server Patch
@source-code-build/app Patch
ssg-fixtures-mega-list-routes Patch
ssg-fixtures-nested-routes Patch
ssg-fixtures-simple Patch
ssg-fixtures-web-server Patch
ssr-base-async-entry-test Patch
ssr-base-async-pre-entry-test Patch
ssr-base-json-test Patch
ssr-base-test Patch
ssr-base-fallback-test Patch
init Patch
ssr-base-loadable Patch
ssr-partial-test Patch
rsc-closing-tags-test Patch
ssr-script-loading Patch
ssr-useid-test Patch
ssr-streaming-inline-test Patch
ssr-streaming-lazy-test Patch
ssr-streaming-test Patch
styled-components-stream Patch
styled-components-string Patch
integration-tailwindcss-v2 Patch
integration-tailwindcss-v3 Patch
integration-tailwindcss-v4-tools Patch
integration-tailwindcss-v4 Patch
tmp-dir Patch
tools-plugin-options-test Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark Minor
@modern-js/server Minor
@modern-js/main-doc Minor
@modern-js/tsconfig Minor
@modern-js/builder Minor
@modern-js/plugin-data-loader Minor
@modern-js/render Minor
@modern-js/bff-core Minor
@modern-js/bff-runtime Minor
@modern-js/server-core Minor
@modern-js/create-request Minor
@modern-js/prod-server Minor
@modern-js/server-runtime Minor
@modern-js/create Minor
@modern-js/i18n-utils Minor
@modern-js/plugin Minor
@modern-js/runtime-utils Minor
@modern-js/sandpack-react Minor
@modern-js/types Minor
@modern-js/utils Minor
@modern-js/rslib Minor
@scripts/prebundle Patch
@scripts/rstest-config Patch
@scripts/release-node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify

netlify Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for modernjs-byted ready!

Name Link
🔨 Latest commit 52e92f7
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-byted/deploys/6b0fba029b6b908a83c729a1
😎 Deploy Preview https://deploy-preview-9288--modernjs-byted.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 91 (no change from production)
PWA: -
View the detailed breakdown and full score reports
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Rsdoctor Bundle Diff Analysis

📊 Quick Summary
Project Total Size Change
bundle-diff 514.3 KB 0

Generated by Rsdoctor GitHub Action

…mat docs

The thread-split assertion assumed at least four cores and the temp dir
assertion assumed POSIX separators; both now hold on the macOS and Windows
runners. The new documentation files are formatted with the repository
formatter.

Co-Authored-By: Riff
…, follow SSG per-entry rule

Running every integration fixture in two-process mode surfaced two problems.

First, consumers of the server bundles depended on plugin order: with the
plugin registered after plugin-ssg, SSG ran before the merge and failed with
a missing dist/bundles/index.js. The merge now runs in a `pre` ordered
Rsbuild after-build hook, ahead of the framework's own after-build bridge,
so deploy and custom plugins see the complete dist whatever their position.

Second, `output.ssgByEntries` was not recognised, so SSG apps configured per
entry were split. SSG detection now follows the framework's rule (per-entry
settings first, then `output.ssg`). SSG keeps falling back to one process:
the server build process runs the whole CLI lifecycle, so it would prerender
a second time without the client output.

The staging directory is removed when the server build fails.

Co-Authored-By: Riff
…ormance.parallelBuild

Replace the separate plugin, which re-ran the whole CLI in a child process
and moved the server bundles back, with a capability built into the build
path. Planning (entries, routes, generated code) and the after-build steps
still run once in the main process. With `performance.parallelBuild` the
builder of the main process is created without the server environment, and
a worker process, started when the build begins, creates a builder for that
environment only from the planning results it receives. Both write to the
final dist, so there is no staging directory or merge, and the output is
identical to a single-process build.

The worker initialises the CLI because functions in the build configuration
cannot cross a process boundary; planning hooks and build/deploy hooks do
not run there. The plain-data parts of the configuration shared by both
builds are compared with the main process and the build fails if they
differ. The option is off by default; without it the build takes the same
path as before. RSC, watch, analyze and Rsdoctor keep the single process.

Co-Authored-By: Riff
@keepview keepview changed the title feat(plugin-parallel-build): build client and server environments in two processes feat(app-tools): compile the server environment in a worker with performance.parallelBuild Dec 1, 2026
…lBuild moves to the worker

Co-Authored-By: Riff
…split

Add integration cases for a server compile that fails in the worker and
for a configuration that evaluates differently in the two processes.

Remove the Rayon thread split between the processes: at 10k modules per
compiler it made no measurable difference against fixed pools of 24 or 64
threads, with or without a JavaScript loader.

Document that the stats passed to onAfterBuild cover the client
environment only.

Co-Authored-By: Riff
…orker

The BFF loader imports the API modules while the server environment is
compiled. The worker did not register the swc hooks and tsconfig paths,
so an API module importing through a path alias failed to load.

Co-Authored-By: Riff

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant