Introducting Minify. It’s just a simple wrapper around jsmin and cssmin to turn it into a Kohana Library. The library handles the minification, setting the correct headers and Gzipping the output for smaller files (based on the rules set out by Yahoo for faster sites).
To finish it off Minify caches output for a configurable time (defaults are 30s on developer sites and 12hrs on production sites) using the Kohana caching library.
Included in the module is a controller (content) which will handle loading JS and CSS files on the fly.
Configuration
Cfg is simple, there are 3 options (2 of which are variable on developer sites).
/* * Set your minify paths * these are relative to APPPATH (or, well, MODPATH/SYSPATH for that matter) */ $config['path_js'] = 'content/js'; $config['path_css'] = 'content/css'; /* * File expirey time * Recommend this is set HIGH (1 yr or more) in production sites * and super low (10s) for developments sites * Remember if you screw this up and set it too long you need to change the filename * because otherwise it wont refresh :) */ if(IN_PRODUCTION == TRUE) { // 1 yr $config['expires'] = 31536000; } else { // 10s $config['expires'] = 10; } /* * Time in seconds to cache the files * as these are fairly static files you can properly * set it quite high for IN_PRODUCTION sites * Whereas on development sites we want it fairly * low (and take the performance hit) * Remember 0 == Cache forever */ if(IN_PRODUCTION == TRUE) { // 12hrs $config['cache_lifetime'] = 43200; } else { // 30s $config['cache_lifetime'] = 30; }
Usage
The easiest usage is to just drop in and load the module in the normal way – then use the included content controller to load files from your application directory.
Calls to [site_url]/content/[file_path] will load the relevant file.
Note that you dont need to add the file extension (.css or .js) as the module will add them automatically.
If you want to output a file manually you can use:
$minify = new Minify($method); echo $minify->compress(implode('/',$arguments));
Get Minify
The package contains the library, css & js drivers, i18n GB & US file, the content controller and config file.
You can download the Zip file here.
Hi. I like the idea of this module, but I can’t for the life of me get it to work. I did have my js and css folders outside of the application. I’ve now moved them within to /application/assets/css etc.
I’ve also copied the example content controller and renamed it assets.
I’ve set the config paths to assets/css, assets/js
My HTML now reads href=”/assets/css/public.css” This gives me the error The page you requested, public.css.css, could not be found
if I remove the .css from the href I get ‘public.css.css, could not be found’
Could you please explain what the href URL should be with relation to the assets. Does it allow for sub directories? I know you’re using the Kohana::find_file method. Can’t quite work out what I;m doing wrong
cheers
heya!
Ok based on that information your href should be /assets/css/public (can you visit that URL directly in the browser and see what it says?)
Also, this might seem a silly question but are you sure you tested it with the .css removed correctly? Often I have bugs I cant get my head round when I find that I simply missed updating the file when I implemented the simple fix
If you dont mind sharing the url (either here or you can email it to me on tom@errant.me.uk) I can have a peek and see if anything occurs to me from the HTML side of things… what you have describes (*without* the .css in the URL) should work.
How use this with kohana template ?
how add Minify on :
Im not sure I quite follow you?
You just upload this to your application.
Set up the content paths in the configuration files and then upload your content – which will be accessible via the internet as usual…
Pingback: Top 10 Free Javascript Compressors « Wepage Designer Blog
Just a little helpful hint for people who don’t want to have to modify their existing css and js includes to point to the “content” controller. Simply keep your css and js files where they are and leave your css and js references the same in your code, except remove the “.js” or “.css” file extension. Then in your /application/config/routes.php file just add a route to map those requests to the “content” controller by adding a rule like this:
//minified CSS and JS files….
$config['/js/([._A-Za-z0-9\-\/]+)’] = ‘/content/js/$1′;
$config['/css/([._A-Za-z0-9\-\/]+)’] = ‘/content/css/$1′;
Hope this helps someone.
Thanks for the script, it’s fantastic.
Having a little problem using it with the page caching solution suggested here:
http://forum.kohanaphp.com/comments.php?DiscussionID=133
Basically, everything works fine when the page isn’t loaded into the cache (first run) but subsequent calls to the a page with minified javascript in it result in the javascript code becoming a jumble of garbage. I try opening the cahced page in my text editor and I am told its a binary file? If I turn off the caching hook, all works perfectly.
I believe its a problem with the page caching hook trying to cache an already cached item (the javascript file) but can’t work out how to stop this from happening.
Any help would be appreciated!
Awesome, thanks Errant
o que posso dizer, nao consegui fazer funcionar,
Hey Rich, sorry I don’t speak Portuguese (?). However Google translate is suggesting your having trouble getting it to work.
Any clues you can give me to help you fix this?
@MrMasonic: sorry to not respond for ages. Have you fixed the problem? Im guessing the caching is happening twice?? and thus messing it up.
The only suggestion I have is to try using the Event::clear(); function to remove the relevant caching code from the events queue inside the content controller.
Thanks Errant. I will try your suggestion and let you know.
In th emeantime I haven’t fixed the issue but I am now looking at the gzip compression that minify seems to do. It may have something to do with Kohana and minify both attempting to do this.
yes that is the problem – try updating to the latest release.
As you can see from http://bitbucket.org/errant/minify/issue/1/gzip-compression-clashes-with-kohana-inbuilt it is possibly all fixed now
(assuming that was your specific issue)
EDIT: sorry I read back and I realise that is irrelevant to your issue. Would you mind opening an new issue ticket on the bitbucket site with some more details – as well as a copy of your caching code,. I can try to help
Add new: http://bitbucket.org/errant/minify/issues/new/