From f0c9e12bc136717b97b65c537b5270565cc4ca01 Mon Sep 17 00:00:00 2001 From: Sonny Le Date: Tue, 7 Nov 2023 10:17:04 +0700 Subject: [PATCH] Fixed issue minify excludes js files --- framework/library/astroid/Document.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/framework/library/astroid/Document.php b/framework/library/astroid/Document.php index a5924b69..cdb4dbf6 100644 --- a/framework/library/astroid/Document.php +++ b/framework/library/astroid/Document.php @@ -287,17 +287,30 @@ public function minifyJS($html) $base_path = str_replace($juri->getScheme() . '://' . $juri->getHost() . '/', '', $juri->root()); Framework::getDebugger()->log('Minifying JS'); + $excludes = Framework::getTemplate()->getParams()->get('minify_js_excludes', ''); $javascripts = []; $javascriptFiles = []; - $html = preg_replace_callback('/(]*src=")([^"]*)("[^>]*>)(.*)(<\/script>)|(' && !empty($matches[2])) { if (strpos($matches[0], 'type="module"') > 0 || strpos($matches[0], 'media/system/js/joomla-hidden-mail-es5.min.js') > 0 || strpos($matches[0], 'webcomponents-bundle.min.js') > 0 || strpos($matches[0], 'media/system/js/core') > 0 || strpos($matches[0], 'www.googletagmanager.com') > 0) { return $matches[0]; } - $script = ['content' => $this->beutifyURL($matches[2]), 'type' => 'url']; - $javascriptFiles[] = $this->beutifyURL($matches[2]); + + // Excludes scripts + $scriptName = $this->beutifyURL($matches[2]); + $file_path = strtok($scriptName, '?'); + if (substr($file_path, 0, strlen($base_path)) === $base_path) { + $file_path = preg_replace('/' . preg_quote($base_path, '/') . '/', '', $file_path, 1); + } + + $file = basename($file_path); + if (Helper::matchFilename($file, \explode(',', $excludes))) { + return $matches[0]; + } + $script = ['content' => $scriptName, 'type' => 'url']; + $javascriptFiles[] = $scriptName; } else if (isset($matches[8]) && $matches[8] == '' && !empty($matches[7])) { $script = ['content' => $matches[7], 'type' => 'script']; } else if (isset($matches[13]) && $matches[13] == '' && !empty($matches[10]) && ($matches[10] == 'text/javascript') && !empty($matches[12])) { @@ -319,7 +332,7 @@ public function minifyJS($html) Helper::putContents($jsFile, ''); foreach ($javascripts as $javascript) { - $excludes = Framework::getTemplate()->getParams()->get('minify_js_excludes', ''); + $minifier = new Minify\JS(); if ($javascript['type'] == 'url') { $file_path = strtok($javascript['content'], '?');