Skip to content

Commit

Permalink
Add entry name to inernal tags origin
Browse files Browse the repository at this point in the history
  • Loading branch information
seggewiss committed Oct 21, 2024
1 parent 069db82 commit 2582b24
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ node_modules

/.vscode/settings.json
/.vscode/launch.json
/.idea

# Local Netlify folder
.netlify
3 changes: 2 additions & 1 deletion src/vite-bundle/src/Model/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
private string $content = '',
private string $origin = '',
string $preloadOption = 'link-tag',
private bool $internal = false,
) {
if (self::LINK_TAG === $tagName && isset($attributes['rel'])) {
if (in_array($attributes['rel'], ['modulepreload', 'preload']) && 'link-tag' !== $preloadOption) {
Expand Down Expand Up @@ -148,7 +149,7 @@ public function getOrigin(): string

public function isInternal(): bool
{
return '_internal' === $this->origin;
return $this->internal;
}

public function isRenderAsTag(): bool
Expand Down
5 changes: 3 additions & 2 deletions src/vite-bundle/src/Service/EntrypointRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function renderScripts(
if (!is_null($viteServer)) {
// vite server is active
if (!isset($this->returnedViteClients[$configName])) {
$tags[] = $tagRenderer->createViteClientScript($viteServer.$base.'@vite/client');
$tags[] = $tagRenderer->createViteClientScript($viteServer.$base.'@vite/client', $entryName);

$this->returnedViteClients[$configName] = true;
}
Expand Down Expand Up @@ -165,7 +165,8 @@ public function renderScripts(
'id' => 'vite-legacy-polyfill',
],
'',
'_internal'
$entryName,
true,
);
}

Expand Down
18 changes: 11 additions & 7 deletions src/vite-bundle/src/Service/TagRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ public function __construct(
) {
}

public function createViteClientScript(string $src): Tag
public function createViteClientScript(string $src, string $entryName = ''): Tag
{
return $this->createInternalScriptTag(
[
'type' => 'module',
'src' => $src,
'crossorigin' => true,
]
],
'',
$entryName
);
}

Expand Down Expand Up @@ -67,21 +69,22 @@ public function createDetectModernBrowserScript(): Tag
}

/** @param array<string, bool|string|null> $attributes */
public function createInternalScriptTag(array $attributes = [], string $content = ''): Tag
public function createInternalScriptTag(array $attributes = [], string $content = '', string $origin = ''): Tag
{
$tag = new Tag(
Tag::SCRIPT_TAG,
$attributes,
$content,
'_internal',
$this->preload
$origin,
$this->preload,
true,
);

return $tag;
}

/** @param array<string, bool|string|null> $attributes */
public function createScriptTag(array $attributes = [], string $content = '', string $origin = ''): Tag
public function createScriptTag(array $attributes = [], string $content = '', string $origin = '', bool $internal = false): Tag
{
$tag = new Tag(
Tag::SCRIPT_TAG,
Expand All @@ -92,7 +95,8 @@ public function createScriptTag(array $attributes = [], string $content = '', st
),
$content,
$origin,
$this->preload
$this->preload,
$internal
);

return $tag;
Expand Down
10 changes: 10 additions & 0 deletions src/vite-bundle/tests/Service/TagRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public function testSpecialTag()
'<script type="module" src="http://127.0.0.1:5173/build/@vite/client" crossorigin></script>',
$tagRenderer->generateTag($tag)
);
$this->assertTrue($tag->isInternal());
$this->assertEquals('', $tag->getOrigin());

$tag = $tagRenderer->createViteClientScript('http://127.0.0.1:5173/build/@vite/client', 'app-foo');
$this->assertEquals(
'<script type="module" src="http://127.0.0.1:5173/build/@vite/client" crossorigin></script>',
$tagRenderer->generateTag($tag)
);
$this->assertTrue($tag->isInternal());
$this->assertEquals('app-foo', $tag->getOrigin());

$tag = $tagRenderer->createReactRefreshScript('http://127.0.0.1:5173');
$this->assertEquals(
Expand Down

0 comments on commit 2582b24

Please sign in to comment.