Skip to content

Latest commit

 

History

History
executable file
·
66 lines (44 loc) · 2.29 KB

README.md

File metadata and controls

executable file
·
66 lines (44 loc) · 2.29 KB

JsCompile

Javascript compiler

Description

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.

Requirements & Installation

  1. Download or clone this repository to your Kohana modules directory.
  2. Enable the module in your 'bootstrap.php' file.
  3. Compile scripts directly into a template variable or a view.

Example View

application/views/example.php

<html>
<head>
	<?=$scripts?>
</head>
...

Example Controller

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'
		))
	));
}

Integration with head.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>

Thanks to

  • 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.