no message

This commit is contained in:
2026-06-18 14:07:38 +08:00
parent d393302388
commit 18990deb2d
12 changed files with 910 additions and 116 deletions

View File

@@ -3,8 +3,10 @@
* 供 package-for-project.js / package-for-cdn.js 共用。
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const zlib = require('zlib');
const { execSync } = require('child_process');
function formatBytes(n) {
if (n >= 1024 * 1024) return `${(n / 1024 / 1024).toFixed(1)} MB`;
@@ -46,6 +48,23 @@ function brotliCompressFile(srcPath, dstPath, quality = 9) {
return { raw: input.length, br: out.length };
}
/** 为 WebGL 目录下所有 .bundle 生成 .bundle.br */
function brotliCompressWebglBundles(webglDir, opts = {}) {
if (!fs.existsSync(webglDir)) return { count: 0, saved: 0 };
let count = 0;
let saved = 0;
for (const name of fs.readdirSync(webglDir)) {
if (!name.endsWith('.bundle') || name.endsWith('.bundle.br')) continue;
const src = path.join(webglDir, name);
if (!fs.statSync(src).isFile()) continue;
const { raw, br } = brotliCompressFile(src, src + '.br', opts.quality);
count += 1;
saved += raw - br;
console.log(`>>> ${name}.br: ${formatBytes(raw)}${formatBytes(br)}`);
}
return { count, saved };
}
/**
* 调整预加载 bundle默认只预加载 mainresources / level-prefabs 按需加载。
* @param {object} opts
@@ -68,6 +87,50 @@ function patchPreloadSettings(settingsObj, opts = {}) {
return settingsObj;
}
/**
* 关闭 Cocos 启动闪屏("Created with Cocos")。
* totalTime <= 0 时引擎跳过闪屏等待,首屏可快约 2s不影响关卡切换与网络下载。
* @param {object} opts
* @param {boolean} [opts.disableSplash=true]
* @param {boolean} [opts.stripSplashAssets=false] 去掉内嵌 logo/background略减 scenes 包体积
*/
function patchSplashSettings(settingsObj, opts = {}) {
const disableSplash = opts.disableSplash !== false;
if (!disableSplash) return settingsObj;
settingsObj.splashScreen = settingsObj.splashScreen || {};
settingsObj.splashScreen.totalTime = 0;
if (opts.stripSplashAssets) {
delete settingsObj.splashScreen.logo;
delete settingsObj.splashScreen.background;
}
return settingsObj;
}
/**
* 就地修改 zip bundle 内的 settings.json必须用 zip -0 storeloader 不支持 deflate
*/
function patchSplashInZipBundle(zipPath, opts = {}) {
const absZip = path.resolve(zipPath);
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'cocos-splash-'));
try {
execSync(`unzip -q -o "${absZip}" -d "${tmp}"`, { stdio: 'pipe' });
const settingsPath = path.join(tmp, 'src', 'settings.json');
if (!fs.existsSync(settingsPath)) return false;
const j = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
patchSplashSettings(j, opts);
fs.writeFileSync(settingsPath, JSON.stringify(j));
const newZip = absZip + '.new';
if (fs.existsSync(newZip)) fs.unlinkSync(newZip);
execSync(`cd "${tmp}" && zip -0 -q -r "${newZip}" .`, { stdio: 'pipe' });
fs.renameSync(newZip, absZip);
return true;
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
}
function printPackageReport(outDir, opts = {}) {
const lines = ['\n>>> 包体积报告:'];
const entries = [
@@ -101,6 +164,9 @@ module.exports = {
fileSize,
minifyLevelsDatabase,
brotliCompressFile,
brotliCompressWebglBundles,
patchPreloadSettings,
patchSplashSettings,
patchSplashInZipBundle,
printPackageReport,
};