diff --git a/.github/workflows/optimize-svgs.yml b/.github/workflows/optimize-svgs.yml index c28ccdc31..c8dfc056e 100644 --- a/.github/workflows/optimize-svgs.yml +++ b/.github/workflows/optimize-svgs.yml @@ -21,20 +21,53 @@ jobs: - uses: actions/checkout@v4 with: ref: ${{github.head_ref}} - - uses: ericcornelissen/svgo-action@v4 - id: svgo + fetch-depth: 0 + + - name: Fetch base ref + if: github.event_name == 'pull_request' + run: git fetch origin ${{github.base_ref}} --depth=1 + + - uses: actions/setup-node@v4 with: - repo-token: ${{secrets.GITHUB_TOKEN}} - svgo-version: 3 + node-version: 20 + + - name: Run SVGO on all SVGs + if: github.event_name == 'push' + run: npx svgo@3 --config svgo.config.js -r assets/svg/ + + - name: Get changed SVG files + if: github.event_name == 'pull_request' + id: changed + run: | + git diff --name-only "origin/${{github.base_ref}}" -- '*.svg' > /tmp/changed_svgs.txt + echo "COUNT=$(wc -l < /tmp/changed_svgs.txt | tr -d ' ')" >> "$GITHUB_OUTPUT" + + - name: Run SVGO on changed SVGs + if: github.event_name == 'pull_request' && steps.changed.outputs.COUNT != '0' + run: xargs npx svgo@3 --config svgo.config.js < /tmp/changed_svgs.txt + + - name: Check for changes + id: svgo + run: | + CHANGED=$(git diff --name-only -- '*.svg' | wc -l | tr -d ' ') + if [ "$CHANGED" -gt 0 ]; then + echo "DID_OPTIMIZE=true" >> "$GITHUB_OUTPUT" + echo "OPTIMIZED_COUNT=$CHANGED" >> "$GITHUB_OUTPUT" + else + echo "DID_OPTIMIZE=false" >> "$GITHUB_OUTPUT" + echo "OPTIMIZED_COUNT=0" >> "$GITHUB_OUTPUT" + fi + - name: Commit optimizations uses: stefanzweifel/git-auto-commit-action@v5 - if: ${{steps.svgo.outputs.DID_OPTIMIZE}} + if: steps.svgo.outputs.DID_OPTIMIZE == 'true' with: commit_message: Optimize ${{steps.svgo.outputs.OPTIMIZED_COUNT}} SVG(s) file_pattern: '*.svg' + - name: Comment on Pull Request uses: thollander/actions-comment-pull-request@v2 - if: ${{steps.svgo.outputs.DID_OPTIMIZE && github.event_name == 'pull_request'}} + if: steps.svgo.outputs.DID_OPTIMIZE == 'true' && github.event_name == 'pull_request' with: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - message: Optimized ${{steps.svgo.outputs.OPTIMIZED_COUNT}} SVG(s) \ No newline at end of file + message: Optimized ${{steps.svgo.outputs.OPTIMIZED_COUNT}} SVG(s) diff --git a/assets/svg/1f004.svg b/assets/svg/1f004.svg index f4fa5844c..3c621ad30 100644 --- a/assets/svg/1f004.svg +++ b/assets/svg/1f004.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/svgo.config.js b/svgo.config.js index 3224d9cf1..d3f19d766 100644 --- a/svgo.config.js +++ b/svgo.config.js @@ -11,6 +11,9 @@ module.exports = { inlineStyles: { onlyMatchedOnce: false, }, + removeDesc: { + removeAny: true, + }, removeViewBox: false, removeUselessStrokeAndFill : { removeNone: true, @@ -19,6 +22,7 @@ module.exports = { }, }, 'convertStyleToAttrs', + 'sortAttrs', 'removeDimensions', 'removeRasterImages', 'removeScriptElement',