Six utilities to minify and beautify

by Andy Prevost

Saturday May 18 2024

Have a total of six utilities now. Three to minify, and three to beautify. All unique builds written in PHP.

Minify:

  • Minify CSS
    • preserves "attribution" comments - those are comments that start with /*! and end with */
      typically these are destroyed by PHP minifiers because they look like regular comments, the only differentiation is the exclamation mark after the first asterisk
    • preserves all @import statements without any modification
    • preserves all url statement portions (example: when used with background: ) without any modification
     
  • Minify Javascript
    • preserves "attribution" comments - those are comments that start with /*! and end with */
      typically these are destroyed by PHP minifiers because they look like regular comments, the only differentiation is the exclamation mark after the first asterisk
  • Minify HTML
    • wonderful piece of code that reduces all HTML to one single line that executes properly.

While the main goal was to create minify utilities, I ended up writing beautifiers for all of the above. The main reason for the "beautifiers" isn't to output properly spaced and idented data, it was to "fix" unpaired tags (or code) and fix language errors. Language errors includes missing separators, missing end of line markers, pairing brackets, etc. Once the repairs were handled, then I could safely use a minifier. For beautifiers, I have a CSS Beautifier, a Javascript Beautifier and an HTML Beautifier. The HTML Beautifier was not simple at all to build ... all the PHP utilities and add-ons do not handle this well - Tidy doesn't work properly, the DOM doesn't work properly. The closest is the DOM, so I use that as a starting point and then fix the issues created by the DOM. This utility is a bit of a mish-mash, but it works.

I also will not release these. I am not prepared to handle licensing and support for these utilities and refuse to put them out there unlicensed.

Addendum: why minify? There are two reasons to minify. The first and most important is to reduce the overall size of the data stream between the web server and the user's browser. In some areas, the cost of internet bandwidth is quite prohibitive – anything we can do to minimize bloat is well worth it. The second is to obfuscate (at least partially).

 

◀ Previous Next ▶

Post a Comment