From fd163eeff4f960fb19fb96adf027a258f8258232 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 13 Feb 2025 18:41:58 +0800 Subject: [PATCH] Refactor CI package size check (#18323) --- .github/workflows/interface-check-report.js | 15 +- .github/workflows/package-size-check.js | 123 +++++++++++-- .github/workflows/web-interface-check.yml | 194 ++++++++++++++------ cocos/2d/components/label.ts | 7 +- 4 files changed, 250 insertions(+), 89 deletions(-) diff --git a/.github/workflows/interface-check-report.js b/.github/workflows/interface-check-report.js index 51f2e769f4..fc3a758296 100644 --- a/.github/workflows/interface-check-report.js +++ b/.github/workflows/interface-check-report.js @@ -4,17 +4,9 @@ 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 = `## ${packageSizeIncreasePrompt} - -## Interface Check Report + reportContent = `## Interface Check Report \`\`\`diff ! WARNING this pull request has changed these public interfaces: @@ -22,10 +14,7 @@ ${reportContent} \`\`\` `; } else { - reportContent = `## ${packageSizeIncreasePrompt} - -## Interface Check Report + reportContent = `## 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 index 01a5149cdf..f2528639a3 100644 --- a/.github/workflows/package-size-check.js +++ b/.github/workflows/package-size-check.js @@ -1,38 +1,90 @@ const fs = require('fs-extra'); const ps = require('path'); const { buildEngine } = require('@cocos/ccbuild'); +const { platform } = require('os'); + +const args = process.argv.slice(2); +if (args.length == 0) { + console.error('Please specify the engine root path'); + process.exit(1); +} +const engineRoot = args[0]; -const engineRoot = ps.resolve(__dirname, '..', '..'); console.log(`Engine root: ${engineRoot}`); const exportsDir = ps.join(engineRoot, 'exports'); const files = fs.readdirSync(exportsDir); -const features = []; + +const allFeatures = []; files.forEach(file => { const filePath = ps.join(exportsDir, file); const feature = ps.parse(ps.basename(filePath)).name; if (feature !== 'vendor-google' && feature !== 'xr') { - features.push(feature); + allFeatures.push(feature); } }); +allFeatures.push('meshopt'); // meshopt feature doesn't have a module entry in 'exports' directory, so append it manually here. -console.log(`features: [ ${features.join(', ')} ]`); +console.log(`all features: [ ${allFeatures.join(', ')} ]`); -(async () => { - const outDir = ps.join(engineRoot, 'build-cc-out'); +const features2DCommon = [ + "2d", + "affine-transform", + "animation", + "audio", + "base", + "dragon-bones", + "gfx-webgl", + "gfx-webgl2", + "graphics", + "intersection-2d", + "mask", + "particle-2d", + "physics-2d-framework", + "physics-2d-builtin", + "physics-2d-box2d", + "physics-2d-box2d-wasm", + "profiler", + "rich-text", + "spine", + "tiled-map", + "tween", + "ui", + "ui-skew", + "video", + "webview", +]; - const options = { +const features2DEmptyLegacyPipeline = [ + "2d", + "audio", + "base", + "gfx-webgl2", + "legacy-pipeline", +]; + +const features2DLegacyPipeline = [...features2DCommon, "legacy-pipeline"]; +const features2DNewPipeline = [...features2DCommon, "custom-pipeline", "custom-pipeline-builtin-scripts"]; + +console.log(`2d features: [ ${features2DLegacyPipeline.join(', ')} ]`); + +async function buildEngineForFeatures(options) { + console.log(`>>> ==============================================================`); + console.log(`>>> ============== features: ${options.features.join(', ')}`); + console.log(`>>> ============== outDir: ${options.outDir}`); + const outDir = options.outDir; + const ccbuildOptions = { engine: engineRoot, out: outDir, - platform: "WECHAT", + platform: options.platform, moduleFormat: "system", compress: true, split: false, - nativeCodeBundleMode: "wasm", + nativeCodeBundleMode: options.nativeCodeBundleMode, assetURLFormat: "runtime-resolved", - noDeprecatedFeatures: true, + noDeprecatedFeatures: options.noDeprecatedFeatures, sourceMap: false, - features, + features: options.features, loose: true, mode: "BUILD", flags: { @@ -40,7 +92,7 @@ console.log(`features: [ ${features.join(', ')} ]`); NET_MODE: 0, SERVER_MODE: false }, - wasmCompressionMode: 'brotli', + wasmCompressionMode: options.wasmCompressionMode, inlineEnum: true, mangleProperties: { mangleList: [ @@ -70,5 +122,50 @@ console.log(`features: [ ${features.join(', ')} ]`); await fs.ensureDir(outDir); await fs.emptyDir(outDir); - await buildEngine(options); + await buildEngine(ccbuildOptions); +} + +(async () => { + await buildEngineForFeatures({ + features: allFeatures, + outDir: ps.join(engineRoot, 'build-cc-out-all'), + noDeprecatedFeatures: false, + platform: "WECHAT", + nativeCodeBundleMode: "wasm", + wasmCompressionMode: 'brotli', + }); + + await buildEngineForFeatures({ + features: allFeatures, + outDir: ps.join(engineRoot, 'build-cc-out-all-web'), + noDeprecatedFeatures: false, + platform: "HTML5", + }); + + await buildEngineForFeatures({ + features: features2DLegacyPipeline, + outDir: ps.join(engineRoot, 'build-cc-out-2d-legacy-pipline'), + noDeprecatedFeatures: true, + platform: "WECHAT", + nativeCodeBundleMode: "wasm", + wasmCompressionMode: 'brotli', + }); + + await buildEngineForFeatures({ + features: features2DNewPipeline, + outDir: ps.join(engineRoot, 'build-cc-out-2d-new-pipline'), + noDeprecatedFeatures: true, + platform: "WECHAT", + nativeCodeBundleMode: "wasm", + wasmCompressionMode: 'brotli', + }); + + await buildEngineForFeatures({ + features: features2DEmptyLegacyPipeline, + outDir: ps.join(engineRoot, 'build-cc-out-2d-empty-legacy-pipline'), + noDeprecatedFeatures: true, + platform: "WECHAT", + nativeCodeBundleMode: "wasm", + wasmCompressionMode: 'brotli', + }); })(); diff --git a/.github/workflows/web-interface-check.yml b/.github/workflows/web-interface-check.yml index f28a6968a2..ea5d7d4999 100644 --- a/.github/workflows/web-interface-check.yml +++ b/.github/workflows/web-interface-check.yml @@ -54,6 +54,48 @@ jobs: run: | echo "BASE_REF=$(git rev-parse HEAD)" >> $GITHUB_ENV + - uses: actions/checkout@v4 + name: Checkout Head Ref + with: + ref: 'refs/pull/${{ github.event.pull_request.number }}/head' + path: './engine-HEAD' + fetch-depth: 0 + + - name: Head merges base + working-directory: ./engine-HEAD + run: | + git config user.name "Cocos CI" + git config user.email "cocos-ci@cocos.com" + echo "BASE_REF: ${{ env.BASE_REF }}" + HEAD_REF=$(git rev-parse HEAD) + echo "HEAD_REF: $HEAD_REF" + echo "--------------------------------------------------------" + echo "HEAD LOG:" + git --no-pager log -3 + echo "--------------------------------------------------------" + git fetch origin ${{ env.BASE_REF }} + git reset --hard ${{ env.BASE_REF }} + echo "--------------------------------------------------------" + echo "BASE LOG:" + git --no-pager log -3 + echo "--------------------------------------------------------" + CURRENT_REF=$(git rev-parse HEAD) + echo "---- $CURRENT_REF merges $HEAD_REF ----" + git merge $HEAD_REF + echo "--------------------------------------------------------" + echo "MERGED LOG:" + git --no-pager log -5 + echo "--------------------------------------------------------" + + - name: Show Refs + run: | + cd ./engine + echo "BASE LOG:" + git --no-pager log -3 + cd ../engine-HEAD + echo "HEAD LOG:" + git --no-pager log -3 + - run: | echo "EXT_VERSION=$(node ./engine/.github/workflows/get-native-external-version.js)" >> $GITHUB_ENV @@ -111,12 +153,15 @@ jobs: git status - name: Build Base Declarations - working-directory: ./engine + working-directory: . run: | + ROOT_DIR=$(pwd) + cd engine npm install - if [ -e ./.github/workflows/package-size-check.js ]; then - node ./.github/workflows/package-size-check.js - fi + cd ../engine-HEAD + npm install --ignore-scripts + # Use package-size-check.js in HEAD + node .github/workflows/package-size-check.js "$ROOT_DIR/engine" - name: Restore patches (BASE) run: | @@ -130,39 +175,6 @@ jobs: run: | npm cache clean --force - - uses: actions/checkout@v4 - name: Checkout Head Ref - with: - ref: 'refs/pull/${{ github.event.pull_request.number }}/head' - path: './engine-HEAD' - fetch-depth: 0 - - - name: Head merges base - working-directory: ./engine-HEAD - run: | - git config user.name "Cocos CI" - git config user.email "cocos-ci@cocos.com" - echo "BASE_REF: ${{ env.BASE_REF }}" - HEAD_REF=$(git rev-parse HEAD) - echo "HEAD_REF: $HEAD_REF" - echo "--------------------------------------------------------" - echo "HEAD LOG:" - git --no-pager log -3 - echo "--------------------------------------------------------" - git fetch origin ${{ env.BASE_REF }} - git reset --hard ${{ env.BASE_REF }} - echo "--------------------------------------------------------" - echo "BASE LOG:" - git --no-pager log -3 - echo "--------------------------------------------------------" - CURRENT_REF=$(git rev-parse HEAD) - echo "---- $CURRENT_REF merges $HEAD_REF ----" - git merge $HEAD_REF - echo "--------------------------------------------------------" - echo "MERGED LOG:" - git --no-pager log -5 - echo "--------------------------------------------------------" - - run: | echo "EXT_VERSION_HEAD=$(node ./engine-HEAD/.github/workflows/get-native-external-version.js)" >> $GITHUB_ENV @@ -229,7 +241,7 @@ jobs: working-directory: ./engine-HEAD run: | npm install - node ./.github/workflows/package-size-check.js + node ./.github/workflows/package-size-check.js "$(pwd)" - name: Restore patches (HEAD) run: | @@ -245,33 +257,95 @@ jobs: new: ./engine-HEAD/bin/.declarations/cc.d.ts mode: addition 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 -sb ./engine/build-cc-out | awk '{print $1}') - fi - HEAD_SIZE=$(du -sb ./engine-HEAD/build-cc-out | awk '{print $1}') - DIFF_SIZE=$((HEAD_SIZE - BASE_SIZE)) - if [ "$DIFF_SIZE" -gt 0 ]; then - PACKAGE_SIZE_INFO="โš ๏ธ Package size โคด $DIFF_SIZE bytes, old: $BASE_SIZE, new: $HEAD_SIZE" - elif [ "$DIFF_SIZE" -lt 0 ]; then - PACKAGE_SIZE_INFO="๐Ÿ‘ Package size โคต $DIFF_SIZE bytes, old: $BASE_SIZE, new: $HEAD_SIZE" - else - PACKAGE_SIZE_INFO="โœ… Package size is not changed" - fi - echo "PACKAGE_SIZE_INFO: ${PACKAGE_SIZE_INFO}" - sed -i "1s/^/$PACKAGE_SIZE_INFO\n/" ./engine/interface-diff.txt - + output: ./engine-HEAD/interface-diff.txt - name: optimize interface check report - working-directory: ./engine + working-directory: ./engine-HEAD run: | cat ./interface-diff.txt node ./.github/workflows/interface-check-report.js + - name: After optimize interface check report + working-directory: ./engine-HEAD + run: | + cat ./interface-diff.txt + + - name: Check Package Size + run: | + PACKAGE_SIZE_INFO="## Code Size Check Report + | Wechat (WASM) | Before | After | Diff | + |:-:|:-:|:-:|:-:|" + + BASE_SIZE=0 + HEAD_SIZE=0 + DIFF_SIZE=0 + ICON="โœ… " + + reset_diff_vars() { + BASE_SIZE=0 + HEAD_SIZE=0 + DIFF_SIZE=0 + ICON="โœ… " + } + + check_diff() { + ccbuild_out_folder_name="$1" + if [ -d ./engine/$ccbuild_out_folder_name ]; then + BASE_SIZE=$(du -sb ./engine/$ccbuild_out_folder_name | awk '{print $1}') + fi + HEAD_SIZE=$(du -sb ./engine-HEAD/$ccbuild_out_folder_name | awk '{print $1}') + DIFF_SIZE=$((HEAD_SIZE - BASE_SIZE)) + if [ "$DIFF_SIZE" -gt 0 ]; then + ICON="โš ๏ธ +" + elif [ "$DIFF_SIZE" -lt 0 ]; then + ICON="๐Ÿ‘ " + else + ICON="โœ… " + fi + } + + # Check Wechat platform + check_diff "build-cc-out-2d-empty-legacy-pipline" + PACKAGE_SIZE_INFO=$(printf "%s\n%s\n" "${PACKAGE_SIZE_INFO}" "| 2D Empty (legacy pipeline) | $BASE_SIZE bytes | $HEAD_SIZE bytes | $ICON$DIFF_SIZE bytes |") + reset_diff_vars + + check_diff "build-cc-out-2d-legacy-pipline" + PACKAGE_SIZE_INFO=$(printf "%s\n%s\n" "${PACKAGE_SIZE_INFO}" "| 2D All (legacy pipeline) | $BASE_SIZE bytes | $HEAD_SIZE bytes | $ICON$DIFF_SIZE bytes |") + reset_diff_vars + + check_diff "build-cc-out-2d-new-pipline" + PACKAGE_SIZE_INFO=$(printf "%s\n%s\n" "${PACKAGE_SIZE_INFO}" "| 2D All (new pipeline) | $BASE_SIZE bytes | $HEAD_SIZE bytes | $ICON$DIFF_SIZE bytes |") + reset_diff_vars + + check_diff "build-cc-out-all" + PACKAGE_SIZE_INFO=$(printf "%s\n%s\n" "${PACKAGE_SIZE_INFO}" "| (2D + 3D) All | $BASE_SIZE bytes | $HEAD_SIZE bytes | $ICON$DIFF_SIZE bytes |") + reset_diff_vars + + PACKAGE_SIZE_INFO=$(printf "%s\n" "${PACKAGE_SIZE_INFO}") + + # Check Web platform + WEB_PLATFORM_TABLE_HEADER="| Web (WASM + ASMJS) | Before | After | Diff | + |:-:|:-:|:-:|:-:|" + PACKAGE_SIZE_INFO=$(printf "%s\n\n\n%s\n" "${PACKAGE_SIZE_INFO}" "$WEB_PLATFORM_TABLE_HEADER") + + check_diff "build-cc-out-all-web" + PACKAGE_SIZE_INFO=$(printf "%s\n%s\n" "${PACKAGE_SIZE_INFO}" "| (2D + 3D) All | $BASE_SIZE bytes | $HEAD_SIZE bytes | $ICON$DIFF_SIZE bytes |") + reset_diff_vars + + printf "%s\n" "$PACKAGE_SIZE_INFO" + + DIFF_FILE_PATH=./engine-HEAD/interface-diff.txt + ORIGINAL_DIFF_CONTENT=$(cat ${DIFF_FILE_PATH}) + echo "Original Diff: " + printf "%s\n" "$ORIGINAL_DIFF_CONTENT" + + # Use printf to avoid \n missing + printf "%s\n" "$PACKAGE_SIZE_INFO" > ${DIFF_FILE_PATH} + printf "%s\n" "$ORIGINAL_DIFF_CONTENT" >> ${DIFF_FILE_PATH} + + echo "Diff after merge: " + cat ${DIFF_FILE_PATH} + - name: Write PR number to file uses: actions/github-script@v7 with: @@ -291,4 +365,4 @@ jobs: with: name: interface-diff.txt path: | - ./engine/interface-diff.txt + ./engine-HEAD/interface-diff.txt diff --git a/cocos/2d/components/label.ts b/cocos/2d/components/label.ts index 22af966add..02016ee66a 100644 --- a/cocos/2d/components/label.ts +++ b/cocos/2d/components/label.ts @@ -496,14 +496,15 @@ export class Label extends UIRenderer { return this._cacheMode; } set cacheMode (value) { - if (this._cacheMode === value) { + const oldCacheMode = this._cacheMode; + if (oldCacheMode === value) { return; } - if (this._cacheMode === CacheMode.BITMAP && !(this._font instanceof BitmapFont) && this._ttfSpriteFrame) { + if (oldCacheMode === CacheMode.BITMAP && !(this._font instanceof BitmapFont) && this._ttfSpriteFrame) { this._ttfSpriteFrame._resetDynamicAtlasFrame(); } - if (this._cacheMode === CacheMode.CHAR) { + if (oldCacheMode === CacheMode.CHAR) { this._ttfSpriteFrame = null; this.destroyLetterTexture(); }