Update js minifier to be more robust, with better error handling

This commit is contained in:
Timothy Warren 2015-11-13 11:32:12 -05:00
parent aee2fa7120
commit 1891aafef5

View File

@ -11,13 +11,18 @@
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
//Get config files //Get config files
require('../app/config/minify_config.php'); require_once('../app/config/minify_config.php');
//Include the js groups //Include the js groups
$groups_file = "../app/config/minify_js_groups.php"; $groups_file = "../app/config/minify_js_groups.php";
$groups = require($groups_file); $groups = require_once($groups_file);
// Include guzzle
require_once('../vendor/autoload.php');
//The name of this file //The name of this file
$this_file = __FILE__; $this_file = __FILE__;
@ -58,30 +63,47 @@ function get_files()
function google_min($new_file) function google_min($new_file)
{ {
$options = [ $options = [
'output_info' => 'compiled_code', 'output_info' => 'errors',
'output_format' => 'json', 'output_format' => 'json',
'compilation_level' => 'SIMPLE_OPTIMIZATIONS', 'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
'js_code' => urlencode($new_file), 'js_code' => $new_file,
'warning_level' => 'QUIET', 'language' => 'ECMASCRIPT5',
'language' => 'ECMASCRIPT5' 'language_out' => 'ECMASCRIPT5_STRICT'
]; ];
$option_pairs = []; // First check for errors
foreach($options as $key => $value) $error_client = new Client();
$error_res = $error_client->post('http://closure-compiler.appspot.com/compile', [
'headers' => [
'Accept-Encoding' => 'gzip',
"Content-type" => "application/x-www-form-urlencoded"
],
'form_params' => $options
]);
$error_json = $error_res->getBody();
$error_obj = json_decode($error_json);
if ( ! empty($error_obj->errors))
{ {
$option_pairs[] = "{$key}={$value}"; ?><pre><?= json_encode($err_obj, JSON_PRETTY_PRINT) ?></pre><?php
die();
} }
$option_string = implode("&", $option_pairs);
//Get a much-minified version from Google's closure compiler // Now actually retrieve the compiled code
$ch = curl_init('http://closure-compiler.appspot.com/compile'); $options['output_info'] = 'compiled_code';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $client = new Client();
curl_setopt($ch, CURLOPT_POST, 1); $res = $client->post('http://closure-compiler.appspot.com/compile', [
curl_setopt($ch, CURLOPT_POSTFIELDS, $option_string); 'headers' => [
$json = curl_exec($ch); 'Accept-Encoding' => 'gzip',
curl_close($ch); "Content-type" => "application/x-www-form-urlencoded"
],
'form_params' => $options
]);
$json = $res->getBody();
$obj = json_decode($json); $obj = json_decode($json);
return $obj->compiledCode; return $obj->compiledCode;
} }