Web Application Developer

Minify – compress your files on the fly!

Are you, like me, one of those who keeps JavaScript and CSS in multiple files for better structure and who don’t compress the code because you want to have the readability intact in your development environment? Well, then this script is for you!

I give you Minify, a script which combines and compress your files on the fly. This way the files get smaller and the client only have to make one request to the server. Just include the class in the top of the page, somewhere before you include your CSS/JavaScript, give it a few parameters and it will do everything for you. Now you can have how many files you want and edit whenever you want as long as you include the CSS/JavaScript file generated by Minify.

But, won’t it slow down the execution of the page to run Minify every time? No! First of all, you’ll barely notice when the script actually runs because it doesn’t take any time at all. And second, it won’t run the script if there is no change to the source files. The script is smart enough to store the hash of every file in a cache file and will just check against it.

I made the function which Minify is based on a few months ago when I tried to figure out a way to compress my files automatically and I’ve used it for two or three projects. I realized that there should be an easier way to integrate the script into an existing code and that’s why I made Minify. The original script used PHP’s function filemtime() to check if a file had been modified since the minified version was generated. It work decently, but if you copy/paste an old file from another project it will still have the same modified time and therefore will the minified version be newer. This and a lot of other stuff were fixed when I changed to the existing system, which is faster too!

So, what’s next for Minify? Well, first of all I want to fix all the issues listed below and more optimization. A nice thing would be native JavaScript and CSS compression in Minify.class.php to keep it in one file. Can you think of something else to add? Suggest it!

Please comment below what you think about the script. Don’t be afraid to report issues, improvements, feedback etc. below and remeber to tell your friends! :)

Download Minify r9 now!

Information

The JSMin Class is based on code by Ryan Grove and CSSMin Class is based on code by Corey Hart. Current version of Minify (r9) was released August 2nd, 2010.

Arguments

type     = type: enum(css,js) - required: yes - default: n/a
dir      = type: string       - required: yes - default: n/a
file     = type: string       - required: yes - default: n/a
filter   = type: array        - required: no  - default: array()
priority = type: array        - required: no  - default: array()
debug    = type: boolean      - required: no  - default: false

Example Usage

require 'Minify.class.php';
new Minify('css',
           'css/',
           'styles.min.css',
           array('fixIE.css', 'debug.css'),
           array('reset.css'),
           true);

Known Issues

  • Minify will crash if it lacks the right permissions and/or the directory is not writable (fixed?).

These issues will most likely be fixed in upcoming releases of Minify. Stay tuned!

Change Log

  • r9:
    • Updated CSSMin to latest trunk (7c4b536f1449eb7be3ca).
    • Added the current version of Minify to the debug output.
    • Minor code cleanup.
  • r8:
    • Updated CSSMin to latest trunk (1ebdcc1ed7569ee7751bf17).
  • r7:
    • Changed caching method again, this time to the SFV format (CRC32).
    • Remade the debug output. If the script doesn’t work it’ll be easier to find the cause.
  • r6:
    • Fixed an issue where the script would include every file twice and ignore the priority array.
  • r5:
    • Changed caching method for a faster and smaller cache. If problem occurs with this new release try to delete your old cache files.
    • Fixed a notice in CSSMin Class triggered by empty attributes in your stylesheet.
  • r4:
    • Lots of code optimization.
    • Fixed the issue with files being compressed in alphabetical order and break thing. The new argument priority minifies the files in it before the rest. Perfect way to compress the jquery library before your jquery calls!
    • Fixed a bug where the cache would become empty and crash the script everytime.
  • r3:
    • Public release!
    • The minified file (specified in the file argument) will now automatically be included in the filter argument to stop an evil loop which will minify the files every page load instead of using the cache.
    • The name of the class is now Minify instead of minify.
    • Made a few changes to the example usage.
    • Renamed the cache files from .md4 to .cache for better understanding of their functionality.
    • The cache is now checked against a hash in raw binary data instead of a hash in lowercase hexits for faster execution (like ~200 microseconds!).
    • Added a “Known Issues” section with all issues and their possible workarounds.
    • Spelling and grammar fixed is various locations.
    • Moved JSMin.class.php and CSSMin.class.php to the folder minify for better structure, will hopefully be integrated in the main file in a future release.
    • Cleaned up some whitespaces – everything for a smaller file! :)
  • r2:
    • Deleted the folder “var”. The files are now included in the CSSMin class.
    • More comments in the code.
    • Added a bunch of options for the css compressor in CSSMin.class.php.
    • The CSSMin class shouldn’t fill the page with notices (temp fix!).
    • Example usage code.
  • r1:
    • First test code produced.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Slashdot
  • Twitter
blog comments powered by Disqus

Twitter

  • I'll dream about bash script tonight :( 16 hours ago
  • Just finished a game of L4D with the gang. Seriously, why did they make the sequel? :( 1 day ago
  • Debugging a ~7 month old script without comments is awesome, still remember big chunks of it though! :) 2 days ago
  • Bra Mercurial guide för repos på extern server (#wdk) - http://bit.ly/ajH9aw 5 days ago
  • Minecraft is probably the best indie game ever, love it! :D 6 days ago