Skip to content

Commit 171d0dc

Browse files
ngstwrtimacdonald
andauthored
[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 <[email protected]>
1 parent 7add440 commit 171d0dc

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/Illuminate/Foundation/Vite.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class Vite implements Htmlable
4848
*/
4949
protected $buildDirectory = 'build';
5050

51+
/**
52+
* The name of the manifest file.
53+
*
54+
* @var string
55+
*/
56+
protected $manifestFilename = 'manifest.json';
57+
5158
/**
5259
* The script tag attributes resolvers.
5360
*
@@ -140,6 +147,19 @@ public function withEntryPoints($entryPoints)
140147
return $this;
141148
}
142149

150+
/**
151+
* Set the filename for the manifest file.
152+
*
153+
* @param string $filename
154+
* @return $this
155+
*/
156+
public function useManifestFilename($filename)
157+
{
158+
$this->manifestFilename = $filename;
159+
160+
return $this;
161+
}
162+
143163
/**
144164
* Get the Vite "hot" file path.
145165
*
@@ -669,7 +689,7 @@ protected function manifest($buildDirectory)
669689
*/
670690
protected function manifestPath($buildDirectory)
671691
{
672-
return public_path($buildDirectory.'/manifest.json');
692+
return public_path($buildDirectory.'/'.$this->manifestFilename);
673693
}
674694

675695
/**

src/Illuminate/Support/Facades/Vite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/**
66
* @method static string useCspNonce(?string $nonce = null)
77
* @method static string|null cspNonce()
8+
* @method static \Illuminate\Foundation\Vite useManifestFilename(string $filename)
89
* @method static string|null manifestHash(?string $buildDirectory = null)
910
* @method static string asset(string $asset, ?string $buildDirectory = null)
1011
* @method static string hotFile()

tests/Foundation/FoundationViteTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,34 @@ public function testCrossoriginAttributeIsIneritedByPreloadTags()
954954
$this->cleanViteManifest($buildDir);
955955
}
956956

957+
public function testItCanConfigureTheManifestFilename()
958+
{
959+
$buildDir = Str::random();
960+
app()->singleton('path.public', fn () => __DIR__);
961+
if (! file_exists(public_path($buildDir))) {
962+
mkdir(public_path($buildDir));
963+
}
964+
$contents = json_encode([
965+
'resources/js/app.js' => [
966+
'src' => 'resources/js/app-from-custom-manifest.js',
967+
'file' => 'assets/app-from-custom-manifest.versioned.js',
968+
],
969+
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
970+
file_put_contents(public_path("{$buildDir}/custom-manifest.json"), $contents);
971+
972+
ViteFacade::useManifestFilename('custom-manifest.json');
973+
974+
$result = app(Vite::class)(['resources/js/app.js'], $buildDir);
975+
976+
$this->assertSame(
977+
'<link rel="modulepreload" href="https://example.com/'.$buildDir.'/assets/app-from-custom-manifest.versioned.js" />'
978+
.'<script type="module" src="https://example.com/'.$buildDir.'/assets/app-from-custom-manifest.versioned.js"></script>',
979+
$result->toHtml());
980+
981+
unlink(public_path("{$buildDir}/custom-manifest.json"));
982+
rmdir(public_path($buildDir));
983+
}
984+
957985
protected function makeViteManifest($contents = null, $path = 'build')
958986
{
959987
app()->singleton('path.public', fn () => __DIR__);

0 commit comments

Comments
 (0)