From 64d15749720668f4d1d7a48ac603516acc95f5ba Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 22 Feb 2021 15:09:59 +0100 Subject: [PATCH] JavaScript: Load minified library assets if available --- library/Icinga/Application/Libraries/Library.php | 13 ++++++++++++- library/Icinga/Web/JavaScript.php | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/library/Icinga/Application/Libraries/Library.php b/library/Icinga/Application/Libraries/Library.php index b7024c8a5..b2e359b57 100644 --- a/library/Icinga/Application/Libraries/Library.php +++ b/library/Icinga/Application/Libraries/Library.php @@ -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 = []; diff --git a/library/Icinga/Web/JavaScript.php b/library/Icinga/Web/JavaScript.php index f0ad3c543..520aef93c 100644 --- a/library/Icinga/Web/JavaScript.php +++ b/library/Icinga/Web/JavaScript.php @@ -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"; + } } }