|
| 1 | +name: Minify JavaScript and Upload to Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "*" |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + minify-and-release: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v2 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Use Node.js |
| 20 | + uses: actions/setup-node@v2 |
| 21 | + with: |
| 22 | + node-version: "14" |
| 23 | + |
| 24 | + - name: Install dependencies |
| 25 | + run: npm install terser |
| 26 | + |
| 27 | + - name: Minify JavaScript |
| 28 | + run: | |
| 29 | + npx terser safe-template-parser.js \ |
| 30 | + --compress passes=2,dead_code=true,conditionals=true,evaluate=true,booleans=true,loops=true,unused=true,hoist_funs=false,keep_fargs=true,hoist_vars=false,if_return=true,join_vars=true,side_effects=true,warnings=false \ |
| 31 | + --mangle \ |
| 32 | + --output safe-template-parser.min.js |
| 33 | +
|
| 34 | + - name: Calculate file sizes |
| 35 | + id: sizes |
| 36 | + run: | |
| 37 | + ORIGINAL_SIZE=$(wc -c < safe-template-parser.js) |
| 38 | + MINIFIED_SIZE=$(wc -c < safe-template-parser.min.js) |
| 39 | + COMPRESSION_RATIO=$(LC_NUMERIC=C printf "%.2f" $(echo "scale=2; $MINIFIED_SIZE / $ORIGINAL_SIZE * 100" | bc)) |
| 40 | + echo "ORIGINAL_SIZE=$ORIGINAL_SIZE" >> $GITHUB_OUTPUT |
| 41 | + echo "MINIFIED_SIZE=$MINIFIED_SIZE" >> $GITHUB_OUTPUT |
| 42 | + echo "COMPRESSION_RATIO=$COMPRESSION_RATIO" >> $GITHUB_OUTPUT |
| 43 | +
|
| 44 | + - name: Get commit messages |
| 45 | + id: get_commits |
| 46 | + run: | |
| 47 | + PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "") |
| 48 | + if [ -z "$PREVIOUS_TAG" ]; then |
| 49 | + # If there's no previous tag, get all commit messages |
| 50 | + COMMIT_MESSAGES=$(git log --pretty=format:"- %s" --reverse) |
| 51 | + else |
| 52 | + # Get commit messages since the previous tag |
| 53 | + COMMIT_MESSAGES=$(git log ${PREVIOUS_TAG}..HEAD --pretty=format:"- %s" --reverse) |
| 54 | + fi |
| 55 | + echo "COMMIT_MESSAGES<<EOF" >> $GITHUB_OUTPUT |
| 56 | + echo "$COMMIT_MESSAGES" >> $GITHUB_OUTPUT |
| 57 | + echo "EOF" >> $GITHUB_OUTPUT |
| 58 | +
|
| 59 | + - name: Create Release |
| 60 | + id: create_release |
| 61 | + uses: actions/create-release@v1 |
| 62 | + env: |
| 63 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + with: |
| 65 | + tag_name: ${{ github.ref }} |
| 66 | + release_name: Release ${{ github.ref }} |
| 67 | + body: | |
| 68 | + Changes in this Release: |
| 69 | + ${{ steps.get_commits.outputs.COMMIT_MESSAGES }} |
| 70 | +
|
| 71 | + Minified version of safe-template-parser.js |
| 72 | +
|
| 73 | + Original size: ${{ steps.sizes.outputs.ORIGINAL_SIZE }} bytes |
| 74 | + Minified size: ${{ steps.sizes.outputs.MINIFIED_SIZE }} bytes |
| 75 | + Compression ratio: ${{ steps.sizes.outputs.COMPRESSION_RATIO }}% |
| 76 | + draft: false |
| 77 | + prerelease: false |
| 78 | + |
| 79 | + - name: Upload Release Asset |
| 80 | + uses: actions/upload-release-asset@v1 |
| 81 | + env: |
| 82 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + with: |
| 84 | + upload_url: ${{ steps.create_release.outputs.upload_url }} |
| 85 | + asset_path: ./safe-template-parser.min.js |
| 86 | + asset_name: safe-template-parser.min.js |
| 87 | + asset_content_type: application/javascript |
0 commit comments