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"; } }
/** * Test minifier action * * @return void */ function action_test_minifier() { w3_require_once(W3TC_LIB_W3_DIR . '/Request.php'); $engine = W3_Request::get_string('engine'); $path_java = W3_Request::get_string('path_java'); $path_jar = W3_Request::get_string('path_jar'); $result = false; $error = ''; if (!$path_java) { $error = __('Empty JAVA executable path.', 'w3-total-cache'); } elseif (!$path_jar) { $error = __('Empty JAR file path.', 'w3-total-cache'); } else { switch ($engine) { case 'yuijs': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/YUICompressor.php'); Minify_YUICompressor::setPathJava($path_java); Minify_YUICompressor::setPathJar($path_jar); $result = Minify_YUICompressor::testJs($error); break; case 'yuicss': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/YUICompressor.php'); Minify_YUICompressor::setPathJava($path_java); Minify_YUICompressor::setPathJar($path_jar); $result = Minify_YUICompressor::testCss($error); break; case 'ccjs': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/ClosureCompiler.php'); Minify_ClosureCompiler::setPathJava($path_java); Minify_ClosureCompiler::setPathJar($path_jar); $result = Minify_ClosureCompiler::test($error); break; default: $error = __('Invalid engine.', 'w3-total-cache'); break; } } $response = array('result' => $result, 'error' => $error); echo json_encode($response); }
// //print_r($test); // list($before) = str_split($contents, $test[2][1]+1); // fetches all the text before the match // $line_number = strlen($before) - strlen(str_replace("\n", "", $before)) + 1; // $warnings[] = "Uncommented console output found building $core_name in: $filePath on line: $line_number"; //} //$core_files[$core_name] .= Minify_ClosureCompiler::minify(file_get_contents($filePath), array()); checkConsoleLog($contents, $warnings, "Console output found in: {$filePath} on line:"); } else { $errors[] = "Unable to find Javascript file for {$core_name}: {$filePath}"; } } if (!count($errors)) { // Strip console.xxx //$core_files[$core_name] = preg_replace('/(;|\s)*console\.(log|debug|info|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\s*\(.*?\)\s*;/m',';',$core_files[$core_name]); // Compile $core_files[$core_name] = Minify_ClosureCompiler::minify($core_files[$core_name], array()); //$core_files[$core_name] = Minify_YUICompressor::minifyJs($core_files[$core_name], array()); } } $change_count = 0; if (!count($errors)) { // Create Core path if needed: if (!file_exists($pathToCore)) { if (!@mkdir($pathToCore)) { $errors[] = "Unable to access or create core script path: {$pathToCore}"; } } // Create Core CSS and JS files if (!count($errors)) { foreach ($core_files as $core_name => $core) { $file = $pathToCore . "/{$core_name}";
public static function setPathJar($pathJar) { self::$_pathJar = $pathJar; }
/** * Initializes minifier * * @param string $engine * @return void */ function init($engine) { switch ($engine) { case 'js': w3_require_once(W3TC_LIB_MINIFY_DIR . '/JSMin.php'); break; case 'css': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/CSS.php'); break; case 'yuijs': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/YUICompressor.php'); Minify_YUICompressor::setPathJava($this->_config->get_string('minify.yuijs.path.java')); Minify_YUICompressor::setPathJar($this->_config->get_string('minify.yuijs.path.jar')); break; case 'yuicss': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/YUICompressor.php'); Minify_YUICompressor::setPathJava($this->_config->get_string('minify.yuicss.path.java')); Minify_YUICompressor::setPathJar($this->_config->get_string('minify.yuicss.path.jar')); break; case 'ccjs': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/ClosureCompiler.php'); Minify_ClosureCompiler::setPathJava($this->_config->get_string('minify.ccjs.path.java')); Minify_ClosureCompiler::setPathJar($this->_config->get_string('minify.ccjs.path.jar')); break; case 'csstidy': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/CSSTidy.php'); break; case 'html': case 'htmlxml': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/HTML.php'); break; case 'htmltidy': case 'htmltidyxml': w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify/HTMLTidy.php'); 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); }
/** * 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; } }
/** * 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); }