Skip to content

Commit 6428515

Browse files
Tianze-Chenclaude
andcommitted
Add the format-trimming panel and per-format build exclusion
New panel (panels/formats.js + editor/trim.js): pick the formats you need; applying moves decoder sources between runtime/ and trimmed/ (move, not copy, .meta travels so UUIDs survive), regenerates codecs.ts/index.ts, records the choice in editor/build/trim.json, and rewrites native/cc_plugin.json platforms so trimmed formats drop out of native builds too. Build hooks and main.js read trim.json to skip staging/copying the ~89KB animated-webp.wasm when WebP is trimmed. main.js also aligns disk state with the saved profile on load and warns when the extension is installed globally (shared runtime/ across projects). WebP ships off by default, and the committed tree matches: sources parked in trimmed/, codecs.ts without the webp entry, trim.json webp:false, cc_plugin.json platforms:[]. Reason: its off-native backend needs an engine that exports cc.wasm (cocos/cocos4#306) and no stable release has that yet, so defaulting it on would ship ~113KB nothing can load. Cocos 3.8 treats every mounted script as a bundle entry with no tree-shaking of unreferenced scripts, so physically moving the files is the only way trimming actually shrinks packages; README documents the mechanism, the WebP engine requirement, and the payload table. Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent 8483138 commit 6428515

18 files changed

Lines changed: 861 additions & 90 deletions

README.md

Lines changed: 72 additions & 51 deletions
Large diffs are not rendered by default.

editor/build/hooks.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
* runtime/webp/index.ts passes the bare name `animated-webp.wasm`, matching the
1818
* destination file name below.
1919
*
20-
* If the WebP codec group in runtime/codecs.ts is commented out the runtime never
21-
* loads this file, but it is still copied — the hook cannot see which codecs the
22-
* project kept. Delete it from the output, or drop this contribution, if the ~89KB
23-
* matters.
20+
* Whether WebP is wanted at all comes from trim.json, which the format panel
21+
* derives from the project's profile (see editor/trim.js). It is read with plain
22+
* fs rather than Editor.Profile because this file runs in the builder's worker
23+
* process, where the Editor API is not available. A missing or unreadable
24+
* trim.json means "keep everything", matching the committed default.
2425
*/
2526

2627
const fs = require('fs');
2728
const path = require('path');
2829

2930
const WASM_NAME = 'animated-webp.wasm';
3031
const WASM_SOURCE = path.join(__dirname, '..', '..', 'native', 'wasm', 'prebuilt', WASM_NAME);
32+
const TRIM_JSON = path.join(__dirname, 'trim.json');
3133

3234
// Platforms that use the JSB binding rather than wasm, so they need no .wasm.
3335
// linux / ohos / harmonyos are listed even though the native plugin mechanism
@@ -48,6 +50,22 @@ function shouldCopy (platform) {
4850
return !platform || !NATIVE_PLATFORMS.has(platform);
4951
}
5052

53+
// Absent / unparseable / missing key all mean "kept", so a user who never opens
54+
// the panel gets exactly today's behaviour.
55+
function readTrim () {
56+
try {
57+
const parsed = JSON.parse(fs.readFileSync(TRIM_JSON, 'utf8'));
58+
return parsed && typeof parsed === 'object' ? parsed : {};
59+
} catch (e) {
60+
return {};
61+
}
62+
}
63+
64+
function describeTrim (trim) {
65+
const keys = ['gif', 'apng', 'webp', 'demo'];
66+
return keys.map((k) => `${k}=${trim[k] === false ? 'off' : 'on'}`).join(' ');
67+
}
68+
5169
// The output root has appeared under both names across editor versions.
5270
function findCocosJsDir (result) {
5371
const bases = [];
@@ -70,6 +88,16 @@ exports.onAfterBuild = async function onAfterBuild (options, result) {
7088
// A throw here fails the whole build, which would be a wildly
7189
// disproportionate outcome for one optional codec's payload.
7290
try {
91+
// The only signal in the build log that says which formats this package
92+
// actually contains. Worth one line: everything else about trimming is
93+
// invisible until you diff the output.
94+
const trim = readTrim();
95+
log(`formats: ${describeTrim(trim)}`);
96+
97+
if (trim.webp === false) {
98+
log(`skip ${WASM_NAME} (WebP 已裁剪)`);
99+
return;
100+
}
73101
const platform = options && options.platform;
74102
if (!shouldCopy(platform)) {
75103
log(`skip ${WASM_NAME} (native platform: ${platform})`);

editor/build/trim.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"gif": true,
3+
"apng": true,
4+
"webp": false,
5+
"demo": true
6+
}

0 commit comments

Comments
 (0)