From 0fa59a1890a94ce5dc282c6f0fb7978d9ec508a9 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 11 Nov 2024 23:01:36 -0500 Subject: [PATCH] refactor: Filter out empty attributes, but preserve boolean values --- src/services/ViteService.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/services/ViteService.php b/src/services/ViteService.php index 07a445a..39d37f5 100755 --- a/src/services/ViteService.php +++ b/src/services/ViteService.php @@ -148,11 +148,6 @@ public function init(): void */ public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): void { - // Filter out empty attributes, but preserve boolean values - $preserveBools = fn($value) => is_bool($value) || !empty($value); - $scriptTagAttrs = array_filter($scriptTagAttrs, $preserveBools); - $cssTagAttrs = array_filter($cssTagAttrs, $preserveBools); - if ($this->devServerRunning()) { $this->devServerRegister($path, $scriptTagAttrs); @@ -254,6 +249,8 @@ public function devServerRegister(string $path, array $scriptTagAttrs = []): voi } $this->devServerShimsIncluded = true; } + // Filter out empty attributes, but preserve boolean values + $scriptTagAttrs = array_filter($scriptTagAttrs, static fn($value) => is_bool($value) || !empty($value)); // Include the entry script $url = FileHelper::createUrl($this->devServerPublic, $path); $view->registerJsFile( @@ -432,11 +429,6 @@ public function invalidateCaches(): void */ public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string { - // Filter out empty attributes, but preserve boolean values - $preserveBools = fn($value) => is_bool($value) || !empty($value); - $scriptTagAttrs = array_filter($scriptTagAttrs, $preserveBools); - $cssTagAttrs = array_filter($cssTagAttrs, $preserveBools); - if ($this->devServerRunning()) { return $this->devServerScript($path, $scriptTagAttrs); } @@ -455,6 +447,8 @@ public function script(string $path, bool $asyncCss = true, array $scriptTagAttr public function devServerScript(string $path, array $scriptTagAttrs = []): string { $lines = []; + // Filter out empty attributes, but preserve boolean values + $scriptTagAttrs = array_filter($scriptTagAttrs, static fn($value) => is_bool($value) || !empty($value)); // Include any dev server shims if (!$this->devServerShimsIncluded) { // Include the react-refresh-shim @@ -540,6 +534,10 @@ protected function manifestRegisterTags(array $tags, array $legacyTags): void $view = Craft::$app->getView(); foreach (array_merge($tags, $legacyTags) as $tag) { if (!empty($tag)) { + // Filter out empty attributes, but preserve boolean values + if (!empty($tag['options'])) { + $tag['options'] = array_filter($tag['options'], static fn($value) => is_bool($value) || !empty($value)); + } $url = FileHelper::createUrl($this->serverPublic, $tag['url']); switch ($tag['type']) { case 'file': @@ -625,6 +623,10 @@ protected function manifestScriptTags(array $tags, array $legacyTags): array foreach (array_merge($tags, $legacyTags) as $tag) { if (!empty($tag)) { $url = FileHelper::createUrl($this->serverPublic, $tag['url']); + // Filter out empty attributes, but preserve boolean values + if (!empty($tag['options'])) { + $tag['options'] = array_filter($tag['options'], static fn($value) => is_bool($value) || !empty($value)); + } switch ($tag['type']) { case 'file': if (!$this->includeScriptOnloadHandler) {