Skip to content

feat: bundle server-side codes into a single file#8286

Open
sylingd wants to merge 29 commits intoweb-infra-dev:mainfrom
sylingd:feat/server-bundle
Open

feat: bundle server-side codes into a single file#8286
sylingd wants to merge 29 commits intoweb-infra-dev:mainfrom
sylingd:feat/server-bundle

Conversation

@sylingd
Copy link
Copy Markdown
Contributor

@sylingd sylingd commented Jan 31, 2026

Summary

添加 bundle server 功能,该功能可以将所有 server 端内容及依赖(含 BFF)打包为一个单一 js 文件。

该功能拆分自 deploy edge env 的 PR,后续 edge deploy 会基于该功能实现。

拆分出该功能的目的,并不是希望用户直接使用该功能,而是出于两个考虑:

  • 减少单个 PR 的功能和代码改动量,方便验证及 Code Review。
  • 在 integration test 中验证打包产物是否可以正常运行(替代难以自动化验证的第三方环境)

因此,该功能不完善,例如目前生成的单一 js 文件 server 还不支持静态文件,故不对用户透出,仅用于 integration test。

在项目中通过 server.ssr.bundleServer 开启该功能。运行完原先的 build 流程后,会执行二次 build。二次 build 会将页面、loader 打包至 route 上,其他依赖(如 api/lambda)打包至一固定 dependencies 对象上;该对象是一个“伪”文件树结构。


主要改动:

  • solutions/app-toolsdeploy/platforms/node 中增加选项判断,调用 bundleServer 进行二次打包
  • 修改相关 plugins,将 SERVER_BUNDLE 打包标记为服务端打包。
  • 引入 MODERN_SERVER_BUNDLE 环境变量,标志是否会打包为单一文件。部分原先基于文件系统的行为将被替换:
    • server/bff-core 基于 dependencies 产生 API 列表。
    • server/core 从 dependencies 读取 manifest,从 route 读取 render、loader
    • server/prod-server 抽取公共逻辑到 base-server 中
  • 调整 utils 包以进尽量减少 bundle server 的产物体积
    • 通过全局代码搜索,确认部分第三方包未被使用,已移除:signale
    • PR fix/v3-zx #8304 中对 browserlist 移除不彻底,本 PR 进一步移除遗留代码
    • 不再直接导出 ora 包,其并未被直接使用,而是被其他第三方包依赖
    • 重新打包 fs-extra,增加 esm 产物,并移除依赖的graceful-fs
      • graceful-fs 主要用于“优化”原生fs的一些边界情况,但其不兼容以 esm import 方式导入的 fs,会在 bundle server 中报错。ref
      • 移除后,相应的在runtime/plugin-runtime中增加了sort语句,以保持不同平台下行为一致,避免ut失败的情况(sort逻辑原先是在graceful-fs中做的)
    • 增加便于 tree shaking 的 /*#__PURE__*/ 标记
    • runtime/plugin-runtime中的一些常量移动到公共包,因 bundle server 功能需要使用几个常量,避免不必要的依赖过深,所以进行移动。
  • 测试完善
    • 增加相应验证

测试运行结果 integration-test-linux

Related Links

拆分自#8097

Checklist

  • I have added changeset via pnpm run change.
  • I have updated the documentation.
  • I have added tests to cover my changes.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jan 31, 2026

🦋 Changeset detected

Latest commit: d3d0d1e

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

This PR includes changesets to release 114 packages
Name Type
@modern-js/plugin-data-loader Minor
@modern-js/runtime Minor
@modern-js/app-tools Minor
@modern-js/prod-server Minor
@modern-js/bff-core Minor
@modern-js/plugin-bff Minor
@modern-js/plugin Minor
@modern-js/server Minor
@modern-js/types Minor
@modern-js/utils Minor
@modern-js/server-core Minor
@modern-js/plugin-styled-components Minor
@modern-js/plugin-i18n Minor
@integration-test/alias-set Patch
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-compatibility Patch
integration-custom-dist-path Patch
custom-file-system-entry Patch
integration-custom-template Patch
deploy Patch
deploy-server Patch
dev-server Patch
integration-disable-html 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
nonce Patch
pure-esm-project 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-config Patch
server-json-script Patch
server-monitors Patch
server-prod Patch
server-routes 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-json-test Patch
ssr-base-test Patch
ssr-base-fallback-test Patch
init Patch
ssr-base-loadable Patch
ssr-partial-test Patch
ssr-script-loading Patch
ssr-useid-test Patch
ssr-streaming-inline-test Patch
ssr-streaming-test Patch
integration-storybook Patch
styled-components-stream Patch
styled-components-string Patch
integration-tailwindcss-v2 Patch
integration-tailwindcss-v3 Patch
integration-tailwindcss-v4 Patch
tmp-dir Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark Minor
@modern-js/adapter-rstest Minor
@modern-js/plugin-ssg Minor
@modern-js/image Minor
@modern-js/plugin-polyfill Minor
entries-app-builder Patch
@modern-js/builder Minor
@modern-js/render Minor
@modern-js/create-request Minor
@modern-js/server-runtime Minor
@modern-js/runtime-utils Minor
@modern-js/server-utils Minor
@modern-js/i18n-utils Minor
@scripts/release-node Patch
@modern-js/create Minor
@modern-js/main-doc Minor
@modern-js/tsconfig Minor
@modern-js/bff-runtime Minor
@modern-js/sandpack-react Minor
@modern-js/rslib Minor
@scripts/prebundle Patch
@scripts/rstest-config 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
Copy link
Copy Markdown

netlify bot commented Jan 31, 2026

Deploy Preview for modernjs-v3 ready!

Name Link
🔨 Latest commit 791267a
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-v3/deploys/697f74a2d0e7a700089ec974
😎 Deploy Preview https://deploy-preview-8286--modernjs-v3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@sylingd sylingd marked this pull request as draft January 31, 2026 06:09
@sylingd sylingd marked this pull request as ready for review January 31, 2026 06:56
@sylingd sylingd changed the title [WIP] feat: server bundle feat: server bundle Jan 31, 2026
@sylingd sylingd changed the title feat: server bundle feat: bundle server-side codes into a single file Jan 31, 2026
@sylingd sylingd marked this pull request as draft January 31, 2026 13:44
@sylingd sylingd marked this pull request as ready for review January 31, 2026 14:08
@sylingd sylingd marked this pull request as draft January 31, 2026 14:20
@sylingd sylingd marked this pull request as ready for review January 31, 2026 16:04
@sylingd sylingd marked this pull request as draft February 3, 2026 16:01
@sylingd sylingd changed the title feat: bundle server-side codes into a single file WIP feat: bundle server-side codes into a single file Feb 25, 2026
@netlify
Copy link
Copy Markdown

netlify bot commented Mar 17, 2026

Deploy Preview for modernjs-byted ready!

Name Link
🔨 Latest commit d3d0d1e
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-byted/deploys/69b93663e27297000912de21
😎 Deploy Preview https://deploy-preview-8286--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: 95 (🔴 down 3 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

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

@sylingd sylingd changed the title WIP feat: bundle server-side codes into a single file feat: bundle server-side codes into a single file Mar 17, 2026
@sylingd sylingd marked this pull request as ready for review March 17, 2026 11:32
@sylingd
Copy link
Copy Markdown
Contributor Author

sylingd commented Mar 17, 2026

@zllkjc Hello,这个PR已经ready,有空的时候review一下哦~

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