Skip to content

Instantly share code, notes, and snippets.

@dtalley
Last active January 17, 2023 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtalley/4528234 to your computer and use it in GitHub Desktop.
Save dtalley/4528234 to your computer and use it in GitHub Desktop.
Small PHP script to build a web application from a set of source files, combine it into as few files as possible (as defined by you), and minify/compress everything using a few useful tools. This particular script requires that you have PHP, Ruby, and Java installed, as well as the SASS gem, and you also need the YUICompressor and HTMLCompressor…
<?php
//You can pass -nm into the php script and no minification will occure (for debug purposes)
$minify = true;
foreach($argv as $arg)
{
if( $arg == "-nm" )
{
$minify = false;
}
}
echo "Compressing javascript files...\n";
//This combines example-1.js and example-2.js into 1 file and minifies it
compress_resources( "..\\output\\js\\final.js", "js", array(
"..\\js\\example-1.js",
"..\\js\\example-2.js"
);
echo "Compressing stylesheet files...\n";
//This runs both example-1.scss and example-2.scss through SASS, and then combines and minifies them
compress_resources( "..\\output\\css\\final.css", "css", array(
"..\\css\\example-1.scss",
"..\\css\\example-2.scss"
));
echo "Compressing markup files...\n";
//This combines example-1.html and example-2.html into 1 file and minifies it
//You wouldn't normally combine HTML files unless you're using them to hold
//templates, like the ones you'd use with Underscore templates or some other
//JavaScript template library.
compress_resources( "..\\output\\final.html", "html", array(
"..\\html\\example-1.html",
"..\\html\\example-2.html"
));
//You can run any of those on single files if you just want to minify/compress
//them instead of combining them. The processing function is below so you
//can poke around in it and see what's happening.
echo "All files successfully compressed...\n";
/*******************************************************************
*******************************************************************
*******************************************************************
*
* Utility functions that handle compression and minification.
*
* Shouldn't need to modify anything beyond this point.
*
*******************************************************************
*******************************************************************
*******************************************************************/
/*
* Process stylesheets using SASS.
*/
function process_stylesheet($file, $output)
{
exec( "sass " . $file . " " . $output );
}
/*
* Compress a given set of files according to their types, which
* means minifying them, combining them all into one file, and
* copying that file to the specified output file.
*/
function compress_resources($output, $type, $files)
{
global $minify;
$string = "";
foreach( $files as $file )
{
$delete = false;
if( $type == "css" && substr( $file, -4 ) == "scss" )
{
echo "Converting SASS stylesheet: " . $file . "...\n";
$converted = substr( $file, 0, strlen($file) - 4 ) . "css";
exec( "sass " . $file . " " . $converted );
if( file_exists( $converted ) )
{
$file = $converted;
$delete = true;
}
}
echo "Processing resource: " . $file . "...\n";
if( substr($file, 0 - strlen($type) - 4, strlen($type) + 4) == "min.$type" || !$minify )
{
$fp = fopen($file, "r");
if( $fp )
{
$body = fread($fp, filesize($file));
fclose($fp);
if( strlen($string) > 0 )
{
$string .= "\r\n";
}
$string .= $body;
}
}
else
{
if( $type == "css" || $type == "js" )
{
exec( "java -jar yuicompressor-2.4.8pre.jar -o tempfile.$type " . $file );
}
else if( $type == "html" )
{
exec( "java -jar htmlcompressor-1.5.3.jar -o tempfile.$type " . $file );
}
if( file_exists("tempfile.$type") )
{
$fp = fopen("tempfile.$type", "r");
if( $fp )
{
$body = fread($fp, filesize("tempfile.$type"));
fclose($fp);
unlink("tempfile.$type");
if( strlen($string) > 0 )
{
$string .= "\r\n";
}
$string .= $body;
}
}
}
if( $delete )
{
unlink($file);
}
}
if( $minify )
{
process_string($string);
}
$fp = fopen($output, "w");
if($fp)
{
fwrite($fp, $string, strlen($string));
fclose($fp);
}
}
/*
* Remove comments and unnecessary white space from a string
*/
function process_string(&$string)
{
$string = preg_replace( "!^\\s*//(.*?)\$!sim", "", $string );
$string = preg_replace( "/\\/\\*(.*?)\\*\\//si", "", $string );
$string = preg_replace( "/\n/si", "", $string );
$string = preg_replace( "/\r/si", "", $string );
$string = preg_replace( "/\r\n/si", "", $string );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment