From 57a295b544d713d45dd8d22477454e9195463f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 28 Sep 2017 14:10:25 +0200 Subject: [PATCH] html minification --- base.php | 1 + compile_html.php | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/base.php b/base.php index e31c847..30eeea4 100644 --- a/base.php +++ b/base.php @@ -35,6 +35,7 @@ if (!file_exists(__DIR__ . '/_env.php')) { define('JS_WEB_ROOT', $root); +define('ESP_PROD', (bool)getenv('ESP_PROD')); define('ESP_DEMO', (bool)getenv('ESP_DEMO')); if (ESP_DEMO) { define('DEMO_APS', << $p) { // making it not a very big improvement at the expense of ugly html. // $s = process_html($s); ob_clean(); - } // clean up - $of = $dest . $_k . ((in_array($_k, $no_tpl_files)||ESP_DEMO) ? '.html' : '.tpl'); - file_put_contents($of, $s); // write to a file + } + + $outputPath = $dest . $_k . ((in_array($_k, $no_tpl_files)||ESP_DEMO) ? '.html' : '.tpl'); + + if (file_exists($outputPath)) unlink($outputPath); + if (ESP_PROD) { + $tmpfile = tempnam('/tmp', 'espterm').'.html'; + file_put_contents($tmpfile, $s); + // using https://github.com/tdewolff/minify + system('minify --html-keep-default-attrvals '. + '-o '.escapeshellarg($outputPath).' '. + ''.escapeshellarg($tmpfile), $rv); + + // fallback if minify is not installed + if (!file_exists($outputPath)) file_put_contents($outputPath, $s); + } else { + file_put_contents($outputPath, $s); + } } ob_flush();