Skip to content

Commit

Permalink
update github action
Browse files Browse the repository at this point in the history
  • Loading branch information
6a67 committed Aug 28, 2024
1 parent 29ae3af commit 1dd5480
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 29 additions & 9 deletions .github/workflows/minify.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Minify JavaScript
name: Minify JavaScript, CSS, and HTML while Preserving Comments

on:
push:
Expand All @@ -8,7 +8,7 @@ on:
- 'script.user.js'

jobs:
minify:
minify-and-update:
runs-on: ubuntu-latest

steps:
Expand All @@ -22,21 +22,41 @@ jobs:
with:
node-version: '16'

- name: Install UglifyJS
run: npm install uglify-js -g
- name: Install dependencies
run: |
npm install uglify-js -g
npm install clean-css-cli -g
npm install html-minifier -g
sudo apt-get install -y sed
- name: Minify JavaScript, CSS, and HTML
run: |
# Extract userscript header
sed -n '/==UserScript==/,/==\/UserScript==/p' script.user.js > header.txt
# Minify JavaScript (excluding header)
sed '1,/==\/UserScript==/d' script.user.js | uglifyjs --compress --mangle --comments '/^!/' > script.min.js
# Minify CSS within the script
sed -i 's/`\(.*\)`/`'"$(echo '\\1' | cleancss -O2)"'`/g' script.min.js
# Minify HTML within the script
sed -i 's/`\(.*\)`/`'"$(echo '\\1' | html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true)"'`/g' script.min.js
# Combine header and minified script
cat header.txt script.min.js > script.final.js
- name: Minify JavaScript
- name: Update @updateURL
run: |
uglifyjs script.user.js -o script.min.js -c -m
sed -i 's|@updateURL.*|@updateURL https://raw.githubusercontent.com/${{ github.repository }}/minified/script.user.js|' script.final.js
- name: Create/Update minified branch
run: |
git config --global user.name 'GitHub Action'
git config --global user.email 'action@github.com'
git checkout --orphan minified
git rm -rf .
mv script.min.js script.user.js
mv script.final.js script.user.js
git add script.user.js
git commit -m "Update minified script"
git commit -m "Update minified script with preserved header, minified CSS/HTML, and new @updateURL"
git push origin minified --force
2 changes: 1 addition & 1 deletion script.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name JPDB Userscript (6a67)
// @namespace http://tampermonkey.net/
// @version 0.1.101
// @version 0.1.102
// @description Script for JPDB that adds some styling and functionality
// @match https://jpdb.io/*
// @grant GM_addStyle
Expand Down

0 comments on commit 1dd5480

Please sign in to comment.