Skip to content

Commit

Permalink
refactor: Filter out empty attributes, but preserve boolean values
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Nov 12, 2024
1 parent 483414e commit 0fa59a1
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/services/ViteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0fa59a1

Please sign in to comment.