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 3fc3d07 commit 9670f3d
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/services/ViteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ public function init()
*/
public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [])
{
// Filter out empty attributes, but preserve boolean values
$preserveBools = function($value) {
return 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 @@ -256,6 +249,10 @@ public function devServerRegister(string $path, array $scriptTagAttrs = [])
}
$this->devServerShimsIncluded = true;
}
// Filter out empty attributes, but preserve boolean values
$scriptTagAttrs = array_filter($scriptTagAttrs, static function($value) {
return is_bool($value) || !empty($value);
});
// Include the entry script
$url = FileHelper::createUrl($this->devServerPublic, $path);
$view->registerJsFile(
Expand Down Expand Up @@ -434,13 +431,6 @@ public function invalidateCaches()
*/
public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string
{
// Filter out empty attributes, but preserve boolean values
$preserveBools = function($value) {
return 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 @@ -459,6 +449,10 @@ 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 function($value) {
return is_bool($value) || !empty($value);
});
// Include any dev server shims
if (!$this->devServerShimsIncluded) {
// Include the react-refresh-shim
Expand Down Expand Up @@ -544,6 +538,12 @@ protected function manifestRegisterTags(array $tags, array $legacyTags)
$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 function($value) {
return is_bool($value) || !empty($value);
});
}
$url = FileHelper::createUrl($this->serverPublic, $tag['url']);
switch ($tag['type']) {
case 'file':
Expand Down Expand Up @@ -629,6 +629,12 @@ 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 function($value) {
return is_bool($value) || !empty($value);
});
}
switch ($tag['type']) {
case 'file':
if (!$this->includeScriptOnloadHandler) {
Expand Down

0 comments on commit 9670f3d

Please sign in to comment.