server side js/css concatenation  [ 729 views ]

Goal: combine js files on the server side

I like to work with independent files. One javascript object one file. This is easy, but to get these files is not.
Here is a simple server side script to create one js file from the many. For example the name is main.php.

1. for js files

header('Content-type: text/javascript; charset=utf-8'); 
$dir = '../js';
$arr = scandir($dir);
foreach($arr as $val) {
  if(strpos($val, '_') === 0){
    include($val);
  }
}

This file contains every js file (name starting with _ /underscore/ ) from the specified directory.
With this simple name trick I can filter easy my files from the output.

Be careful this is only a simple file concatenation!

Of course this method is usable for css files as well like this. Just we need to change the content type and the source directory.

1. for css files

header('Content-type: text/css; charset=utf-8'); 
$dir = '../css';
$arr = scandir($dir);
foreach($arr as $val) {
  if(strpos($val, '_') === 0){
    include($val);
  }
}
#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }