Compiling JS with Closure Compiler and PHP


/ Published in: PHP
Save to your folder(s)

ported to PHP5 from this tutorial: http://bohuco.net/blog/2010/09/use-google-closure-compiler-local-with-php/
since I had problems running the compiler on mutliple files, I did the joining myself.


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3. my scripts and the order of the scripts are stored in a simple XML file (static.scripts.xml):
  4. <group>
  5. <item>jquery.js</item>
  6. <item>jquery.easing.js</item>
  7. <item>scrollable.js</item>
  8. <item>scrollable.autoscroll.js</item>
  9. <item>pointbar.js</item>
  10. <item>jquery.actions.js</item>
  11. </group>
  12. **/
  13. class JSCompiler {
  14. public $scriptpath = '/js/';
  15. public $tmppath = '/js/cache/';
  16. public $tmpname;
  17. public $compiler = '/js/jscompiler/compiler.jar';
  18. public $output = '/js/scripts.min.js';
  19.  
  20. public function JSCompiler() {
  21. $this->tmpname = "joined-".time().".js";
  22. $this->output = $_SERVER["DOCUMENT_ROOT"].$this->output;
  23. $this->getScripts();
  24. }
  25.  
  26. /**
  27. * reads all Javascript files from XML, joins them and saves a temp file in /js/cache
  28. * @return void
  29. */
  30. public function getScripts() {
  31. $scripts = new DOMDocument();
  32. $tmp = file_get_contents('/static.scripts.xml');
  33. // load script names
  34. $scripts->loadXML($tmp);
  35. // get each files contents
  36. $xpath = new DOMXPath($scripts);
  37. $res = $xpath->query("//item");
  38. $jsString = '';
  39. foreach($res as $item) {
  40. $filename = $item->nodeValue;
  41. if (file_exists($_SERVER["DOCUMENT_ROOT"]."/js/$filename")) {
  42. $jsString .= file_get_contents($_SERVER["DOCUMENT_ROOT"]."/js/$filename"); // join them together
  43. } else {
  44. echo "file <samp>$filename</samp> not found!";
  45. }
  46. }
  47. if (!is_dir($_SERVER["DOCUMENT_ROOT"].$this->tmppath)) {
  48. echo $this->tmppath." doesn't exist";
  49. } else {
  50. file_put_contents($_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname, $jsString); // save temp. file
  51. }
  52. }
  53.  
  54. /**
  55. * fires the command to run Google Closure Compiler
  56. * @uses the temporary file created by getScipts()
  57. * @return string error message
  58. */
  59. public function compile($output = '', $level = '') {
  60. if ($output === '') $output = $this->output;
  61. if ($level === '') $level = 'ADVANCED_OPTIMIZATIONS';
  62. $compiler = $this->compiler;
  63. if (file_exists($_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname)) {
  64. $compilerCommand = "/usr/bin/java -jar $compiler --compilation_level $level --jscomp_dev_mode START --js " . $_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname . " --js_output_file $output 2>&1";
  65. exec($compilerCommand, $return, $code);
  66. if ($code != 0) {
  67. return sprintf("Fuck! Something went wrong: %s (%s)", join('<br />', $return), $code);
  68. } else {
  69. return "compiled successfully";
  70. }
  71. } else {
  72. return "tempfile not found.";
  73. }
  74. }
  75. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.