Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add origin to internal tags #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
seggewiss marked this conversation as resolved.
Show resolved Hide resolved

$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