Javascript compiler
- Module Version: 1.1.0
- Module URL: http://github.com/DanHulton/kohana-jscompile
- Compatible Kohana Version(s): 3.3.x
Reducing the number of javascript files in your project and keeping them as small as possible can have tremendous benefits with regards to page load speed. This module will compile and minify javascript files at runtime and cache the results so you don't have to worry about adding this step to your deploy and deploy testing.
Bonus benefit: You can split your javascript into multiple files for increased maintainability, AND keep your raw scripts out of your docroot.
- Download or clone this repository to your Kohana modules directory.
- Enable the module in your 'bootstrap.php' file.
- Compile scripts directly into a template variable or a view.
application/views/example.php
<html>
<head>
<?=$scripts?>
</head>
...
application/classes/controller/example.php
public function action_index() {
$this->request->response = View::factory('example', array(
'scripts' => JsCompile::compile(array(
APPPATH . 'js/jquery-autocomplete-plugin.js',
APPPATH . 'js/sample.js'
))
));
}
If you use head.js or another javascript loader that requires you pass the name of the javascript file instead of creating a link directly into the document, you can simply pass an extra variable to compile(), like so:
JsCompile::compile(array(
APPPATH . 'js/jquery-autocomplete-plugin.js',
APPPATH . 'js/sample.js'
), JsCompile::FORMAT_FILENAME);
Which returns:
/js/db554181f11f9189951cea98f3b4b697-1288017184.js
Instead of the tag:
<script src="/js/js-cache/db554181f11f9189951cea98f3b4b697-1288017184.js"></script>
- Zeelot One of my Kohana heroes, and from whom I shamelessly stole this README format.
- Douglas Crockford My big Javascript hero, and author of the original JsMin.
- Ryan Grove Author of the PHP conversion of Crockford's JsMin script that I use in the
vendor
directory. - Mon Geslani Author of Kohana LESS compiler module, from which I took the original inspiration.