Skip to content

Commit

Permalink
Updated paths and printed js warnings to screen
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Barnes <eric@ericlbarnes.com>
  • Loading branch information
ericlbarnes committed Mar 5, 2012
1 parent a809ce7 commit e732dda
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cli/Css.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Css {
*/
public static function compress($files, $output)
{
$css = require 'config/css.php';
$css = require BASE.'config/css.php';
$written = 0;
foreach ($files as $file)
{
Expand All @@ -33,7 +33,7 @@ public static function compress($files, $output)
*/
protected static function minify($data, $filters, $plugins)
{
require_once('cli/lib/cssmin-v3.0.1.php');
require_once(BASE.'cli/lib/cssmin-v3.0.1.php');
return \CssMin::minify(file_get_contents($data), $filters, $plugins);
}
}
46 changes: 37 additions & 9 deletions cli/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Js {
*/
public static function compress($files, $output)
{
$js = require 'config/js.php';
$js = require BASE.'config/js.php';
$written = 0;
foreach ($files as $file)
{
Expand All @@ -21,7 +21,20 @@ public static function compress($files, $output)
}

File::make(rtrim($output, '/'));
$written .= File::write(rtrim($output, '/').'/'.basename($file), static::curl($file, $js));
$compiled = static::request($file, $js);
$written .= File::write(rtrim($output, '/').'/'.basename($file), $compiled['compiled_code']);
if ($compiled['warnings'])
{
$warnings = json_decode($compiled['warnings']);
foreach ($warnings as $warning)
{
print 'You have some javascript issues:'."\n";
foreach ($warning as $error)
{
print $error->type.' - '.$error->warning.' - Line #'.$error->lineno."\n";
}
}
}
}
return $written;
}
Expand All @@ -32,24 +45,39 @@ public static function compress($files, $output)
* @param string $data
* @return mixed
*/
protected static function curl($data, $js)
protected static function request($data, $js)
{
// REST API arguments
$api_args = array(
$warning_args = array(
'compilation_level' => $js['compilation_level'],
'output_format' => 'json',
'output_info' => 'warnings',
'warning_level' => 'VERBOSE'
);

$compiled_args = array(
'compilation_level' => $js['compilation_level'],
'output_format' => 'text',
'output_info' => 'compiled_code'
'output_info' => 'compiled_code',
'warning_level' => 'VERBOSE'
);

$args = 'js_code=' . urlencode(file_get_contents($data));
$args .= http_build_query($api_args);
return array(
'compiled_code' => static::curl($data, $compiled_args),
'warnings' => static::curl($data, $warning_args),
);
}

protected static function curl($data, $args)
{
$curl_args = 'js_code='.urlencode(file_get_contents($data)).'&';
$curl_args .= http_build_query($args);

// API call using cURL
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://closure-compiler.appspot.com/compile',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $args,
CURLOPT_POSTFIELDS => $curl_args,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 0
Expand Down
6 changes: 3 additions & 3 deletions cli/minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ public static function run()
die('The -o ouput directory is required');
}

require_once('cli/File.php');
require_once(BASE.'cli/File.php');

$files = explode(' ', $opts['f']);

// Do the good stuff
if ($type == 'js')
{
static::__('Starting compressing js files');
require_once('cli/js.php');
require_once(BASE.'cli/js.php');
$compressed = Js::compress($files, $opts['o']);
}
else
{
static::__('Starting compressing css files');
require_once('cli/css.php');
require_once(BASE.'cli/css.php');
$compressed = Css::compress($files, $opts['o']);
}

Expand Down
1 change: 1 addition & 0 deletions minify
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
ini_set('log_errors', 0);
ini_set('html_errors', 0);
define('BASE', __DIR__.'/');

// --------------------------------------------------------------
// Launch the CLI.
Expand Down
7 changes: 4 additions & 3 deletions tests/js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function hello(name) {
function add_one(argument) {
return argument+'_one';
}
alert(hello('John Doe'));
alert(hello('John Doe'))

(function( $ ){

Expand All @@ -16,7 +16,8 @@ alert(hello('John Doe'));
max = Math.max( max, $(this).height() );
});

return max;
return maxed;
};
})( jQuery );
$('div').maxHeight();
$('div').maxHeight();
alerts('test');

0 comments on commit e732dda

Please sign in to comment.