From c84efe2990ce5208f6e5855bc7614ea05926ec5b Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Fri, 3 Oct 2025 17:12:45 +0200 Subject: [PATCH 1/5] Add custom resolver for vite public paths --- src/Illuminate/Foundation/Vite.php | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 81278816abb8..71d05b17943d 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -83,6 +83,13 @@ class Vite implements Htmlable */ protected $preloadTagAttributesResolvers = []; + /** + * The public path resolver. + * + * @var callable|null + */ + protected $publicPathResolver = null; + /** * The preloaded assets. * @@ -215,6 +222,19 @@ public function createAssetPathsUsing($resolver) return $this; } + /** + * Resolve public paths using the provided resolver. + * + * @param callable|null $resolver + * @return $this + */ + public function createPublicPathsUsing($resolver) + { + $this->publicPathResolver = $resolver; + + return $this; + } + /** * Get the Vite "hot" file path. * @@ -897,7 +917,7 @@ public function content($asset, $buildDirectory = null) $chunk = $this->chunk($this->manifest($buildDirectory), $asset); - $path = public_path($buildDirectory.'/'.$chunk['file']); + $path = $this->publicPath($buildDirectory.'/'.$chunk['file']); if (! is_file($path) || ! file_exists($path)) { throw new ViteException("Unable to locate file from Vite manifest: {$path}."); @@ -918,6 +938,17 @@ protected function assetPath($path, $secure = null) return ($this->assetPathResolver ?? asset(...))($path, $secure); } + /** + * Generate a public path for an asset. + * + * @param string $path + * @return string + */ + protected function publicPath($path) + { + return ($this->publicPathResolver ?? public_path(...))($path); + } + /** * Get the manifest file for the given build directory. * From 489077924136a5c1c828bef22dca0a2143dfbe19 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Fri, 3 Oct 2025 17:13:02 +0200 Subject: [PATCH 2/5] Add test for custom vite public path resolver --- tests/Foundation/FoundationViteTest.php | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 0ac9c39512f3..6970fd02707c 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -696,6 +696,34 @@ public function testViteCanAssetPath() $this->cleanViteManifest($buildDir); } + public function testViteCanResolvePublicPath() + { + $this->makeViteManifest([ + 'resources/style.css' => [ + 'src' => 'resources/style.css', + 'file' => 'assets/style.css?v=version', + ], + ], $buildDir = Str::random()); + $vite = app(Vite::class)->useBuildDirectory($buildDir); + $this->makeAsset('/assets/style.css', 'some content'); + + // default behaviour: exception caused by appended version param + $this->assertThrows(fn () => $vite->content('resources/style.css'), ViteException::class); + + // custom behaviour: works because we strip the version param + $vite->createPublicPathsUsing(fn ($path) => Str::before($path, '?')); + + $this->assertDoesntThrow(fn () => $vite->content('resources/style.css')); + $this->assertSame('some content', $vite->content('resources/style.css')); + + // restore default behaviour... + $vite->createPublicPathsUsing(null); + + $this->assertThrows(fn () => $vite->content('resources/style.css'), ViteException::class); + + $this->cleanViteManifest($buildDir); + } + public function testViteIsMacroable() { $this->makeViteManifest([ From 300d381980647034e71d1be0f7b6e47f23bde434 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 7 Oct 2025 12:01:48 +0200 Subject: [PATCH 3/5] Make test build dir configurable --- tests/Foundation/FoundationViteTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 6970fd02707c..583905ccccd2 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -1761,9 +1761,9 @@ protected function cleanViteManifest($path = 'build') } } - protected function makeAsset($asset, $content) + protected function makeAsset($asset, $content, $buildDir = 'build') { - $path = public_path('build/assets'); + $path = public_path($buildDir.'/assets'); if (! file_exists($path)) { mkdir($path, recursive: true); @@ -1772,9 +1772,9 @@ protected function makeAsset($asset, $content) file_put_contents($path.'/'.$asset, $content); } - protected function cleanAsset($asset) + protected function cleanAsset($asset, $buildDir = 'build') { - $path = public_path('build/assets'); + $path = public_path($buildDir.'/assets'); unlink($path.$asset); From 41c2665b18151371e1c0f79fa7c52bf46c3d9a7e Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 7 Oct 2025 12:01:59 +0200 Subject: [PATCH 4/5] Clean up after tests --- tests/Foundation/FoundationViteTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 583905ccccd2..8cee6437619c 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -705,7 +705,7 @@ public function testViteCanResolvePublicPath() ], ], $buildDir = Str::random()); $vite = app(Vite::class)->useBuildDirectory($buildDir); - $this->makeAsset('/assets/style.css', 'some content'); + $this->makeAsset('/style.css', 'some content', $buildDir); // default behaviour: exception caused by appended version param $this->assertThrows(fn () => $vite->content('resources/style.css'), ViteException::class); @@ -721,6 +721,7 @@ public function testViteCanResolvePublicPath() $this->assertThrows(fn () => $vite->content('resources/style.css'), ViteException::class); + $this->cleanAsset('/style.css', $buildDir); $this->cleanViteManifest($buildDir); } From 9ae96da2e18c7e6d3e838c7bffb45211c3f972fd Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Tue, 7 Oct 2025 12:02:11 +0200 Subject: [PATCH 5/5] Resolve to correct public path --- tests/Foundation/FoundationViteTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 8cee6437619c..8bc01d6b2fa3 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -711,7 +711,7 @@ public function testViteCanResolvePublicPath() $this->assertThrows(fn () => $vite->content('resources/style.css'), ViteException::class); // custom behaviour: works because we strip the version param - $vite->createPublicPathsUsing(fn ($path) => Str::before($path, '?')); + $vite->createPublicPathsUsing(fn ($path) => public_path(Str::before($path, '?'))); $this->assertDoesntThrow(fn () => $vite->content('resources/style.css')); $this->assertSame('some content', $vite->content('resources/style.css'));