CRM_Core_Region::add() - Allow JS resources to be flaged as ES5 modules (`esm=>TRUE`)
authorTim Otten <totten@civicrm.org>
Fri, 5 May 2023 05:36:08 +0000 (22:36 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 10 May 2023 21:22:49 +0000 (14:22 -0700)
CRM/Core/Region.php
CRM/Core/Resources/CollectionInterface.php
tests/phpunit/CRM/Core/RegionTest.php

index 4d9f733068c67003c28ad68cd6345be2b37c3d3e..08338cc42631a95294fc21d4f8c850fe05d2f87c 100644 (file)
@@ -88,7 +88,10 @@ class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Cor
           break;
 
         case 'scriptUrl':
-          if (!$allowCmsOverride || !$cms->addScriptUrl($snippet['scriptUrl'], $this->_name)) {
+          if (!empty($snippet['esm'])) {
+            $html .= sprintf("<script type=\"module\" src=\"%s\">\n</script>\n", $snippet['scriptUrl']);
+          }
+          elseif (!$allowCmsOverride || !$cms->addScriptUrl($snippet['scriptUrl'], $this->_name)) {
             $html .= sprintf("<script type=\"text/javascript\" src=\"%s\">\n</script>\n", $snippet['scriptUrl']);
           }
           break;
@@ -107,7 +110,10 @@ class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Cor
           break;
 
         case 'script':
-          if (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) {
+          if (!empty($snippet['esm'])) {
+            $html .= sprintf("<script type=\"module\">\n%s\n</script>\n", $snippet['script']);
+          }
+          elseif (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) {
             $html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $snippet['script']);
           }
           break;
index a81db1cfacb32e45f4eed2ee9c9ba7a265e3cac1..50a7aae3470501462defbcdb0af5cce8bbaf25c0 100644 (file)
@@ -57,6 +57,7 @@
  *     not guaranteed among versions/implementations.)
  *   - disabled: int, default=0
  *   - region: string
+ *   - esm: bool; for "script","scriptFile","scriptUrl", the "esm" flag enables Ecmascript 5 Module support
  *   - translate: bool|string, Autoload translations. (Only applies to 'scriptFile')
  *       - FALSE: Do not load translated strings.
  *       - TRUE: Load translated strings. Use the $ext's default domain.
index d009bb4b7969d9db30442cf3d92a8af50eabe023..032d1d7172e314eb90ca19614eb4429e2265b81c 100644 (file)
@@ -113,6 +113,14 @@ class CRM_Core_RegionTest extends CiviUnitTestCase {
     CRM_Core_Region::instance('testAllTypes')->add([
       'jquery' => '$("div");',
     ]);
+    CRM_Core_Region::instance('testAllTypes')->add([
+      'scriptUrl' => '/my%20module.mjs',
+      'esm' => TRUE,
+    ]);
+    CRM_Core_Region::instance('testAllTypes')->add([
+      'script' => 'import foo from "./foobar.mjs";',
+      'esm' => TRUE,
+    ]);
     CRM_Core_Region::instance('testAllTypes')->add([
       'styleUrl' => '/foo%20bar.css',
     ]);
@@ -131,6 +139,8 @@ class CRM_Core_RegionTest extends CiviUnitTestCase {
       . "<script type=\"text/javascript\" src=\"/foo%20bar.js\">\n</script>\n"
       . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n"
       . "<script type=\"text/javascript\">\nCRM.\$(function(\$) {\n\$(\"div\");\n});\n</script>\n"
+      . "<script type=\"module\" src=\"/my%20module.mjs\">\n</script>\n"
+      . "<script type=\"module\">\nimport foo from \"./foobar.mjs\";\n</script>\n"
       . "<link href=\"/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
       . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n";
     $this->assertEquals($expected, $actual);