From afd3a28902781be030e06bbb126717b2238e9938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Fri, 4 Aug 2023 16:57:34 +0200 Subject: [PATCH 01/10] Add `Vite::content()` method. --- src/Illuminate/Foundation/Vite.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 249ac0ceddd0..2a1f1d38edbc 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -655,6 +655,24 @@ public function asset($asset, $buildDirectory = null) return $this->assetPath($buildDirectory.'/'.$chunk['file']); } + /** + * Get the content for an asset. + * + * @param string $asset + * @param string|null $buildDirectory + * @return string + */ + public function content($asset, $buildDirectory = null) + { + $buildDirectory ??= $this->buildDirectory; + + $chunk = $this->chunk($this->manifest($buildDirectory), $asset); + + $path = public_path($buildDirectory.'/'.$chunk['file']); + + return file_get_contents($path); + } + /** * Generate an asset path for the application. * From 805c5bf05b79dc9eaa28ad38123dbdf7bd557415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Fri, 4 Aug 2023 16:59:11 +0200 Subject: [PATCH 02/10] =?UTF-8?q?Add=20test=20for=20`content`=20method=20?= =?UTF-8?q?=F0=9F=A7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Foundation/FoundationViteTest.php | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 4c0319a6afb8..b54f74f35ffc 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -1184,6 +1184,21 @@ 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(); + } + protected function makeViteManifest($contents = null, $path = 'build') { app()->usePublicPath(__DIR__); @@ -1245,6 +1260,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__); From acce954a688d474d13fbb8bbaefcc4658d5b5438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Fri, 4 Aug 2023 17:00:08 +0200 Subject: [PATCH 03/10] Add `content` method to the facade. --- src/Illuminate/Support/Facades/Vite.php | 1 + 1 file changed, 1 insertion(+) 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() From b668e82afc1102b7f786d9b918488788109598a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Fri, 4 Aug 2023 17:19:23 +0200 Subject: [PATCH 04/10] Styling. --- 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 b54f74f35ffc..2daf43abfd78 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -1275,7 +1275,7 @@ protected function cleanAsset($asset) { $path = public_path('build/assets'); - unlink($path . $asset); + unlink($path.$asset); rmdir($path); } From 0cbbf68ea5646554a7b20d2841c2c128a9e57a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Fri, 4 Aug 2023 17:23:03 +0200 Subject: [PATCH 05/10] =?UTF-8?q?Add=20file=20existence=20check=20?= =?UTF-8?q?=F0=9F=9B=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Illuminate/Foundation/Vite.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 2a1f1d38edbc..3093903d6dc9 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -670,6 +670,10 @@ public function content($asset, $buildDirectory = null) $path = public_path($buildDirectory.'/'.$chunk['file']); + if (! is_file($path) || ! file_exists($path)) { + return ''; + } + return file_get_contents($path); } From 7b61309d658eb92d4d02f5a5d2cde1fa3cf2f5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Sun, 6 Aug 2023 02:31:08 +0200 Subject: [PATCH 06/10] Throw an exception instead of returning an empty string. --- src/Illuminate/Foundation/Vite.php | 4 +++- tests/Foundation/FoundationViteTest.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 3093903d6dc9..dfa97f8e8e68 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -661,6 +661,8 @@ public function asset($asset, $buildDirectory = null) * @param string $asset * @param string|null $buildDirectory * @return string + * + * @throws \Exception */ public function content($asset, $buildDirectory = null) { @@ -671,7 +673,7 @@ public function content($asset, $buildDirectory = null) $path = public_path($buildDirectory.'/'.$chunk['file']); if (! is_file($path) || ! file_exists($path)) { - return ''; + throw new Exception("Unable to locate file at path: {$path}."); } return file_get_contents($path); diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 2daf43abfd78..1a88841e3dc0 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -1199,6 +1199,20 @@ public function testItRetrievesAssetContent() $this->cleanViteManifest(); } + public function testItThrowsWhenUnableToFindFileToRetrieveContent() + { + $this->makeViteManifest(); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Unable to locate file at path: '.public_path('build/assets/app.versioned.js')); + + $content = ViteFacade::content('resources/js/app.js'); + + $this->assertSame('some content', $content); + + $this->cleanViteManifest(); + } + protected function makeViteManifest($contents = null, $path = 'build') { app()->usePublicPath(__DIR__); From e8702e291068df5a531248a0c4f536b0322e14c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Sun, 6 Aug 2023 02:41:16 +0200 Subject: [PATCH 07/10] =?UTF-8?q?Remove=20redundant=20code=20in=20test=20?= =?UTF-8?q?=F0=9F=A7=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Foundation/FoundationViteTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 1a88841e3dc0..f0cf804152ae 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -1206,11 +1206,7 @@ public function testItThrowsWhenUnableToFindFileToRetrieveContent() $this->expectException(Exception::class); $this->expectExceptionMessage('Unable to locate file at path: '.public_path('build/assets/app.versioned.js')); - $content = ViteFacade::content('resources/js/app.js'); - - $this->assertSame('some content', $content); - - $this->cleanViteManifest(); + ViteFacade::content('resources/js/app.js'); } protected function makeViteManifest($contents = null, $path = 'build') From 7057f8c778bdc7468d7add660be8b6c373537fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Sun, 6 Aug 2023 02:50:29 +0200 Subject: [PATCH 08/10] =?UTF-8?q?Apply=20suggestion=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jess Archer --- src/Illuminate/Foundation/Vite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index dfa97f8e8e68..775a77e370e9 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -673,7 +673,7 @@ public function content($asset, $buildDirectory = null) $path = public_path($buildDirectory.'/'.$chunk['file']); if (! is_file($path) || ! file_exists($path)) { - throw new Exception("Unable to locate file at path: {$path}."); + throw new Exception("Unable to locate file from Vite manifest: {$path}."); } return file_get_contents($path); From 78f82083be48b54c8470d0feac4565a3cfc16e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Rub=C3=A9l?= Date: Sun, 6 Aug 2023 02:55:31 +0200 Subject: [PATCH 09/10] =?UTF-8?q?Fix=20test=20based=20on=20applied=20sugge?= =?UTF-8?q?stion=20=F0=9F=A7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 f0cf804152ae..dc7defb288d3 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -1204,7 +1204,7 @@ public function testItThrowsWhenUnableToFindFileToRetrieveContent() $this->makeViteManifest(); $this->expectException(Exception::class); - $this->expectExceptionMessage('Unable to locate file at path: '.public_path('build/assets/app.versioned.js')); + $this->expectExceptionMessage('Unable to locate file from Vite manifest: '.public_path('build/assets/app.versioned.js')); ViteFacade::content('resources/js/app.js'); } From c807ef070956d5cf2a45449f886cc69d13eb441c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 8 Aug 2023 09:13:20 -0500 Subject: [PATCH 10/10] formatting --- src/Illuminate/Foundation/Vite.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 775a77e370e9..9c0a24d04f98 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -656,7 +656,7 @@ public function asset($asset, $buildDirectory = null) } /** - * Get the content for an asset. + * Get the content of a given asset. * * @param string $asset * @param string|null $buildDirectory