From 171d0dccddd061e119170e063167b72e7c7cd632 Mon Sep 17 00:00:00 2001 From: Nagesh Tiwari Date: Mon, 21 Nov 2022 22:00:36 +0530 Subject: [PATCH] [9.x] Adding option for custom manifest filename on Vite Facade (#45007) * Adding option for custom manifest filename on Vite Facade * formatting Co-authored-by: Tim MacDonald --- src/Illuminate/Foundation/Vite.php | 22 ++++++++++++++++++- src/Illuminate/Support/Facades/Vite.php | 1 + tests/Foundation/FoundationViteTest.php | 28 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Vite.php b/src/Illuminate/Foundation/Vite.php index 18ea508b6828..f5f53364b0d8 100644 --- a/src/Illuminate/Foundation/Vite.php +++ b/src/Illuminate/Foundation/Vite.php @@ -48,6 +48,13 @@ class Vite implements Htmlable */ protected $buildDirectory = 'build'; + /** + * The name of the manifest file. + * + * @var string + */ + protected $manifestFilename = 'manifest.json'; + /** * The script tag attributes resolvers. * @@ -140,6 +147,19 @@ public function withEntryPoints($entryPoints) return $this; } + /** + * Set the filename for the manifest file. + * + * @param string $filename + * @return $this + */ + public function useManifestFilename($filename) + { + $this->manifestFilename = $filename; + + return $this; + } + /** * Get the Vite "hot" file path. * @@ -669,7 +689,7 @@ protected function manifest($buildDirectory) */ protected function manifestPath($buildDirectory) { - return public_path($buildDirectory.'/manifest.json'); + return public_path($buildDirectory.'/'.$this->manifestFilename); } /** diff --git a/src/Illuminate/Support/Facades/Vite.php b/src/Illuminate/Support/Facades/Vite.php index 7b51009a2d67..89265de2b7d2 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 \Illuminate\Foundation\Vite useManifestFilename(string $filename) * @method static string|null manifestHash(?string $buildDirectory = null) * @method static string asset(string $asset, ?string $buildDirectory = null) * @method static string hotFile() diff --git a/tests/Foundation/FoundationViteTest.php b/tests/Foundation/FoundationViteTest.php index 74ab7174be14..68814a7535b1 100644 --- a/tests/Foundation/FoundationViteTest.php +++ b/tests/Foundation/FoundationViteTest.php @@ -954,6 +954,34 @@ public function testCrossoriginAttributeIsIneritedByPreloadTags() $this->cleanViteManifest($buildDir); } + public function testItCanConfigureTheManifestFilename() + { + $buildDir = Str::random(); + app()->singleton('path.public', fn () => __DIR__); + if (! file_exists(public_path($buildDir))) { + mkdir(public_path($buildDir)); + } + $contents = json_encode([ + 'resources/js/app.js' => [ + 'src' => 'resources/js/app-from-custom-manifest.js', + 'file' => 'assets/app-from-custom-manifest.versioned.js', + ], + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + file_put_contents(public_path("{$buildDir}/custom-manifest.json"), $contents); + + ViteFacade::useManifestFilename('custom-manifest.json'); + + $result = app(Vite::class)(['resources/js/app.js'], $buildDir); + + $this->assertSame( + '' + .'', + $result->toHtml()); + + unlink(public_path("{$buildDir}/custom-manifest.json")); + rmdir(public_path($buildDir)); + } + protected function makeViteManifest($contents = null, $path = 'build') { app()->singleton('path.public', fn () => __DIR__);