From 6495a6eb9a2122be46244314ab537961e292a0b2 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 11:00:17 +1000 Subject: [PATCH 01/20] extract manifest file caching and access --- src/Illuminate/Foundation/Vite.php | 42 +++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 44233cc4b16e..2e81161112fa 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -9,6 +9,13 @@ class Vite { + /** + * The cached manifest files. + * + * @var array + */ + protected static $manifests = []; + /** * The Content Security Policy nonce to apply to all generated tags. * @@ -116,8 +123,6 @@ public function useStyleTagAttributes($attributes) */ public function __invoke($entrypoints, $buildDirectory = 'build') { - static $manifests = []; - $entrypoints = collect($entrypoints); $buildDirectory = Str::start($buildDirectory, '/'); @@ -132,17 +137,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build') ); } - $manifestPath = public_path($buildDirectory.'/manifest.json'); - - if (! isset($manifests[$manifestPath])) { - if (! is_file($manifestPath)) { - throw new Exception("Vite manifest not found at: {$manifestPath}"); - } - - $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); - } - - $manifest = $manifests[$manifestPath]; + $manifest = $this->manifest($buildDirectory); $tags = collect(); @@ -399,4 +394,25 @@ public function reactRefresh() ) ); } + + /** + * Get the the manifest file in the build directory. + * + * @param string $buildDirectory + * @return array + */ + protected function manifest($buildDirectory) + { + $path = public_path($buildDirectory.'/manifest.json'); + + if (! isset(static::$manifests[$path])) { + if (! is_file($path)) { + throw new Exception("Vite manifest not found at: {$path}"); + } + + static::$manifests[$path] = json_decode(file_get_contents($path), true); + } + + return static::$manifests[$path]; + } } From c3623e982d82b518cddf88eded89f507eb21bd8c Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 11:27:14 +1000 Subject: [PATCH 02/20] extract common functionality --- src/Illuminate/Foundation/Vite.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 2e81161112fa..f11cf94996ef 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -142,18 +142,16 @@ public function __invoke($entrypoints, $buildDirectory = 'build') $tags = collect(); foreach ($entrypoints as $entrypoint) { - if (! isset($manifest[$entrypoint])) { - throw new Exception("Unable to locate file in Vite manifest: {$entrypoint}."); - } + $chunk = $this->findChunk($manifest, $entrypoint); $tags->push($this->makeTagForChunk( $entrypoint, - asset("{$buildDirectory}/{$manifest[$entrypoint]['file']}"), - $manifest[$entrypoint], + asset("{$buildDirectory}/{$chunk['file']}"), + $chunk, $manifest )); - foreach ($manifest[$entrypoint]['css'] ?? [] as $css) { + foreach ($chunk['css'] ?? [] as $css) { $partialManifest = Collection::make($manifest)->where('file', $css); $tags->push($this->makeTagForChunk( @@ -164,7 +162,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build') )); } - foreach ($manifest[$entrypoint]['imports'] ?? [] as $import) { + foreach ($chunk['imports'] ?? [] as $import) { foreach ($manifest[$import]['css'] ?? [] as $css) { $partialManifest = Collection::make($manifest)->where('file', $css); @@ -415,4 +413,22 @@ protected function manifest($buildDirectory) return static::$manifests[$path]; } + + /** + * Find the chunk for the given entry point / asset. + * + * @param array $manifest + * @param string $file + * @return array + * + * @throws \Exception + */ + public function findChunk($manifest, $file) + { + if (! isset($manifest[$file])) { + throw new Exception("Unable to locate file in Vite manifest: {$file}."); + } + + return $manifest[$file]; + } } From 2dac23c7742e7021a8a4bf447b91e5c240f37007 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 11:27:59 +1000 Subject: [PATCH 03/20] add Vite asset helper --- src/Illuminate/Foundation/Vite.php | 18 ++++++++++++++++++ src/Illuminate/Support/Facades/Vite.php | 1 + tests/Foundation/FoundationViteTest.php | 9 +++++++++ 3 files changed, 28 insertions(+) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index f11cf94996ef..724964672701 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -393,6 +393,24 @@ public function reactRefresh() ); } + /** + * Get the URL for an asset. + * + * @param string $asset + * @param string|null $buildDirectory + * @return string + */ + public function asset($asset, $buildDirectory = null) + { + $buildDirectory = func_get_args()[1] ?? $this->buildDirectory ?? 'build'; + + $manifest = $this->manifest($buildDirectory); + + $this->findChunk($manifest, $asset); + + return asset($buildDirectory.'/'.$manifest[$asset]['file']); + } + /** * Get the the manifest file in the build directory. * diff --git a/src/Illuminate/Support/Facades/Vite.php b/src/Illuminate/Support/Facades/Vite.php index 610dc823016d..25afa063747b 100644 --- a/src/Illuminate/Support/Facades/Vite.php +++ b/src/Illuminate/Support/Facades/Vite.php @@ -5,6 +5,7 @@ /** * @method static string useCspNonce(?string $nonce = null) * @method static string|null cspNonce() + * @method static string asset(string $asset, string|null $buildDirectory) * @method static \Illuminte\Foundation\Vite useIntegrityKey(string|false $key) * @method static \Illuminte\Foundation\Vite useScriptTagAttributes(callable|array $callback) * @method static \Illuminte\Foundation\Vite useStyleTagAttributes(callable|array $callback) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 8bfc20eb9d27..6b0f036a8fe7 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -513,6 +513,15 @@ public function testItCanOverrideAllAttributes() ); } + public function testItCanGenerateIndividualAssetUrlInBuildMode() + { + $this->makeViteManifest(); + + $url = ViteFacade::asset('resources/js/app.js'); + + $this->assertSame('https://example.com/build/assets/app.versioned.js', $url); + } + protected function makeViteManifest($contents = null, $path = 'build') { app()->singleton('path.public', fn () => __DIR__); From 7e794ed45bdfcc21819351f7bc161fde0bb892f9 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 11:31:14 +1000 Subject: [PATCH 04/20] bring tests closer to reality --- src/Illuminate/Foundation/Vite.php | 1 - tests/Foundation/FoundationViteTest.php | 16 ++++------------ 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 724964672701..7435e349ef7a 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -124,7 +124,6 @@ public function useStyleTagAttributes($attributes) public function __invoke($entrypoints, $buildDirectory = 'build') { $entrypoints = collect($entrypoints); - $buildDirectory = Str::start($buildDirectory, '/'); if (is_file(public_path('/hot'))) { $url = rtrim(file_get_contents(public_path('/hot'))); diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 6b0f036a8fe7..d4b504664f8e 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -8,29 +8,21 @@ use Illuminate\Support\Facades\Vite as ViteFacade; use Illuminate\Support\Str; use Mockery as m; -use PHPUnit\Framework\TestCase; +use Orchestra\Testbench\TestCase; class FoundationViteTest extends TestCase { protected function setUp(): void { - app()->instance('url', tap( - m::mock(UrlGenerator::class), - fn ($url) => $url - ->shouldReceive('asset') - ->andReturnUsing(fn ($value) => "https://example.com{$value}") - )); - - app()->singleton(Vite::class); - Facade::setFacadeApplication(app()); + parent::setUp(); + + app('config')->set('app.asset_url', 'https://example.com'); } protected function tearDown(): void { $this->cleanViteManifest(); $this->cleanViteHotFile(); - Facade::clearResolvedInstances(); - m::close(); } public function testViteWithJsOnly() From affb1938be47d476866144d96e9114617c76d197 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 11:31:21 +1000 Subject: [PATCH 05/20] add missing docblock --- src/Illuminate/Foundation/Vite.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 7435e349ef7a..1f0811737eb4 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -415,6 +415,8 @@ public function asset($asset, $buildDirectory = null) * * @param string $buildDirectory * @return array + * + * @throws \Exception */ protected function manifest($buildDirectory) { From 6d83df63046111212aeed2787abb938d969980f3 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 11:36:14 +1000 Subject: [PATCH 06/20] fix visibility --- 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 1f0811737eb4..946893ed19da 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -442,7 +442,7 @@ protected function manifest($buildDirectory) * * @throws \Exception */ - public function findChunk($manifest, $file) + protected function findChunk($manifest, $file) { if (! isset($manifest[$file])) { throw new Exception("Unable to locate file in Vite manifest: {$file}."); From bafbe1b52378d3773f225269de94fe06a36f0a03 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 11:42:52 +1000 Subject: [PATCH 07/20] extract HMR check --- src/Illuminate/Foundation/Vite.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 946893ed19da..1cce8c2f4475 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -125,7 +125,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build') { $entrypoints = collect($entrypoints); - if (is_file(public_path('/hot'))) { + if ($this->isRunningHMR()) { $url = rtrim(file_get_contents(public_path('/hot'))); return new HtmlString( @@ -370,7 +370,7 @@ protected function parseAttributes($attributes) */ public function reactRefresh() { - if (! is_file(public_path('/hot'))) { + if (! $this->isRunningHMR()) { return; } @@ -450,4 +450,14 @@ protected function findChunk($manifest, $file) return $manifest[$file]; } + + /** + * Determine if the HMR server is running. + * + * @return bool + */ + protected function isRunningHMR() + { + return is_file(public_path('/hot')); + } } From bcc23506bd9a70b344321bd85e52a8fdb8109522 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 12:14:31 +1000 Subject: [PATCH 08/20] extract hot functions --- src/Illuminate/Foundation/Vite.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 1cce8c2f4475..538a5873e364 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -125,13 +125,11 @@ public function __invoke($entrypoints, $buildDirectory = 'build') { $entrypoints = collect($entrypoints); - if ($this->isRunningHMR()) { - $url = rtrim(file_get_contents(public_path('/hot'))); - + if ($this->isRunningHot()) { return new HtmlString( $entrypoints ->prepend('@vite/client') - ->map(fn ($entrypoint) => $this->makeTagForChunk($entrypoint, "{$url}/{$entrypoint}", null, null)) + ->map(fn ($entrypoint) => $this->makeTagForChunk($entrypoint, "{$this->hotAssetUrl()}/{$entrypoint}", null, null)) ->join('') ); } @@ -370,12 +368,10 @@ protected function parseAttributes($attributes) */ public function reactRefresh() { - if (! $this->isRunningHMR()) { + if (! $this->isRunningHot()) { return; } - $url = rtrim(file_get_contents(public_path('/hot'))); - return new HtmlString( sprintf( <<<'HTML' @@ -387,7 +383,7 @@ public function reactRefresh() window.__vite_plugin_react_preamble_installed__ = true HTML, - $url + $this->hotAssetUrl() ) ); } @@ -456,8 +452,18 @@ protected function findChunk($manifest, $file) * * @return bool */ - protected function isRunningHMR() + protected function isRunningHot() { return is_file(public_path('/hot')); } + + /** + * The path for assets during HMR mode. + * + * @return string + */ + protected function hotAssetUrl() + { + return rtrim(file_get_contents(public_path('/hot'))); + } } From 75f17652baf724469f2fe40513fb2cde9a917092 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 12:24:21 +1000 Subject: [PATCH 09/20] refactor hot asset to support passing an asset --- src/Illuminate/Foundation/Vite.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 538a5873e364..c4a69cbb1dcc 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -129,7 +129,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build') return new HtmlString( $entrypoints ->prepend('@vite/client') - ->map(fn ($entrypoint) => $this->makeTagForChunk($entrypoint, "{$this->hotAssetUrl()}/{$entrypoint}", null, null)) + ->map(fn ($entrypoint) => $this->makeTagForChunk($entrypoint, $this->hotAsset($entrypoint), null, null)) ->join('') ); } @@ -376,14 +376,14 @@ public function reactRefresh() sprintf( <<<'HTML' HTML, - $this->hotAssetUrl() + $this->hotAsset('@react-refresh') ) ); } @@ -462,8 +462,8 @@ protected function isRunningHot() * * @return string */ - protected function hotAssetUrl() + protected function hotAsset($asset) { - return rtrim(file_get_contents(public_path('/hot'))); + return rtrim(file_get_contents(public_path('/hot'))).'/'.$asset; } } From e3b307cda90b225f2e92864ad2ce1f306cd24087 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 12:24:36 +1000 Subject: [PATCH 10/20] add support for hot assets --- src/Illuminate/Foundation/Vite.php | 4 ++++ tests/Foundation/FoundationViteTest.php | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index c4a69cbb1dcc..a514962ca43a 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -397,6 +397,10 @@ public function reactRefresh() */ public function asset($asset, $buildDirectory = null) { + if ($this->isRunningHot()) { + return $this->hotAsset($asset); + } + $buildDirectory = func_get_args()[1] ?? $this->buildDirectory ?? 'build'; $manifest = $this->manifest($buildDirectory); diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index d4b504664f8e..92a334054272 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -514,6 +514,15 @@ public function testItCanGenerateIndividualAssetUrlInBuildMode() $this->assertSame('https://example.com/build/assets/app.versioned.js', $url); } + public function testItCanGenerateIndividualAssetUrlInHotMode() + { + $this->makeViteHotFile(); + + $url = ViteFacade::asset('resources/js/app.js'); + + $this->assertSame('http://localhost:3000/resources/js/app.js', $url); + } + protected function makeViteManifest($contents = null, $path = 'build') { app()->singleton('path.public', fn () => __DIR__); From 6cbdfce93321f0360158ad8bf46dceeb872611c5 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 12:41:35 +1000 Subject: [PATCH 11/20] cs --- tests/Foundation/FoundationViteTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 92a334054272..6b6093024375 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -3,11 +3,8 @@ namespace Illuminate\Tests\Foundation; use Illuminate\Foundation\Vite; -use Illuminate\Routing\UrlGenerator; -use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Vite as ViteFacade; use Illuminate\Support\Str; -use Mockery as m; use Orchestra\Testbench\TestCase; class FoundationViteTest extends TestCase From d59404f5535bbce50ad3ee3cf2cbd5c48328d900 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 12:50:35 +1000 Subject: [PATCH 12/20] use chunk --- src/Illuminate/Foundation/Vite.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index a514962ca43a..d7abd64c939e 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -405,9 +405,9 @@ public function asset($asset, $buildDirectory = null) $manifest = $this->manifest($buildDirectory); - $this->findChunk($manifest, $asset); + $chunk = $this->findChunk($manifest, $asset); - return asset($buildDirectory.'/'.$manifest[$asset]['file']); + return asset($buildDirectory.'/'$chunk['file']); } /** From 07bf70e7115446343449989017ebef2372a5d5b4 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 12:56:12 +1000 Subject: [PATCH 13/20] fix --- 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 d7abd64c939e..ba549447f9c1 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -407,7 +407,7 @@ public function asset($asset, $buildDirectory = null) $chunk = $this->findChunk($manifest, $asset); - return asset($buildDirectory.'/'$chunk['file']); + return asset($buildDirectory.'/'.$chunk['file']); } /** From 15b7366d8847d041d6e05bc127d0ca7448bdb31a Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 13:39:54 +1000 Subject: [PATCH 14/20] ref build directly --- 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 ba549447f9c1..e3a6261865d8 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -401,7 +401,7 @@ public function asset($asset, $buildDirectory = null) return $this->hotAsset($asset); } - $buildDirectory = func_get_args()[1] ?? $this->buildDirectory ?? 'build'; + $buildDirectory = $buildDirectory ?? $this->buildDirectory ?? 'build'; $manifest = $this->manifest($buildDirectory); From 4d8f0eb464c27916bd070c8fa18c8d078a6005f5 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 13:58:34 +1000 Subject: [PATCH 15/20] refactor --- src/Illuminate/Foundation/Vite.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index e3a6261865d8..b744777abb90 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -403,9 +403,7 @@ public function asset($asset, $buildDirectory = null) $buildDirectory = $buildDirectory ?? $this->buildDirectory ?? 'build'; - $manifest = $this->manifest($buildDirectory); - - $chunk = $this->findChunk($manifest, $asset); + $chunk = $this->findChunk($this->manifest($buildDirectory), $asset); return asset($buildDirectory.'/'.$chunk['file']); } From 1b398b7d66e2da1668c4df8c9a27f3f9b769456c Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 14:08:17 +1000 Subject: [PATCH 16/20] remove preemptive support --- src/Illuminate/Foundation/Vite.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index b744777abb90..a58c6ea362c7 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -395,14 +395,12 @@ public function reactRefresh() * @param string|null $buildDirectory * @return string */ - public function asset($asset, $buildDirectory = null) + public function asset($asset, $buildDirectory = 'build') { if ($this->isRunningHot()) { return $this->hotAsset($asset); } - $buildDirectory = $buildDirectory ?? $this->buildDirectory ?? 'build'; - $chunk = $this->findChunk($this->manifest($buildDirectory), $asset); return asset($buildDirectory.'/'.$chunk['file']); From 637e6f8c31554ec8ea0972fdfada9ed9f6484533 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 14:10:48 +1000 Subject: [PATCH 17/20] standardise naming --- src/Illuminate/Foundation/Vite.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index a58c6ea362c7..aba7626a82e4 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -139,7 +139,7 @@ public function __invoke($entrypoints, $buildDirectory = 'build') $tags = collect(); foreach ($entrypoints as $entrypoint) { - $chunk = $this->findChunk($manifest, $entrypoint); + $chunk = $this->chunk($manifest, $entrypoint); $tags->push($this->makeTagForChunk( $entrypoint, @@ -401,7 +401,7 @@ public function asset($asset, $buildDirectory = 'build') return $this->hotAsset($asset); } - $chunk = $this->findChunk($this->manifest($buildDirectory), $asset); + $chunk = $this->chunk($this->manifest($buildDirectory), $asset); return asset($buildDirectory.'/'.$chunk['file']); } @@ -430,7 +430,7 @@ protected function manifest($buildDirectory) } /** - * Find the chunk for the given entry point / asset. + * Get the chunk for the given entry point / asset. * * @param array $manifest * @param string $file @@ -438,7 +438,7 @@ protected function manifest($buildDirectory) * * @throws \Exception */ - protected function findChunk($manifest, $file) + protected function chunk($manifest, $file) { if (! isset($manifest[$file])) { throw new Exception("Unable to locate file in Vite manifest: {$file}."); From 78d8008faedff3feae515ea065fcd4809ff79604 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 14:20:51 +1000 Subject: [PATCH 18/20] exception tests --- tests/Foundation/FoundationViteTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 6b6093024375..873f22954959 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Foundation; +use Exception; use Illuminate\Foundation\Vite; use Illuminate\Support\Facades\Vite as ViteFacade; use Illuminate\Support\Str; @@ -520,6 +521,24 @@ public function testItCanGenerateIndividualAssetUrlInHotMode() $this->assertSame('http://localhost:3000/resources/js/app.js', $url); } + public function testItThrowsWhenUnableToFindAssetManifestInBuildMode() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Vite manifest not found at: '.public_path('build/manifest.json')); + + ViteFacade::asset('resources/js/app.js'); + } + + public function testItThrowsWhenUnableToFindAssetChunkInBuildMode() + { + $this->makeViteManifest(); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Unable to locate file in Vite manifest: resources/js/missing.js'); + + ViteFacade::asset('resources/js/missing.js'); + } + protected function makeViteManifest($contents = null, $path = 'build') { app()->singleton('path.public', fn () => __DIR__); From f9da3c1391e4af5c3a2c8bc2e8d1fcff4e2c89cc Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 15 Aug 2022 14:22:06 +1000 Subject: [PATCH 19/20] restore unrelated function removal --- src/Illuminate/Foundation/Vite.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index aba7626a82e4..046f7e35940c 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -124,6 +124,7 @@ public function useStyleTagAttributes($attributes) public function __invoke($entrypoints, $buildDirectory = 'build') { $entrypoints = collect($entrypoints); + $buildDirectory = Str::start($buildDirectory, '/'); if ($this->isRunningHot()) { return new HtmlString( From a8e965d7baa880dc728608bccbd0540c17eecb8e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 17 Aug 2022 08:48:32 -0500 Subject: [PATCH 20/20] formatting --- src/Illuminate/Foundation/Vite.php | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 046f7e35940c..7a137e10db8a 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -9,13 +9,6 @@ class Vite { - /** - * The cached manifest files. - * - * @var array - */ - protected static $manifests = []; - /** * The Content Security Policy nonce to apply to all generated tags. * @@ -44,6 +37,13 @@ class Vite */ protected $styleTagAttributesResolvers = []; + /** + * The cached manifest files. + * + * @var array + */ + protected static $manifests = []; + /** * Get the Content Security Policy nonce applied to all generated tags. * @@ -389,6 +389,16 @@ public function reactRefresh() ); } + /** + * Get the path to a given asset when running in HMR mode. + * + * @return string + */ + protected function hotAsset($asset) + { + return rtrim(file_get_contents(public_path('/hot'))).'/'.$asset; + } + /** * Get the URL for an asset. * @@ -408,7 +418,7 @@ public function asset($asset, $buildDirectory = 'build') } /** - * Get the the manifest file in the build directory. + * Get the the manifest file for the given build directory. * * @param string $buildDirectory * @return array @@ -457,14 +467,4 @@ protected function isRunningHot() { return is_file(public_path('/hot')); } - - /** - * The path for assets during HMR mode. - * - * @return string - */ - protected function hotAsset($asset) - { - return rtrim(file_get_contents(public_path('/hot'))).'/'.$asset; - } }