From 1891aafef5c0f6ef5a5b70d6751cf3d7325589f4 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 13 Nov 2015 11:32:12 -0500 Subject: [PATCH] Update js minifier to be more robust, with better error handling --- public/js.php | 56 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/public/js.php b/public/js.php index 88fb22e8..787387dc 100644 --- a/public/js.php +++ b/public/js.php @@ -11,13 +11,18 @@ */ // -------------------------------------------------------------------------- +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\Request; //Get config files -require('../app/config/minify_config.php'); +require_once('../app/config/minify_config.php'); //Include the js groups $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 $this_file = __FILE__; @@ -58,30 +63,47 @@ function get_files() function google_min($new_file) { $options = [ - 'output_info' => 'compiled_code', + 'output_info' => 'errors', 'output_format' => 'json', 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', - 'js_code' => urlencode($new_file), - 'warning_level' => 'QUIET', - 'language' => 'ECMASCRIPT5' + 'js_code' => $new_file, + 'language' => 'ECMASCRIPT5', + 'language_out' => 'ECMASCRIPT5_STRICT' ]; - $option_pairs = []; - foreach($options as $key => $value) + // First check for errors + $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}"; + ?>
post('http://closure-compiler.appspot.com/compile', [ + 'headers' => [ + 'Accept-Encoding' => 'gzip', + "Content-type" => "application/x-www-form-urlencoded" + ], + 'form_params' => $options + ]); + $json = $res->getBody(); $obj = json_decode($json); + return $obj->compiledCode; }