Welcome Guest, Not a member yet? Register   Sign In
Carabiner 1.0: Asset Management Library
#1

[eluser]tonydewan[/eluser]
NOTE: Carabiner has been updated to version 1.42. See the new thread for more info and to download.

Cara-wha?
Carabiner is a library for managing JavaScript and CSS assets. It's different from others that currently exist (for CodeIgniter, anyway) in that it acts differently depending on whether it is in a production or development environment. In a production environment, it will combine, minify, and cache assets. (As files are changed, new cache files will be generated.) In a development environment, it will simply include references to the original assets.


Requirements
Carabiner requires the JSMin and CSSMin libraries that I previously ported. They're both included in this release. You don't need to load them, unless you'll be using them elsewhere. Carabiner will load them automatically as needed. (Note: the only reason they're included as separate libraries is that it allows you to use them independently of Carabiner. If desired, you could probably include them in the carabiner.php file itself. You'd have to edit the Carabiner functions, but it could work.)


GZIP, Packer, etc.
Carabiner does not implement GZIP encoding, because I think that the web server should handle that. I think GZIPing is important, I just prefer not to do it PHP. If you need GZIP in an Asset Library, AssetLibPro does it. I've also chosen not to implement any kind of JavaScript obfuscation (like packer), primarily because of the client-side decompression overhead. More about this idea from John Resig. However, that's not to say you can't do it. You can easily provide a production version of a script that is packed. However, note that combining a packed script with minified scripts could cause problems. In that case, you can flag it to be not combined. (See usage below) Also, that's not to say that I won't port a PHP Version and include it eventually.


Inspiration/License
Carabiner is inspired by Minify, PHP Combine by Niels Leenheer and AssetLibPro by Vincent Esche, among other things. Carabiner is released under a BSD License.


Usage
Load the library as normal:
Code:
$this->load->library('carabiner');


Configure it like so:
Code:
$carabiner_config = array(
    'script_dir' => 'assets/scripts/',
    'style_dir'  => 'assets/styles/',
    'cache_dir'  => 'assets/cache/',
    'base_uri'   => $base,
    'combine'    => TRUE,
    'dev'        => FALSE
);
        
$this->carabiner->config($carabiner_config);


There are 8 relevant configuration options. The first 4 are required for Carabiner to function, the last 4 are not.

script_dir
STRING Path to the script directory. Relative to the CI front controller (index.php)

style_dir
STRING Path to the style directory. Relative to the CI front controller (index.php)

cache_dir
STRING Path to the cache directory. Must be writable. Relative to the CI front controller (index.php)

base_uri
STRING Base uri of the site, like 'http://www.example.com/'


dev
BOOLEAN Flags whether your in a development environment or not. See above for what this means. Defaults to FALSE.

combine
BOOLEAN Flags whether to combine files. Defaults to TRUE.

minify_js
BOOLEAN Flags whether to minify javascript. Defaults to TRUE.

minify_css
BOOLEAN Flags whether to minify CSS. Defaults to TRUE.



Add assets like so:
Code:
$this->carabiner->js('scripts.js');
        
$this->carabiner->css('reset.css');
        
$this->carabiner->css('admin/styles.css');


To set a (prebuilt) production version of an asset:
Code:
// pass a second string to the method with a path to the production version
$this->carabiner->css('wymeditor/wymeditor.js', 'wymeditor/wymeditor.pack.js' );


And to prevent a file from being combined:
Code:
// pass a boolean FALSE as the third attribute of the method
$this->carabiner->css('wymeditor/wymeditor.js', 'wymeditor/wymeditor.pack.js', FALSE );


You can also pass arrays (and arrays of arrays) to these methods. Like so:
Code:
// a single array
$this->carabiner->css( array('base.css', 'base.prod.css') );
        
// an array of arrays
$js_assets = array(
    array('dev/jquery.js', 'prod/jquery.js'),
    array('dev/jquery.ext.js', 'prod/jquery.ext.js'),
)

$this->carabiner->js( $js_assets );


To output your assets, including appropriate markup:
Code:
// display css
$this->carabiner->display('css');
    
//display js
$this->carabiner->display('js');


Finally, since Carabiner won't delete old cached files, you'll need to clear them out manually. To do so programatically:
Code:
// clear css cache
$this->carabiner->empty_cache('css');
        
//clear js cache
$this->carabiner->empty_cache('js');
        
// clear both
$this->carabiner->empty_cache();

Please let me know what you think! If there's something you would like changed or ad
#2

[eluser]tonydewan[/eluser]
I forgot to mention that a feature I would like to add is asset grouping. That is, allowing assets to be grouped (and then combined, minified, and output in those groups). It's not in this version because it's not something I've needed, but I'll get to it eventually. If you need/want that, let me know. I could move it up on the list if there was demand for it.
#3

[eluser]demogar[/eluser]
This looks great dude!
Is there any advantage over AssetLibPro?

Thank you in advance!
#4

[eluser]tonydewan[/eluser]
Well, that would probably be a matter of opinion. I looked at AssetLibPro before i built Carabiner, and there were a few things I didn't like about it. They are:

Use of a Config File
I think separate config file is a little overkill in this instance.

Unmodified CSS Tidy and JSMin
I rather dislike the idea of including tools in my CI application that aren't directly usable within it. Plus CSSTidy is big. I chose to port JSMin and a CSS minification class to CI directly, and use them the 'CI way' within Carabiner.

PHP Cached Files
AssetLibPro stores it's cached files as PHP files. I don't like the idea of PHP being interpreted for static assets. I also don't like how the file reference doesn't have a proper (js|css) extension. This is the only way to do some of the things it does (like GZIP and far-future headers), so I'm not saying it's wrong. I just don't like it.

Otherwise, the thing that I wanted most that AssetLibPro didn't provide was the ability to do different things based on the environment. Part of my standard CI install is an application-wide flag for development vs production environment. Carabiner is set up using that flag, so I never have to think about it. When I'm in development, the full scripts/stylesheets are in use, so I can easily debug, etc. Then, when I move things to production, assets are automatically combined/minified/cached. So, to answer your question directly:

The biggest advantage of Carabiner over AssetLibPro is the fact that it automatically reacts differently based on where you are in the development cycle.

All that being said, AssetLibPro is really nice. There is a lot I like about it, and there's a lot that it does that Carabiner does not (GZIP, Far-Future headers, asset grouping). There's a reason I listed it as inspiration!
#5

[eluser]demogar[/eluser]
You've convinced me, cause I also dislike the idea of PHP files for js and css.
I'll give a try to Carabiner, it looks really usefull.
I think I had a problem with AssetLibPro and CodeIgniter gzip.. is there any problem with Carabiner and CI gzip too?
#6

[eluser]tonydewan[/eluser]
Quote:I think I had a problem with AssetLibPro and CodeIgniter gzip.. is there any problem with Carabiner and CI gzip too?

Do you mean Output Compression? If so, I'm not sure whether there would be any problems. I would think not, but I can't say for sure.
#7

[eluser]demogar[/eluser]
I'll give a try tomorrow (it's 23:24 here) and I let you know Smile
#8

[eluser]Colin Williams[/eluser]
Wonderful stuff. Great addition to the framework, and definitely often requested
#9

[eluser]trice22[/eluser]
Thanks!
It's looking sweet. I'll give it a spin.
Any plans to include asset grouping in the future?

—trice
#10

[eluser]Phil Sturgeon[/eluser]
This is a pretty handy backend to the lovely syntax of my asset helper. I will be combining them and I get to keep my modular folder structure, but gain minification.




Theme © iAndrew 2016 - Forum software by © MyBB