From 226be758853f3efef23bf9dbcb4070ebcaa0ed06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E9=9C=91?= Date: Tue, 16 Jun 2026 11:22:29 +0800 Subject: [PATCH] fix(build): externalize @galacean peers in miniprogram build instead 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 `/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) (cherry picked from commit 095ea5b6169f27f6847d8948e9579aeda6b74d36) --- rollup.config.mjs | 6 +++++- rollup.miniprogram.plugin.mjs | 20 +++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index 408cb924..0ba1b9a3 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -199,9 +199,13 @@ function makeRollupConfig(pkg) { sourcemap: false, format: "cjs" }, + // Externalize each peer/dep both at its package name and its `/dist/miniprogram` entry: + // @galacean/engine is redirected to the latter by miniProgramPlugin, while peers without a + // dedicated miniprogram build (e.g. @galacean/engine-xr) stay external at the package name + // instead of being bundled in. external: Object.keys(Object.assign(pkg.pkgJson.dependencies ?? {}, pkg.pkgJson.peerDependencies ?? {})) .concat("@galacean/engine-miniprogram-adapter") - .map((name) => `${name}/dist/miniprogram`), + .flatMap((name) => [name, `${name}/dist/miniprogram`]), plugins: [...plugins, ...miniProgramPlugin] }); diff --git a/rollup.miniprogram.plugin.mjs b/rollup.miniprogram.plugin.mjs index a041454e..797d4a11 100644 --- a/rollup.miniprogram.plugin.mjs +++ b/rollup.miniprogram.plugin.mjs @@ -42,14 +42,24 @@ adapterArray.forEach((name) => { adapterVars[name] = register(name); }); -const regStr = [`"@galacean/engine"`, `'@galacean/engine-xr'`].join("|"); +// Only `@galacean/engine` ships a dedicated miniprogram build whose specifier must be +// redirected to `/dist/miniprogram`; other @galacean peers (e.g. @galacean/engine-xr) +// have no such entry and are externalized at their normal package name (see rollup.config.mjs). +// +// The previous implementation matched `@galacean/engine` with double quotes but +// `@galacean/engine-xr` with single quotes; the transpiled source uses double quotes, so the +// `@galacean/engine-xr` import was never matched, fell through to bundling, and dragged +// @galacean/engine-math in with it — bloating dist/miniprogram.js by ~2MB. Matching either +// quote and redirecting only the package that actually has a miniprogram build avoids both. +const redirectModules = ["@galacean/engine"]; +const moduleAlternation = redirectModules.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"); export default [ inject(adapterVars), modify({ - find: new RegExp(regStr, "g"), - replace: (match, moduleName) => { - return `${match.substr(0, match.length - 1)}/dist/miniprogram"`; - } + // Match the specifier in single OR double quotes, anchored by a back-reference so + // `@galacean/engine` does not partially match `@galacean/engine-xr`. + find: new RegExp(`(['"])(${moduleAlternation})\\1`, "g"), + replace: (_match, quote, name) => `${quote}${name}/dist/miniprogram${quote}` }) ];