From 51c0fa2636865df308c967ca5a5d2e61ed006ef2 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 2 Sep 2014 19:46:53 +0200 Subject: [PATCH] Added JS+CSS+HTML compiler --- composer.json | 4 +- public_html/.gitignore | 1 + public_html/.htaccess | 3 + public_html/compile.php | 133 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 public_html/.gitignore create mode 100644 public_html/compile.php diff --git a/composer.json b/composer.json index f055dc53..025a45e3 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,7 @@ { "require": { - "mnapoli/php-di": "~4.0" + "mnapoli/php-di": "~4.0", + "linkorb/jsmin-php": "1.*", + "natxet/CssMin": "3.*" } } diff --git a/public_html/.gitignore b/public_html/.gitignore new file mode 100644 index 00000000..24320287 --- /dev/null +++ b/public_html/.gitignore @@ -0,0 +1 @@ +index-compiled.html diff --git a/public_html/.htaccess b/public_html/.htaccess index 169fbf32..550b38ba 100644 --- a/public_html/.htaccess +++ b/public_html/.htaccess @@ -1,3 +1,6 @@ +DirectoryIndex index-compiled.html +DirectoryIndex index.html + RewriteEngine On RewriteRule ^/?api/(.*) api-dispatch.php?q=$1 [L] diff --git a/public_html/compile.php b/public_html/compile.php new file mode 100644 index 00000000..eb86646a --- /dev/null +++ b/public_html/compile.php @@ -0,0 +1,133 @@ +)/ms', $html, -1, + PREG_SPLIT_DELIM_CAPTURE); + $buffer = ''; + foreach ($chunks as $chunk) + { + if (in_array($chunk, $illegalTags)) + continue; + + if (preg_match('/^<(' . join('|', $illegalTags) . ')/', $chunk)) + { + $buffer .= $chunk; + continue; + } + + # remove new lines & tabs + $chunk = preg_replace( '/[\\n\\r\\t]+/', ' ', $chunk); + # remove extra whitespace + $chunk = preg_replace( '/\\s{2,}/', ' ', $chunk); + # remove inter-tag whitespace + $chunk = preg_replace( '/>\\s<', $chunk); + # remove CSS & JS comments + $chunk = preg_replace( '/\\/\\*.*?\\*\\//i', '', $chunk); + $buffer .= $chunk; + } + return $buffer; + } +} + +class IndexBuilder +{ + public static function build() + { + $html = file_get_contents(__DIR__ . DS . 'index.html'); + self::includeTemplates($html); + self::minifyScripts($html); + self::minifyStylesheets($html); + return $html; + } + + private static function injectBody(&$html, $text) + { + $html = str_replace('', $text . '', $html); + } + + private static function injectHead(&$html, $text) + { + $html = str_replace('', $text . '', $html); + } + + private static function minifyScripts(&$html) + { + $scriptsToMinify = []; + + $html = preg_replace_callback( + '/]*src="([^"]+)"[^>]*><\/script>/', + function($matches) use (&$scriptsToMinify) + { + $scriptPath = $matches[1]; + if (substr($scriptPath, 0, 2) == '//' or strpos($scriptPath, 'http') !== false) + return $matches[0]; + $scriptsToMinify []= __DIR__ . DS . $scriptPath; + return ''; + }, $html); + + $out = ''; + self::injectBody($html, $out); + } + + private static function minifyStylesheets(&$html) + { + $html = preg_replace_callback( + '/]*href="([^"]+)"[^>]*>/', + function($matches) use (&$stylesToMinify) + { + $stylePath = $matches[1]; + if (substr($stylePath, 0, 2) == '//' or strpos($stylePath, 'http') !== false) + return $matches[0]; + if (strpos($matches[0], 'css') === false) + return $matches[0]; + $stylesToMinify []= __DIR__ . DS . $stylePath; + return ''; + }, $html); + + $out = ''; + self::injectHead($html, $out); + } + + private static function includeTemplates(&$html) + { + $templatesToInclude = []; + foreach (glob(__DIR__ . DS . 'templates' . DS . '*.tpl') as $templatePath) + $templatesToInclude []= $templatePath; + + $out = ''; + foreach ($templatesToInclude as $templatePath) + { + $out .= ''; + } + self::injectBody($html, $out); + } +} + +$compiledPath = __DIR__ . DS . 'index-compiled.html'; +$html = IndexBuilder::build(); +$html = Compressor::html($html); +file_put_contents($compiledPath, $html);