JavaScript: Load minified library assets if available

This commit is contained in:
Johannes Meyer 2021-02-22 15:09:59 +01:00
parent 5c5e83a700
commit 64d1574972
2 changed files with 26 additions and 3 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Application\Libraries;
use CallbackFilterIterator;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\Json\JsonDecodeException;
use Icinga\Util\Json;
@ -202,10 +203,20 @@ class Library
$this->{$type . 'AssetPath'} = $dir;
return new RecursiveIteratorIterator(new RecursiveDirectoryIterator(
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(
$dir,
RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS
));
if ($type === 'static') {
return $iterator;
}
return new CallbackFilterIterator(
$iterator,
function ($path) use ($type) {
return substr($path, -5 - strlen($type)) !== ".min.$type";
}
);
};
$this->assets = [];

View File

@ -157,12 +157,24 @@ class JavaScript
// Library files need to be namespaced first before they can be included
foreach (Icinga::app()->getLibraries() as $library) {
foreach ($library->getJsAssets() as $file) {
$js .= self::optimizeDefine(
$alreadyMinified = false;
if ($minified && file_exists(($minFile = substr($file, 0, -3) . '.min.js'))) {
$alreadyMinified = true;
$file = $minFile;
}
$content = self::optimizeDefine(
file_get_contents($file),
$file,
$library->getJsAssetPath(),
$library->getName()
) . "\n\n\n";
);
if ($alreadyMinified) {
$out .= ';' . ltrim(trim($content), ';') . "\n";
} else {
$js .= $content . "\n\n\n";
}
}
}