Skip to content

feat: openclaw gateway 启动速度优化 (esbuild bundle + compile cache + 插件预编译)#395

Merged
btc69m979y-dotcom merged 8 commits intofeature/openclawfrom
feat/openclaw-gateway-esbuild-bundle
Mar 12, 2026
Merged

feat: openclaw gateway 启动速度优化 (esbuild bundle + compile cache + 插件预编译)#395
btc69m979y-dotcom merged 8 commits intofeature/openclawfrom
feat/openclaw-gateway-esbuild-bundle

Conversation

@btc69m979y-dotcom
Copy link
Copy Markdown
Collaborator

Summary

优化 openclaw gateway 在 Windows Electron utilityProcess 中的启动速度,从 180s+ 超时 降至 ~85s(首次)/ ~15s(后续)

核心改动:

  • esbuild 单文件 bundle:将 gateway 入口打包为 gateway-bundle.mjs,消除 ESM 模块图解析开销(1100+ 文件 → 单文件,启动从 ~100s 降至 ~4s)
  • 专用 gateway-entry patch:跳过 Commander CLI 基础设施,直接调用 runGatewayCommand
  • V8 compile cache warmup:NSIS 安装时预热编译缓存,首次 bundle import 从 ~95s 降至 ~4s
  • 扩展插件预编译:esbuild 编译 40 个 TypeScript 扩展为 JS,减少 jiti 运行时 Babel 开销(135s → ~82s)
  • 最小化 openclaw 配置:全新安装不启用 IM 插件,避免 jiti 同步加载阻塞 gateway 端口绑定
  • NSIS 安装器优化:安装计时日志 + 隐藏空白详情框

改动文件

文件 说明
scripts/bundle-openclaw-gateway.cjs esbuild 打包 gateway 入口
scripts/precompile-openclaw-extensions.cjs 预编译扩展插件 TS → JS
scripts/warmup-compile-cache.cjs V8 编译缓存预热脚本
scripts/apply-openclaw-patches.cjs openclaw patch 应用机制
scripts/patches/openclaw-gateway-entry.patch 专用 gateway 入口 patch
scripts/build-openclaw-runtime.sh 构建流程集成
scripts/nsis-installer.nsh NSIS 安装器自定义宏
scripts/electron-builder-hooks.cjs Windows asar 路径兼容
src/main/libs/openclawEngineManager.ts gateway launcher 生成 + bundle 加载
src/main/libs/openclawConfigSync.ts 最小配置 + 保护已有 IM 配置
package.json 构建脚本链更新

Test plan

  • Windows 全新安装:gateway 启动成功,无 180s 超时
  • Windows 覆盖安装:已有 IM 配置不被覆盖
  • V8 compile cache:bundle import ~4s(有缓存)vs ~95s(无缓存)
  • 扩展预编译:37 个插件编译成功,3 个回退 jiti
  • macOS 打包签名不受影响

🤖 Generated with Claude Code

btc69m979y-dotcom and others added 8 commits March 11, 2026 15:55
在 Electron utilityProcess 中,openclaw 的 ~1100 个 ESM 模块解析极慢
(80-100s)。通过 esbuild 将 dist/entry.js 打包为单文件 gateway-bundle.mjs,
使用 import() 动态加载,消除模块解析开销。

主要改动:
- 新增 scripts/bundle-openclaw-gateway.cjs: esbuild 打包脚本,带缓存检查
- package.json: 添加 openclaw:bundle 步骤到所有平台构建链
- openclawEngineManager.ts: launcher 优先 import(bundle),失败回退到 require(dist/entry.js)
- 添加 esbuild devDependency
- launcher 中 argv[1] patch 确保 isMainModule() 识别 bundle 为主入口
- banner 注入 createRequire 解决 ESM 上下文中 CJS 模块的 require() 问题

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- 新增 scripts/patches/openclaw-gateway-entry.patch:
  为 openclaw 添加专用 gateway 入口(gateway-entry.ts),
  跳过 Commander CLI 基础设施,冷启动从 156s 降至 95s
- 新增 scripts/apply-openclaw-patches.cjs:
  自动化 patch 应用脚本,幂等执行,已应用则跳过
- 构建链中插入 openclaw:patch 步骤(ensure → patch → build)
- build-openclaw-runtime.sh 增加 patchHash 指纹,
  patch 文件变更时自动触发重新构建

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Enable V8 compile cache for the openclaw gateway to eliminate cold-start
V8 compilation overhead (~35s for the 26MB bundle).

Changes:
- Set NODE_COMPILE_CACHE env var in utilityProcess.fork() for ESM support
- Add enableCompileCache() + flushCompileCache() in gateway-launcher.cjs
- Add warmup-compile-cache.cjs script for pre-populating the cache
- Add NSIS post-install hook to run warmup during Windows installation
- Copy warmup script to runtime dir during bundle step

The compile cache is stored at <userData>/openclaw/state/.compile-cache
and persists across app restarts. On Windows NSIS install, the warmup
runs automatically after file extraction using ELECTRON_RUN_AS_NODE=1.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
On Windows, @electron/asar listPackage() returns backslash-separated
paths (e.g. \dist\entry.js) but the validation code checks for
forward-slash paths (/dist/entry.js). Normalize to forward slashes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
On a fresh install with no API model configured, syncOpenClawConfig()
would bail out entirely, leaving no openclaw.json file. Without this
file the gateway has no gateway.mode=local and exits silently.

Now writes a minimal config with gateway.mode=local and plugin entries
so the gateway can start immediately. The full config (with model
provider) is synced once the user configures an API in the UI.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Remove plugins from minimal openclaw config so gateway can bind port
  immediately on fresh install (plugins loaded via jiti were blocking
  httpServer.listen for minutes)
- Protect existing configs with IM plugin settings from being overwritten
  by minimal config on sync
- Show file extraction details in NSIS installer while hiding warmup
  operations from progress text
- Fix apply-openclaw-patches idempotent detection for patches containing
  new files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add scripts/precompile-openclaw-extensions.cjs that uses esbuild to
compile each extension's TypeScript entry point to JavaScript at build
time. This eliminates jiti's runtime Babel transpilation (~135s on
first Windows startup reduced to ~82s). Extensions already shipping
as .js (feishu-openclaw-plugin, wecom-openclaw-plugin) are skipped.

Also:
- Add install timing log to NSIS installer for diagnosing slow installs
- Hide empty details list (ShowInstDetails nevershow) since
  electron-builder uses 7z solid extraction with no per-file output
- Integrate precompile step into all openclaw:runtime:* build scripts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…claw-gateway-esbuild-bundle

# Conflicts:
#	package.json
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