From 75c9870fffaedb6eabe18c067f458d060dc5041f Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 18 Oct 2024 17:53:58 +0800 Subject: [PATCH] [ci] Check package size for Pull Requests (#17739) --- .github/workflows/interface-check-report.js | 15 ++++++- .github/workflows/package-size-check.js | 49 +++++++++++++++++++++ .github/workflows/web-interface-check.yml | 23 ++++++++++ cocos/asset/asset-manager/plist-parser.ts | 4 +- package-lock.json | 8 ++-- package.json | 2 +- 6 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/package-size-check.js diff --git a/.github/workflows/interface-check-report.js b/.github/workflows/interface-check-report.js index fc3a7582967..51f2e769f4e 100644 --- a/.github/workflows/interface-check-report.js +++ b/.github/workflows/interface-check-report.js @@ -4,9 +4,17 @@ const fs = require('fs'); const interfaceDiffPath = ps.join(__dirname, '../../interface-diff.txt'); let reportContent = fs.readFileSync(interfaceDiffPath, 'utf8'); +// Remove the first line that show the package size. +const firstLineEnd = reportContent.indexOf('\n'); +const packageSizeIncreasePrompt = reportContent.substring(0, firstLineEnd); +reportContent = reportContent.substring(firstLineEnd + 1); +// + if (reportContent.includes('@')) { reportContent = reportContent.split('\n').slice(3).join('\n'); - reportContent = `## Interface Check Report + reportContent = `## ${packageSizeIncreasePrompt} + +## Interface Check Report \`\`\`diff ! WARNING this pull request has changed these public interfaces: @@ -14,7 +22,10 @@ ${reportContent} \`\`\` `; } else { - reportContent = `## Interface Check Report + reportContent = `## ${packageSizeIncreasePrompt} + +## Interface Check Report This pull request does not change any public interfaces !`; } + fs.writeFileSync(interfaceDiffPath, reportContent, 'utf8'); diff --git a/.github/workflows/package-size-check.js b/.github/workflows/package-size-check.js new file mode 100644 index 00000000000..ea58509b211 --- /dev/null +++ b/.github/workflows/package-size-check.js @@ -0,0 +1,49 @@ +const fs = require('fs-extra'); +const ps = require('path'); +const { buildEngine } = require('@cocos/ccbuild'); + +const engineRoot = ps.resolve(__dirname, '..', '..'); +console.log(`Engine root: ${engineRoot}`); + +const exportsDir = ps.join(engineRoot, 'exports'); +const files = fs.readdirSync(exportsDir); +const features = []; +files.forEach(file => { + const filePath = ps.join(exportsDir, file); + const feature = ps.parse(ps.basename(filePath)).name; + features.push(feature); +}); + +console.log(`features: [ ${features.join(', ')} ]`); + +(async () => { + const outDir = ps.join(engineRoot, 'build-cc-out'); + + const options = { + engine: engineRoot, + out: outDir, + platform: "WECHAT", + moduleFormat: "system", + compress: true, + split: false, + nativeCodeBundleMode: "wasm", + assetURLFormat: "runtime-resolved", + noDeprecatedFeatures: true, + sourceMap: false, + features, + loose: true, + mode: "BUILD", + flags: { + DEBUG: false, + NET_MODE: 0, + SERVER_MODE: false + }, + wasmCompressionMode: 'brotli', + inlineEnum: true, + }; + + await fs.ensureDir(outDir); + await fs.emptyDir(outDir); + + await buildEngine(options); +})(); \ No newline at end of file diff --git a/.github/workflows/web-interface-check.yml b/.github/workflows/web-interface-check.yml index 0be0d252f0f..7cbe7b3314c 100644 --- a/.github/workflows/web-interface-check.yml +++ b/.github/workflows/web-interface-check.yml @@ -41,6 +41,9 @@ jobs: working-directory: ./engine run: | npm install + if [ -e ./.github/workflows/package-size-check.js ]; then + node ./.github/workflows/package-size-check.js + fi - name: Clear npm Cache run: | @@ -68,6 +71,7 @@ jobs: working-directory: ./engine-HEAD run: | npm install + node ./.github/workflows/package-size-check.js - uses: LouisBrunner/diff-action@v2.0.0 with: @@ -77,6 +81,25 @@ jobs: tolerance: worse output: ./engine/interface-diff.txt + - name: Check package size + run: | + BASE_SIZE=0 + if [ -d ./engine/build-cc-out ]; then + BASE_SIZE=$(du -sk ./engine/build-cc-out | awk '{print $1 * 1024}') + fi + HEAD_SIZE=$(du -sk ./engine-HEAD/build-cc-out | awk '{print $1 * 1024}') + DIFF_SIZE=$((HEAD_SIZE - BASE_SIZE)) + if [ "$DIFF_SIZE" -gt 0 ]; then + PACKAGE_SIZE_INFO="📈📈📈 Package Size increased by $DIFF_SIZE bytes, OLD: $BASE_SIZE, NEW: $HEAD_SIZE" + elif [ "$DIFF_SIZE" -lt 0 ]; then + PACKAGE_SIZE_INFO="📉📉📉 Package Size decreased by $DIFF_SIZE bytes, OLD: $BASE_SIZE, NEW: $HEAD_SIZE" + else + PACKAGE_SIZE_INFO="🟢🟢🟢 Package Size is not changed, BASE: $BASE_SIZE, HEAD: $HEAD_SIZE" + fi + echo "PACKAGE_SIZE_INFO: ${PACKAGE_SIZE_INFO}" + sed -i "1s/^/$PACKAGE_SIZE_INFO\n/" ./engine/interface-diff.txt + + - name: optimize interface check report working-directory: ./engine run: | diff --git a/cocos/asset/asset-manager/plist-parser.ts b/cocos/asset/asset-manager/plist-parser.ts index 4bd356d2e83..42e4866c6a0 100644 --- a/cocos/asset/asset-manager/plist-parser.ts +++ b/cocos/asset/asset-manager/plist-parser.ts @@ -67,10 +67,10 @@ class PlistParser extends SAXParser { /** * @en parse a xml string as plist object. * @zh 将xml字符串解析为plist对象。 - * @param {String} xmlTxt - plist xml contents + * @param xmlTxt - plist xml contents * @return {*} plist object */ - public parse (xmlTxt): any { + public parse (xmlTxt: string): any { const xmlDoc = this._parseXML(xmlTxt); const plist = xmlDoc.documentElement; if (plist.tagName !== 'plist') { diff --git a/package-lock.json b/package-lock.json index 4d7273c376a..301a7fef392 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "@babel/plugin-proposal-class-properties": "^7.18.6", "@cocos/box2d": "1.0.1", "@cocos/cannon": "1.2.8", - "@cocos/ccbuild": "^2.2.15", + "@cocos/ccbuild": "^2.2.16", "@cocos/dragonbones-js": "^1.0.1" }, "devDependencies": { @@ -2059,9 +2059,9 @@ } }, "node_modules/@cocos/ccbuild": { - "version": "2.2.15", - "resolved": "https://registry.npmjs.org/@cocos/ccbuild/-/ccbuild-2.2.15.tgz", - "integrity": "sha512-P4vBIghbF2nBjD+XwgYCLA7AFHDrk0ZYEJVaBQaB5zqpvXDqFsJc0PnMk2Q//5xVk0Aajhu6Lnwfls6xtY+9YA==", + "version": "2.2.16", + "resolved": "https://registry.npmjs.org/@cocos/ccbuild/-/ccbuild-2.2.16.tgz", + "integrity": "sha512-mq5nbEn5mx7+o3VIdtCipzu60uK0OHzsnQj5YpchfIP6K+TEj442duq1vYah69oVfchMo69M4Vpi7Pra5JHvZw==", "dependencies": { "@babel/core": "^7.20.12", "@babel/helper-module-imports": "7.18.6", diff --git a/package.json b/package.json index b4dbe7cc977..15f7eafa522 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "@babel/plugin-proposal-class-properties": "^7.18.6", "@cocos/box2d": "1.0.1", "@cocos/cannon": "1.2.8", - "@cocos/ccbuild": "^2.2.15", + "@cocos/ccbuild": "^2.2.16", "@cocos/dragonbones-js": "^1.0.1" } }