From 2582b2466818ac04fd53cfe9883a2f208d7643e1 Mon Sep 17 00:00:00 2001 From: Sebastian Seggewiss Date: Mon, 21 Oct 2024 13:26:36 +0200 Subject: [PATCH] Add entry name to inernal tags origin --- .gitignore | 1 + src/vite-bundle/src/Model/Tag.php | 3 ++- .../src/Service/EntrypointRenderer.php | 5 +++-- src/vite-bundle/src/Service/TagRenderer.php | 18 +++++++++++------- .../tests/Service/TagRendererTest.php | 10 ++++++++++ 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 82b2424e..95458094 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,7 @@ node_modules /.vscode/settings.json /.vscode/launch.json +/.idea # Local Netlify folder .netlify diff --git a/src/vite-bundle/src/Model/Tag.php b/src/vite-bundle/src/Model/Tag.php index 6e48ffe7..57a24d52 100644 --- a/src/vite-bundle/src/Model/Tag.php +++ b/src/vite-bundle/src/Model/Tag.php @@ -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) { @@ -148,7 +149,7 @@ public function getOrigin(): string public function isInternal(): bool { - return '_internal' === $this->origin; + return $this->internal; } public function isRenderAsTag(): bool diff --git a/src/vite-bundle/src/Service/EntrypointRenderer.php b/src/vite-bundle/src/Service/EntrypointRenderer.php index 7294fbfc..69244904 100644 --- a/src/vite-bundle/src/Service/EntrypointRenderer.php +++ b/src/vite-bundle/src/Service/EntrypointRenderer.php @@ -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; } @@ -165,7 +165,8 @@ public function renderScripts( 'id' => 'vite-legacy-polyfill', ], '', - '_internal' + $entryName, + true, ); } diff --git a/src/vite-bundle/src/Service/TagRenderer.php b/src/vite-bundle/src/Service/TagRenderer.php index b7756be3..ffc5fefc 100644 --- a/src/vite-bundle/src/Service/TagRenderer.php +++ b/src/vite-bundle/src/Service/TagRenderer.php @@ -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 ); } @@ -67,21 +69,22 @@ public function createDetectModernBrowserScript(): Tag } /** @param array $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 $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, @@ -92,7 +95,8 @@ public function createScriptTag(array $attributes = [], string $content = '', st ), $content, $origin, - $this->preload + $this->preload, + $internal ); return $tag; diff --git a/src/vite-bundle/tests/Service/TagRendererTest.php b/src/vite-bundle/tests/Service/TagRendererTest.php index b30dc0d7..5d09ac86 100644 --- a/src/vite-bundle/tests/Service/TagRendererTest.php +++ b/src/vite-bundle/tests/Service/TagRendererTest.php @@ -169,6 +169,16 @@ public function testSpecialTag() '', $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( + '', + $tagRenderer->generateTag($tag) + ); + $this->assertTrue($tag->isInternal()); + $this->assertEquals('app-foo', $tag->getOrigin()); $tag = $tagRenderer->createReactRefreshScript('http://127.0.0.1:5173'); $this->assertEquals(