From 2413d9ca4c96412eb466b031b1fb5dde67102cd1 Mon Sep 17 00:00:00 2001 From: Andy Footner Date: Wed, 28 Aug 2024 11:31:20 +0200 Subject: [PATCH] fix: js optimizer errors Refs: IASC-799 --- PATCHES/core_js_optimizer.patch | 53 +++++++++++++++++++++++++++++++++ composer.patches.json | 1 + 2 files changed, 54 insertions(+) create mode 100644 PATCHES/core_js_optimizer.patch diff --git a/PATCHES/core_js_optimizer.patch b/PATCHES/core_js_optimizer.patch new file mode 100644 index 0000000..dc7eb58 --- /dev/null +++ b/PATCHES/core_js_optimizer.patch @@ -0,0 +1,53 @@ +diff --git a/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php b/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php +index 647a2c36a017f890c0e43bbdb350084989b1a7f1..3912afe5a9feeaae235c640cd89816091c7e97be 100644 +--- a/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php ++++ b/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php +@@ -155,24 +155,32 @@ public function deleteAll() { + public function optimizeGroup(array $group): string { + $data = ''; + $current_license = FALSE; +- foreach ($group['items'] as $js_asset) { +- // Ensure license information is available as a comment after +- // optimization. +- if ($js_asset['license'] !== $current_license) { +- $data .= "/* @license " . $js_asset['license']['name'] . " " . $js_asset['license']['url'] . " */\n"; +- } +- $current_license = $js_asset['license']; +- // Optimize this JS file, but only if it's not yet minified. +- if (isset($js_asset['minified']) && $js_asset['minified']) { +- $data .= file_get_contents($js_asset['data']); +- } +- else { +- $data .= $this->optimizer->optimize($js_asset); ++ ++ // No preprocessing, single JS asset: just use the existing URI. ++ if ($group['type'] === 'file' && !$group['preprocess']) { ++ $data = file_get_contents($group['items'][0]['data']); ++ } ++ else { ++ foreach ($group['items'] as $js_asset) { ++ // Ensure license information is available as a comment after ++ // optimization. ++ if ($js_asset['license'] !== $current_license) { ++ $data .= "/* @license " . $js_asset['license']['name'] . " " . $js_asset['license']['url'] . " */\n"; ++ } ++ $current_license = $js_asset['license']; ++ // Optimize this JS file, but only if it's not yet minified. ++ if (isset($js_asset['minified']) && $js_asset['minified']) { ++ $data .= file_get_contents($js_asset['data']); ++ } ++ else { ++ $data .= $this->optimizer->optimize($js_asset); ++ } ++ // Append a ';' and a newline after each JS file to prevent them from ++ // running together. ++ $data .= ";\n"; + } +- // Append a ';' and a newline after each JS file to prevent them from +- // running together. +- $data .= ";\n"; + } ++ + // Remove unwanted JS code that causes issues. + return $this->optimizer->clean($data); + } diff --git a/composer.patches.json b/composer.patches.json index 9ba59c8..ad5d18c 100644 --- a/composer.patches.json +++ b/composer.patches.json @@ -4,6 +4,7 @@ "Cron fails on amazon_ses when not configured": "PATCHES/amazon_ses-3417090-cron-queue-14.patch" }, "drupal/core": { + "Js optimize error https://www.drupal.org/project/drupal/issues/3416508": "PATCHES/core_js_optimizer.patch", "Views PHP 8.2 compatibility": "PATCHES/core-views-php-82-compatibility.patch" }, "drupal/stage_file_proxy": {