Example #1
0
<?php

include "../uglifyjs.php";
$compiler = new UglifyJS();
$compiler->cacheDir("./tmp/")->add("./src/jquery.js")->add("./src/underscore.js")->add("./src/backbone.js")->write(true);
Example #2
0
 function compileJS($scripts)
 {
     // make this a config option?
     $baseUrl = "assets/js/";
     $http = new Http();
     $http->setMethod('GET');
     // sort results
     //ksort_recursive( $minify );
     // record signature
     //$md5 = "";
     $cache_path = $this->_getCacheDir() . "{$baseUrl}";
     // FIX: create the dir if not available
     if (!is_dir($cache_path)) {
         mkdir($cache_path, 0775, true);
     }
     // process each group
     foreach ($scripts as $name => $group) {
         $first = current($group);
         $result = "";
         $timestamp = 0;
         // go to next group if minify flag is not true
         if (!$first["data"]['minify']) {
             continue;
         }
         $min = new UglifyJS();
         $min->cacheDir($cache_path);
         $min->compiler($GLOBALS['config']['admin']['uglify_service']);
         // get the encoding from the first member of the group
         $encode = $first["data"]["encode"];
         // loop through the group and add the files
         foreach ($group as $script) {
             // the move the domain from the script (if available)
             // check if it's a local url
             $href = $script["src"];
             $local = substr($href, 0, 4) !== "http" || substr($href, 0, 2) !== "//";
             if ($local) {
                 $href = url($href);
             }
             $result .= $http->execute($href);
             // get modified time
             $mtime = urlmtime($href);
             // compare with newest file in the group
             if ($timestamp < $mtime) {
                 $timestamp = $mtime;
             }
             //$src = str_replace( array(url(), cdn() ),"", $script["src"] );
             // remove leading slash
             //$src = ltrim($src,'/');
             //$md5 .= md5_file($file);
         }
         // compress signatures of all files
         $md5 = md5($result);
         // set timestamp
         $min->timestamp($timestamp);
         //contents of each group are saved in a tmp file
         $tmp_file = $cache_path . "tmp.{$md5}.js";
         file_put_contents($tmp_file, $result);
         // add tmp file
         $min->add($tmp_file);
         //$min->setFile( "$name.$md5.min" );
         $min->setFile("{$name}");
         if (!DEBUG) {
             $min->quiet()->hideDebugInfo();
         }
         // condition the method of minification here...
         switch ($encode) {
             case "whitespace":
                 $min->whitespaceOnly();
                 break;
             case "simple":
                 $min->simpleMode();
                 break;
             case "advanced":
                 $min->advancedMode();
                 break;
             default:
                 $min->simpleMode();
                 break;
         }
         $min->write();
         $min->clear();
         // add the signature in the group
         //$scripts[$name][]["data"]["md5"] = $md5;
     }
     return $scripts;
 }