Skip to content

Commit 1c10ae5

Browse files
fix(build): externalize @galacean peers in miniprogram build instead of bundling (#350)
fix(build): externalize @galacean peers in miniprogram build instead of bundling
1 parent b89bb15 commit 1c10ae5

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎rollup.config.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,13 @@ function makeRollupConfig(pkg) {
157157
sourcemap: false,
158158
format: "cjs"
159159
},
160+
// Externalize each peer/dep both at its package name and its `/dist/miniprogram` entry:
161+
// @galacean/engine is redirected to the latter by miniProgramPlugin, while peers without a
162+
// dedicated miniprogram build (e.g. @galacean/engine-xr) stay external at the package name
163+
// instead of being bundled in.
160164
external: Object.keys(Object.assign(pkg.pkgJson.dependencies ?? {}, pkg.pkgJson.peerDependencies ?? {}))
161165
.concat("@galacean/engine-miniprogram-adapter")
162-
.map((name) => `${name}/dist/miniprogram`),
166+
.flatMap((name) => [name, `${name}/dist/miniprogram`]),
163167
plugins: [...plugins, ...miniProgramPlugin]
164168
});
165169

‎rollup.miniprogram.plugin.mjs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,24 @@ adapterArray.forEach((name) => {
4242
adapterVars[name] = register(name);
4343
});
4444

45-
const regStr = [`"@galacean/engine"`, `'@galacean/engine-xr'`].join("|");
45+
// Only `@galacean/engine` ships a dedicated miniprogram build whose specifier must be
46+
// redirected to `<pkg>/dist/miniprogram`; other @galacean peers (e.g. @galacean/engine-xr)
47+
// have no such entry and are externalized at their normal package name (see rollup.config.mjs).
48+
//
49+
// The previous implementation matched `@galacean/engine` with double quotes but
50+
// `@galacean/engine-xr` with single quotes; the transpiled source uses double quotes, so the
51+
// `@galacean/engine-xr` import was never matched, fell through to bundling, and dragged
52+
// @galacean/engine-math in with it — bloating dist/miniprogram.js by ~2MB. Matching either
53+
// quote and redirecting only the package that actually has a miniprogram build avoids both.
54+
const redirectModules = ["@galacean/engine"];
55+
const moduleAlternation = redirectModules.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|");
4656

4757
export default [
4858
inject(adapterVars),
4959
modify({
50-
find: new RegExp(regStr, "g"),
51-
replace: (match, moduleName) => {
52-
return `${match.substr(0, match.length - 1)}/dist/miniprogram"`;
53-
}
60+
// Match the specifier in single OR double quotes, anchored by a back-reference so
61+
// `@galacean/engine` does not partially match `@galacean/engine-xr`.
62+
find: new RegExp(`(['"])(${moduleAlternation})\\1`, "g"),
63+
replace: (_match, quote, name) => `${quote}${name}/dist/miniprogram${quote}`
5464
})
5565
];

0 commit comments

Comments
 (0)