public
Last active

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 .jar files in the same directory as the script when you run it. This is a Windows specific script, so if you run it under Linux make sure you use forward slashes instead of backslashes in your file names.

  • Download Gist
build-site.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?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 );
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.