diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 249ac0ceddd0..9c0a24d04f98 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -655,6 +655,30 @@ public function asset($asset, $buildDirectory = null) return $this->assetPath($buildDirectory.'/'.$chunk['file']); } + /** + * Get the content of a given asset. + * + * @param string $asset + * @param string|null $buildDirectory + * @return string + * + * @throws \Exception + */ + public function content($asset, $buildDirectory = null) + { + $buildDirectory ??= $this->buildDirectory; + + $chunk = $this->chunk($this->manifest($buildDirectory), $asset); + + $path = public_path($buildDirectory.'/'.$chunk['file']); + + if (! is_file($path) || ! file_exists($path)) { + throw new Exception("Unable to locate file from Vite manifest: {$path}."); + } + + return file_get_contents($path); + } + /** * Generate an asset path for the application. * diff --git a/src/Illuminate/Support/Facades/Vite.php b/src/Illuminate/Support/Facades/Vite.php index f7b4f02b67d1..4ebda1a3ae2d 100644 --- a/src/Illuminate/Support/Facades/Vite.php +++ b/src/Illuminate/Support/Facades/Vite.php @@ -17,6 +17,7 @@ * @method static \Illuminate\Foundation\Vite usePreloadTagAttributes(callable|array|false $attributes) * @method static \Illuminate\Support\HtmlString|void reactRefresh() * @method static string asset(string $asset, string|null $buildDirectory = null) + * @method static string content(string $asset, string|null $buildDirectory = null) * @method static string|null manifestHash(string|null $buildDirectory = null) * @method static bool isRunningHot() * @method static string toHtml() diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 4c0319a6afb8..dc7defb288d3 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -1184,6 +1184,31 @@ public function testItOnlyOutputsUniquePreloadTags() $this->cleanViteManifest($buildDir); } + public function testItRetrievesAssetContent() + { + $this->makeViteManifest(); + + $this->makeAsset('/app.versioned.js', 'some content'); + + $content = ViteFacade::content('resources/js/app.js'); + + $this->assertSame('some content', $content); + + $this->cleanAsset('/app.versioned.js'); + + $this->cleanViteManifest(); + } + + public function testItThrowsWhenUnableToFindFileToRetrieveContent() + { + $this->makeViteManifest(); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Unable to locate file from Vite manifest: '.public_path('build/assets/app.versioned.js')); + + ViteFacade::content('resources/js/app.js'); + } + protected function makeViteManifest($contents = null, $path = 'build') { app()->usePublicPath(__DIR__); @@ -1245,6 +1270,26 @@ protected function cleanViteManifest($path = 'build') } } + protected function makeAsset($asset, $content) + { + $path = public_path('build/assets'); + + if (! file_exists($path)) { + mkdir($path, recursive: true); + } + + file_put_contents($path.'/'.$asset, $content); + } + + protected function cleanAsset($asset) + { + $path = public_path('build/assets'); + + unlink($path.$asset); + + rmdir($path); + } + protected function makeViteHotFile($path = null) { app()->usePublicPath(__DIR__);