Skip to content

Commit 64b1ff0

Browse files
authored
feat(view): remove empty slots in production (#950)
1 parent 3d3a094 commit 64b1ff0

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/Tempest/View/src/Elements/ElementFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Dom\Node;
1111
use Dom\Text;
1212
use Tempest\Container\Container;
13+
use Tempest\Core\AppConfig;
1314
use Tempest\View\Element;
1415
use Tempest\View\Renderers\TempestViewCompiler;
1516
use Tempest\View\ViewComponent;
@@ -21,6 +22,7 @@ final class ElementFactory
2122
private TempestViewCompiler $compiler;
2223

2324
public function __construct(
25+
private readonly AppConfig $appConfig,
2426
private readonly ViewConfig $viewConfig,
2527
private readonly Container $container,
2628
) {}
@@ -97,9 +99,10 @@ private function makeElement(Node $node, ?Element $parent): ?Element
9799
}
98100

99101
$element = new ViewComponentElement(
100-
$this->compiler,
101-
$viewComponentClass,
102-
$attributes,
102+
environment: $this->appConfig->environment,
103+
compiler: $this->compiler,
104+
viewComponent: $viewComponentClass,
105+
attributes: $attributes,
103106
);
104107
} elseif ($tagName === 'x-template') {
105108
$element = new TemplateElement(

src/Tempest/View/src/Elements/ViewComponentElement.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tempest\View\Elements;
66

7+
use Tempest\Core\Environment;
78
use Tempest\View\Element;
89
use Tempest\View\Renderers\TempestViewCompiler;
910
use Tempest\View\Slot;
@@ -16,6 +17,7 @@ final class ViewComponentElement implements Element
1617
use IsElement;
1718

1819
public function __construct(
20+
private readonly Environment $environment,
1921
private readonly TempestViewCompiler $compiler,
2022
private readonly ViewComponent $viewComponent,
2123
array $attributes,
@@ -106,7 +108,7 @@ public function compile(): string
106108
if ($slot === null) {
107109
// A slot doesn't have any content, so we'll comment it out.
108110
// This is to prevent DOM parsing errors (slots in <head> tags is one example, see #937)
109-
return '<!--' . $matches[0] . '-->';
111+
return $this->environment->isProduction() ? '' : '<!--' . $matches[0] . '-->';
110112
}
111113

112114
return $slot->compile();

tests/Integration/View/ViewComponentTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Generator;
88
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Tempest\Core\AppConfig;
10+
use Tempest\Core\Environment;
911
use Tempest\Router\Session\Session;
1012
use Tempest\Validation\Rules\AlphaNumeric;
1113
use Tempest\Validation\Rules\Between;
@@ -503,4 +505,27 @@ public function test_empty_slots_are_commented_out(): void
503505
<html lang="en"><head><!--<x-slot name="styles" ></x-slot>--><link rel="stylesheet" href="#"></link></head></html>
504506
HTML, $html);
505507
}
508+
509+
public function test_empty_slots_are_removed_in_production(): void
510+
{
511+
$this->container->get(AppConfig::class)->environment = Environment::PRODUCTION;
512+
513+
$this->registerViewComponent('x-layout', <<<'HTML'
514+
<html lang="en">
515+
<head>
516+
<x-slot name="styles" />
517+
<link rel="stylesheet" href="#" />
518+
</head>
519+
</html>
520+
HTML);
521+
522+
$html = $this->render(<<<'HTML'
523+
<x-layout>
524+
</x-layout>
525+
HTML);
526+
527+
$this->assertStringEqualsStringIgnoringLineEndings(<<<'HTML'
528+
<html lang="en"><head><link rel="stylesheet" href="#"></link></head></html>
529+
HTML, $html);
530+
}
506531
}

0 commit comments

Comments
 (0)