diff --git a/.gitignore b/.gitignore index 82b2424..9545809 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 6e48ffe..57a24d5 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 7294fbf..6924490 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 b7756be..ffc5fef 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 b30dc0d..5d09ac8 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(