Skip to content

Comments

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

Draft
sylingd wants to merge 26 commits intoweb-infra-dev:mainfrom
sylingd:feat/server-bundle
Draft

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

Conversation

@sylingd
Copy link
Contributor

@sylingd sylingd commented Jan 31, 2026

为方便 Review,拆分出更小的 PR

Summary

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

该功能拆分自 deploy edge env 的 PR,后续 edge deploy 会基于该功能实现。将该功能单独拆分,主要出于两个目的:

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

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

目前该功能并不完善,例如目前生成的单一 js 文件 server 还不支持静态文件,加上该功能主要是为了后续功能做铺垫,所以暂时不对用户透出,仅用于 integration test。如有需要可以后续再进行完善


主要改动:

  • solutions/app-toolsdeploy/platforms/node 中增加选项判断,调用 bundleServer 进行二次打包
  • 修改相关 plugins,将 SERVER_BUNDLE 打包标记为服务端打包。
  • 引入 MODERN_SERVER_BUNDLE 环境变量,标志是否会打包为单一文件。部分原先基于文件系统的行为将被替换:
    • server/bff-core 基于 dependencies 产生 API 列表。
    • server/core 从 dependencies 读取 manifest,从 route 读取 render、loader
  • 在 esm 项目中运行 deploy 命令,将生成 esm 格式的产物
    • solutions/app-tools 增加 esm entry,修改相关 generator 逻辑
    • server/bff-core 改为仅使用 compatibleRequire,以前的为了兼容 jest 的代码疑似已经不再有用?
  • 调整 utils 包
    • 通过全局代码搜索,确认部分第三方包未被使用,已移除:browserslist signale
    • 不再直接导出 ora 包,其并未被直接使用,而是被其他第三方包依赖
    • 重新打包 fs-extra,增加 esm 产物,并移除依赖的graceful-fs
      • graceful-fs 主要用于“优化”原生fs的一些边界情况,但其不兼容以 esm import 方式导入的 fs,会在 bundle server 中报错。ref
    • 增加便于 tree shaking 的 /*#__PURE__*/ 标记
    • 修改 prebundle 后的 lodash 包名称,避免本地测试时 ndepe 复制错误的依赖。
  • 测试完善
    • deploy-server integration tests 增加对产物的验证
    • 增加 pure esm 项目的 deploy 验证
    • 移除少量 isVersionAtLeast1819 判断

测试运行结果 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

changeset-bot bot commented Jan 31, 2026

🦋 Changeset detected

Latest commit: 791267a

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

This PR includes changesets to release 110 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
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-streaming-inline-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 Patch
tmp-dir Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark 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/flight-server-transform-plugin 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

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
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