Last active
March 29, 2021 01:00
-
-
Save ericmagnuson/3846371 to your computer and use it in GitHub Desktop.
Compiling and serving LESS on-the-fly with PHP & Apache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<IfModule mod_rewrite.c> | |
RewriteEngine on | |
RewriteRule (.*\.min.css) less.php?min=yes&path=$1 | |
RewriteRule (.*\.css) less.php?min=no&path=$1 | |
</IfModule> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Path to your lessc executable. This can be installed | |
// easily using npm. Just run `npm install less`. See | |
// the LESS site for more info: http://lesscss.org | |
$path_to_lessc = exec('which lessc'); | |
// Enable minified output for file requests ending | |
// in '.min.css'. If disabled, files ending in '.min.css' | |
// will still compile – just not as a minified version | |
$enable_minified = TRUE; | |
////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////// | |
$seconds_to_cache = 3600; | |
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT"; | |
$output = $exit_status = NULL; | |
$css = $_GET['path']; | |
$min = $_GET['min']; | |
$less = str_replace(".css", ".less", $css); | |
$less = str_replace(".min", "", $less); | |
if (file_exists($less)) | |
{ | |
header("Expires: $ts"); | |
header("Pragma: cache"); | |
header("Cache-Control: max-age=$seconds_to_cache"); | |
header("Content-type: text/css"); | |
// Minify if requested | |
if ($enable_minified && ($min == 'yes')) | |
$less_command = $path_to_lessc . " -x " . realpath($less) . ' 2>&1'; | |
else | |
$less_command = $path_to_lessc . " " . realpath($less) . ' 2>&1'; | |
// Run the command and catch the output and the error code | |
exec($less_command, $output_lines, $exit_status); | |
$was_successful = ($exit_status == 0) ? TRUE : FALSE; | |
if ($was_successful) | |
{ | |
// Just output the CSS | |
foreach ($output_lines as $output_line) echo $output_line . "\n"; | |
} | |
else | |
{ | |
// Output the errors while stripping out any command line color codes | |
$output_lines = preg_replace('/\[[0-9][0-9]?m/', '', $output_lines); | |
foreach ($output_lines as $output_line) echo $output_line . "\n"; | |
} | |
} | |
else if (file_exists($css)) | |
{ | |
header("Expires: $ts"); | |
header("Pragma: cache"); | |
header("Cache-Control: max-age=$seconds_to_cache"); | |
header("Content-type: text/css"); | |
echo file_get_contents($css); | |
} | |
else | |
{ | |
header("HTTP/1.0 404 Not Found"); | |
echo "Not Found"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note:
If using MAMP, there are a few problems executing
lessc
through PHP. You may need to do the following:
/Applications/MAMP/Library/bin/
, comment out the twolines
(DYLD_LIBRARY_PATH)
, and addPATH=${PATH}:/usr/local/bin
to the end.