function test_Minify_ClosureCompiler()
{
    global $thisDir;
    Minify_ClosureCompiler::$isDebug = true;
    // --- Test minification w/o setting the necessary settings ---
    try {
        Minify_ClosureCompiler::minify('');
    } catch (Exception $e) {
        $exc = $e;
    }
    $passed = assertTrue($exc instanceof Minify_ClosureCompiler_Exception, 'Minify_ClosureCompiler : Throws Minify_ClosureCompiler_Exception');
    if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
        echo "\n---Message: " . var_export($exc->getMessage(), 1) . "\n\n\n";
    }
    $compiler_jar_path = __DIR__ . DIRECTORY_SEPARATOR . 'compiler.jar';
    if (is_file($compiler_jar_path)) {
        // set minimum necessary settings
        Minify_ClosureCompiler::$jarFile = $compiler_jar_path;
        Minify_ClosureCompiler::$tempDir = sys_get_temp_dir();
        // --- Test minification with the minimum necessary settings ---
        $src = "\n    (function (window, undefined){\n        function addOne(input) {\n            return 1 + input;\n        }\n        window.addOne = addOne;\n        window.undefined = undefined;\n    })(window);\n        ";
        $minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
        $minOutput = Minify_ClosureCompiler::minify($src);
        $passed = assertTrue($minExpected == $minOutput, 'Minify_ClosureCompiler : minimum necessary settings');
        if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
            echo "\n---Output: " . countBytes($minOutput) . " bytes\n\n{$minOutput}\n\n";
            echo "---Expected: " . countBytes($minExpected) . " bytes\n\n{$minExpected}\n\n";
            echo "---Source: " . countBytes($src) . " bytes\n\n{$src}\n\n\n";
        }
        // --- Test minification with advanced compilation level ---
        $src = "function unused() {};";
        $minExpected = '';
        $minOutput = Minify_ClosureCompiler::minify($src, array(Minify_ClosureCompiler::OPTION_COMPILATION_LEVEL => 'ADVANCED_OPTIMIZATIONS'));
        $passed = assertTrue($minExpected == $minOutput, 'Minify_ClosureCompiler : advanced optimizations');
        if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
            echo "\n---Output: " . countBytes($minOutput) . " bytes\n\n{$minOutput}\n\n";
            echo "---Expected: " . countBytes($minExpected) . " bytes\n\n{$minExpected}\n\n";
            echo "---Source: " . countBytes($src) . " bytes\n\n{$src}\n\n\n";
        }
    } else {
        echo "      Minify_ClosureCompiler : To test more functionality, download a compiler.jar from\n";
        echo "                               https://code.google.com/p/closure-compiler/wiki/BinaryDownloads,\n";
        echo "                               put it under '{$compiler_jar_path}',\n";
        echo "                               and make it readable by your webserver\n";
    }
}
Example #2
0
$pathToRoot = dirname(dirname(dirname(dirname(__FILE__))));
$baseVersionFile = $pathToAssets + "/script.ver";
//$pathToCss = $pathToAssets . '/stylesheets/sitewide.css';
$getScripts = "http://" . $_SERVER['HTTP_HOST'] . "/v5/servlet/getScripts";
$pathToCore = $pathToAssets . '/core';
if (!file_exists($pathToTmp)) {
    if (!mkdir($pathToTmp)) {
        die('unable to create tmp folder');
    }
}
// Set up Minify
require "{$pathToLib}/Minify/Loader.php";
Minify_Loader::register();
Minify_YUICompressor::$jarFile = $pathToYui;
Minify_YUICompressor::$tempDir = $pathToTmp;
Minify_ClosureCompiler::$jarFile = $pathToClosureCompiler;
Minify_ClosureCompiler::$tempDir = $pathToTmp;
Minify_ClosureStylesheets::$jarFile = $pathToClosureStylesheets;
Minify_ClosureStylesheets::$tempDir = $pathToTmp;
// Set up CSSTidy
define('ABSPATH', $pathToCssTidy);
require $pathToCssTidy . '/class.csstidy.php';
require $pathToCssTidy . '/lang.inc.php';
/*
$css->settings['optimise_shorthands'] = true;
$css->settings['template'] = 'highest';
//$css->settings['css_level'] = 'CSS3.1';
$css->settings['remove_bslash'] = true;
$css->settings['compress_colors'] = true;
$css->settings['compress_font-weight'] = true;
$css->settings['lowercase_s'] = true;
 /**
  * Initializes minifier
  *
  * @param string  $engine
  * @return void
  */
 function init($engine)
 {
     switch ($engine) {
         case 'yuijs':
             Minify_YUICompressor::$tempDir = Util_File::create_tmp_dir();
             Minify_YUICompressor::$javaExecutable = $this->_config->get_string('minify.yuijs.path.java');
             Minify_YUICompressor::$jarFile = $this->_config->get_string('minify.yuijs.path.jar');
             break;
         case 'yuicss':
             Minify_YUICompressor::$tempDir = Util_File::create_tmp_dir();
             Minify_YUICompressor::$javaExecutable = $this->_config->get_string('minify.yuicss.path.java');
             Minify_YUICompressor::$jarFile = $this->_config->get_string('minify.yuicss.path.jar');
             break;
         case 'ccjs':
             Minify_ClosureCompiler::$tempDir = Util_File::create_tmp_dir();
             Minify_ClosureCompiler::$javaExecutable = $this->_config->get_string('minify.ccjs.path.java');
             Minify_ClosureCompiler::$jarFile = $this->_config->get_string('minify.ccjs.path.jar');
             break;
     }
 }
 /**
  * Test minifier action
  *
  * @return void
  */
 function w3tc_test_minifier()
 {
     $engine = Util_Request::get_string('engine');
     $path_java = Util_Request::get_string('path_java');
     $path_jar = Util_Request::get_string('path_jar');
     $result = false;
     $error = '';
     if ($engine != 'googleccjs') {
         if (!$path_java) {
             $error = __('Empty JAVA executable path.', 'w3-total-cache');
         } elseif (!$path_jar) {
             $error = __('Empty JAR file path.', 'w3-total-cache');
         }
     }
     if (empty($error)) {
         switch ($engine) {
             case 'yuijs':
                 Minify_YUICompressor::$tempDir = Util_File::create_tmp_dir();
                 Minify_YUICompressor::$javaExecutable = $path_java;
                 Minify_YUICompressor::$jarFile = $path_jar;
                 $result = Minify_YUICompressor::testJs($error);
                 break;
             case 'yuicss':
                 Minify_YUICompressor::$tempDir = Util_File::create_tmp_dir();
                 Minify_YUICompressor::$javaExecutable = $path_java;
                 Minify_YUICompressor::$jarFile = $path_jar;
                 $result = Minify_YUICompressor::testCss($error);
                 break;
             case 'ccjs':
                 Minify_ClosureCompiler::$tempDir = Util_File::create_tmp_dir();
                 Minify_ClosureCompiler::$javaExecutable = $path_java;
                 Minify_ClosureCompiler::$jarFile = $path_jar;
                 $result = Minify_ClosureCompiler::test($error);
                 break;
             case 'googleccjs':
                 $result = Minify_JS_ClosureCompiler::test($error);
                 break;
             default:
                 $error = __('Invalid engine.', 'w3-total-cache');
                 break;
         }
     }
     $response = array('result' => $result, 'error' => $error);
     echo json_encode($response);
 }
Example #5
0
/**
 * Use Google Closure Compiler instead of JSMin
 * This will make the first uncached visit painfully slow.
 * Please *only* use this for development. Use regular cached files for deployment.
 */
function CompilerJS($js)
{
    require_once 'Minify/ClosureCompiler.php';
    Minify_ClosureCompiler::$jarFile = dirname(__FILE__) . '/lib/closure-compiler.jar';
    Minify_ClosureCompiler::$tempDir = '/tmp';
    return Minify_ClosureCompiler::minifyJs($js);
}