gzip assets

This commit is contained in:
Alex Grintsvayg 2016-09-02 10:22:30 -04:00
parent 6b3ad6cfb3
commit 764ec67919
3 changed files with 23 additions and 3 deletions

1
.gitignore vendored
View file

@ -3,6 +3,7 @@
/data/config.php /data/config.php
/data/writeable/* /data/writeable/*
/web/css/* /web/css/*
/web/js/*.gz
/log /log
/web/zohoverify /web/zohoverify
nbproject nbproject

View file

@ -3,6 +3,9 @@
include __DIR__.'/bootstrap.php'; include __DIR__.'/bootstrap.php';
View::gzipAssets();
die;
$options = getopt('f'); $options = getopt('f');
$force = isset($options['f']); // update even if no NEEDS_UPDATE file exists $force = isset($options['f']); // update even if no NEEDS_UPDATE file exists

View file

@ -14,6 +14,11 @@ class View
{ {
const LAYOUT_PARAMS = '_layout_params'; const LAYOUT_PARAMS = '_layout_params';
const WEB_DIR = ROOT_DIR.'/web';
const SCSS_DIR = self::WEB_DIR.'/scss';
const CSS_DIR = self::WEB_DIR.'/css';
const JS_DIR = self::WEB_DIR.'/js';
public static function render($template, array $vars = []) public static function render($template, array $vars = [])
{ {
if (!static::exists($template) || substr_count($template, '/') !== 1) if (!static::exists($template) || substr_count($template, '/') !== 1)
@ -121,7 +126,7 @@ class View
{ {
$scssCompiler = new \Leafo\ScssPhp\Compiler(); $scssCompiler = new \Leafo\ScssPhp\Compiler();
$scssCompiler->setImportPaths([ROOT_DIR.'/web/scss']); $scssCompiler->setImportPaths([self::SCSS_DIR]);
$compress = true; $compress = true;
if ($compress) if ($compress)
@ -134,8 +139,19 @@ class View
$scssCompiler->setLineNumberStyle(Leafo\ScssPhp\Compiler::LINE_COMMENTS); $scssCompiler->setLineNumberStyle(Leafo\ScssPhp\Compiler::LINE_COMMENTS);
} }
$css = $scssCompiler->compile(file_get_contents(ROOT_DIR.'/web/scss/all.scss')); $css = $scssCompiler->compile(file_get_contents(self::SCSS_DIR.'/all.scss'));
file_put_contents(ROOT_DIR.'/web/css/all.css', $css); file_put_contents(self::CSS_DIR.'/all.css', $css);
}
public static function gzipAssets()
{
foreach([self::CSS_DIR => 'css', self::JS_DIR => 'js'] as $dir => $ext)
{
foreach(glob("$dir/*.$ext") as $file)
{
file_put_contents($file.'.gz', gzcompress(file_get_contents($file)));
}
}
} }
protected static function interpolateTokens($html) protected static function interpolateTokens($html)