Shrink the size of your javascript with js min (the php way)
Friday, December 22nd, 2006Getting more and more into javascript coding, two tools I found I could not do without are JSMin and JSlint, from Douglas Crockford.
On the JSmin webpage, a php version is available for download, but it did not fit my needs really well (in fact, the js version did, but, being a php-head, I got that one first), so I patched it a bit:
- made all the code work with PHP 4 (removed usage of splfileiterator, exceptions are replaced by triggering errors, class constants are turned into global constants, etc…)
- constant VERSION changed to JSMIN_VERSION to avoid name clashes
- added a new function to class JSMin to cleanly separate writing to output stream from parsing
- make sure that newline chars passed inside the comments parameter do not break output
- the file can now be included in a php app (whether cli or web hosted) and used as library (just define JSMIN_AS_LIB before including jsmin.php)
- when used as library, the class can operate on php strings instead of files
- i could not resist the urge to remove excess whitespace here and there in the php source, too (phpmin-syndrome???)
The new code is available here. Take a look at the comments inside for more info.
There is some other (php) work done on jsmin from a guy named Ed Eliot there


