Skip to content

fix(build): externalize @galacean peers in miniprogram build instead of bundling#350

Merged
cptbtptpbcptdtptp merged 1 commit into
mainfrom
fix/miniprogram-externalize-engine-xr
Jun 16, 2026
Merged

fix(build): externalize @galacean peers in miniprogram build instead of bundling#350
cptbtptpbcptdtptp merged 1 commit into
mainfrom
fix/miniprogram-externalize-engine-xr

Conversation

@cptbtptpbcptdtptp

@cptbtptpbcptdtptp cptbtptpbcptdtptp commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Fixes #349.

Problem

@galacean/engine-toolkit-xr@1.6.0 ships a ~2.5 MB dist/miniprogram.js (vs ~80 KB) because @galacean/engine-xr and its @galacean/engine-math dependency are bundled inline instead of kept external.

Two defects in the miniprogram build config:

  1. rollup.config.mjs — the external list only contained the <pkg>/dist/miniprogram variants, so any peer import that wasn't redirected there fell through to bundling.
  2. rollup.miniprogram.plugin.mjs — the redirect regex hard-coded mismatched quotes ("@galacean/engine" double, '@galacean/engine-xr' single). The transpiled source uses double quotes, so the @galacean/engine-xr import was never redirected → bundled → dragged @galacean/engine-math in with it.

Note: @galacean/engine-xr ships no /dist/miniprogram build (only dist/main.js/dist/module.js), so it must be externalized at its bare name — only @galacean/engine actually has a miniprogram build to redirect to.

Change

  • external now lists each peer/dep at both its package name and /dist/miniprogram, so a peer that isn't redirected stays external instead of being bundled.
  • The redirect now targets only @galacean/engine and is quote-agnostic (matches single or double quotes, anchored by a back-reference so @galacean/engine can't partially match @galacean/engine-xr).

Result: @galacean/enginerequire('@galacean/engine/dist/miniprogram'), @galacean/engine-xrrequire('@galacean/engine-xr') (a path that exists), neither bundled.

Verification

A full native pnpm build wasn't run here, so the fix was verified with a focused harness using the real rollup + rollup-plugin-modify and the exact external lists, stubbing the engine packages so only the external-vs-bundled decision is exercised:

OLD (buggy)   | engine -> /dist/miniprogram: true | engine-xr external(bare): false | engine-xr BUNDLED: true
NEW (fixed)   | engine -> /dist/miniprogram: true | engine-xr external(bare): true  | engine-xr BUNDLED: false

i.e. the original config bundles @galacean/engine-xr; the patched config externalizes it to require('@galacean/engine-xr'), while @galacean/engine stays redirected to its miniprogram build. A native build + a CI dist/miniprogram.js size guard would be a good follow-up to lock this in.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Fixed module redirection logic to prevent incorrect package path resolution in miniprogram builds
  • Chores

    • Optimized miniprogram build configuration for improved dependency externalization handling

…of bundling

The miniprogram bundle of @galacean/engine-toolkit-xr shipped a ~2.5MB
dist/miniprogram.js (vs ~80KB) because @galacean/engine-xr and its
@galacean/engine-math dependency were bundled inline instead of kept external.

Two issues caused it:

- The external list only contained the `<pkg>/dist/miniprogram` variants, so any
  peer import that was not redirected there fell through to bundling.
- The redirect regex hard-coded mismatched quotes (`"@galacean/engine"` vs
  `'@galacean/engine-xr'`). The transpiled source uses double quotes, so the
  `@galacean/engine-xr` import was never redirected and got bundled, dragging
  @galacean/engine-math along with it.

Externalize every peer/dep at both its package name and `/dist/miniprogram`, and
redirect only @galacean/engine (the sole package that actually ships a miniprogram
build). @galacean/engine-xr now stays external at `@galacean/engine-xr`, which is a
path that exists, rather than being bundled or redirected to a missing entry.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Two bugs in the miniprogram Rollup build are fixed. The rollup.miniprogram.plugin.mjs plugin's module-redirect regex is replaced with a quote-aware, back-referenced pattern that targets only @galacean/engine. The rollup.config.mjs external list is changed to emit both the bare package name and the /dist/miniprogram path for each dependency.

Changes

Miniprogram bundle externalization fix

Layer / File(s) Summary
Plugin redirect regex: target only @galacean/engine
rollup.miniprogram.plugin.mjs
Introduces redirectModules constant and a dynamically generated alternation pattern. The modify plugin's find regex now uses a capture group for quote style with a back-reference, preventing partial matches against @galacean/engine-xr. The replace appends /dist/miniprogram while preserving the original quote character.
Config: externalize deps at both bare name and /dist/miniprogram
rollup.config.mjs
Switches the miniprogram build's external list from .map to .flatMap, so each dependency/peer dependency is externalized as both name and ${name}/dist/miniprogram. Inline comments document which packages have a dedicated miniprogram build versus which are external at their bare name only.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A sneaky regex once matched the wrong hare,
Bundling up engine-xr beyond its fair share.
Now quotes are back-referenced, names are precise,
And flatMap externalizes everything twice.
The miniprogram dist? A svelte 80 KB—
No more 2.5 MB ruining the day! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: externalizing @galacean peer dependencies in the miniprogram build instead of bundling them, which directly addresses the ~2.5 MB bundle size issue.
Linked Issues check ✅ Passed The PR successfully implements all requirements from issue #349: externalizes peers at both package name and /dist/miniprogram path, fixes the quote-agnostic redirect for @galacean/engine only, and prevents @galacean/engine-xr from being bundled.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the two configuration files identified in issue #349 as root causes; no unrelated modifications or scope creep is present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/miniprogram-externalize-engine-xr

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
rollup.config.mjs (1)

153-168: Add a CI size budget gate for dist/miniprogram.js.

Given this regression persisted across multiple releases, add a CI check that fails when miniprogram output exceeds a threshold (for example, ~150 KB) so future externalization regressions are caught pre-release.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rollup.config.mjs` around lines 153 - 168, Add a CI size budget gate for the
dist/miniprogram.js build output. The rollup configuration generates
miniprogram.js but there is no automated check to prevent size regressions.
Implement a size check in your CI workflow that runs after the build step and
fails the build if dist/miniprogram.js exceeds approximately 150 KB, capturing
size violations before they reach release. This can be done through a dedicated
size checking tool (such as bundlesize or similar CI plugins) or by adding a
post-build script that compares the actual output size against your threshold
and exits with an error code if exceeded.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@rollup.config.mjs`:
- Around line 153-168: Add a CI size budget gate for the dist/miniprogram.js
build output. The rollup configuration generates miniprogram.js but there is no
automated check to prevent size regressions. Implement a size check in your CI
workflow that runs after the build step and fails the build if
dist/miniprogram.js exceeds approximately 150 KB, capturing size violations
before they reach release. This can be done through a dedicated size checking
tool (such as bundlesize or similar CI plugins) or by adding a post-build script
that compares the actual output size against your threshold and exits with an
error code if exceeded.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 7f8d5abb-2d69-4398-9a53-87b2e04c03d7

📥 Commits

Reviewing files that changed from the base of the PR and between b89bb15 and 095ea5b.

📒 Files selected for processing (2)
  • rollup.config.mjs
  • rollup.miniprogram.plugin.mjs

@cptbtptpbcptdtptp cptbtptpbcptdtptp merged commit 1c10ae5 into main Jun 16, 2026
2 checks passed
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.

engine-toolkit-xr miniprogram build bundles engine-xr + engine-math (~2.5MB dist/miniprogram.js)

1 participant