code-minifier.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- mode: sh; tab-width: 4; coding: utf-8 -*-
  2. # vim: ts=4 noet ai ft=sh
  3. # Minify various code
  4. # This file is part of JPM.sh.
  5. # Copyright (C) 2016 the Desktopd developers
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. # jpmMinifyHtml <dir>
  20. jpmMinifyHtml () {
  21. aDir=$1
  22. jpmConsoleLog "Minifying HTML files..."
  23. # Remove empty lines
  24. html_egrep_v='^\s*$|vim:'
  25. find "$aDir" -type f -name '*.html' -execdir \
  26. sh -c 'egrep -v "$2" "$1" > "$1.tmp" && mv -f "$1.tmp" "$1"' \
  27. -- {} "$html_egrep_v" \;
  28. }
  29. # jpmMinifyCss <dir>
  30. jpmMinifyCss () {
  31. aDir=$1
  32. # CSS minifier is aggressive.
  33. jpmConsoleLog "Minifying CSS files..."
  34. css_egrep_v='^\s*$|vim:'
  35. css_sed='s/\s\{1,\}/ /g ; s/\s*\([{:>+,;]\)\s*/\1/g ; s/;}/}/g'
  36. find "$aDir" -type f -name '*.css' -execdir \
  37. sh -c \
  38. 'egrep -v "$2" "$1" | tr -d "\t\n" | sed "$3" > "$1.tmp" && mv -f "$1.tmp" "$1"' \
  39. -- {} "$css_egrep_v" "$css_sed" \;
  40. }
  41. # jpmMinifyJs <dir>
  42. jpmMinifyJs () {
  43. aDir=$1
  44. # This new JS minifier tries to preserve line numbers.
  45. # It just removes some spaces and comments.
  46. jpmConsoleLog "Minifying JS files..."
  47. js_sed='s/\s*$//'
  48. find "$aDir" -type f -name '*.js' -execdir \
  49. sh -c \
  50. 'sed "$1" < "$2" > "$2.tmp" && mv -f "$2.tmp" "$2"' \
  51. -- "$js_sed" {} \;
  52. }