Skip to content

Commit 74cad57

Browse files
[9.x] Add manifestHash function to Illuminate\Foundation\Vite (#44136)
* feat: add `hash` function to Vite * chore: rename `hash` to `manifestHash` * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent b7e096a commit 74cad57

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Illuminate/Foundation/Vite.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,26 @@ protected function manifestPath($buildDirectory)
536536
return public_path($buildDirectory.'/manifest.json');
537537
}
538538

539+
/**
540+
* Get a unique hash representing the current manifest, or null if there is no manifest.
541+
*
542+
* @return string|null
543+
*/
544+
public function manifestHash($buildDirectory = null)
545+
{
546+
$buildDirectory ??= $this->buildDirectory;
547+
548+
if ($this->isRunningHot()) {
549+
return null;
550+
}
551+
552+
if (! is_file($path = $this->manifestPath($buildDirectory))) {
553+
return null;
554+
}
555+
556+
return md5_file($path) ?: null;
557+
}
558+
539559
/**
540560
* Get the chunk for the given entry point / asset.
541561
*

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 string|null manifestHash(?string $buildDirectory = null)
89
* @method static string asset(string $asset, string|null $buildDirectory)
910
* @method static string hotFile()
1011
* @method static \Illuminate\Foundation\Vite useBuildDirectory(string $path)

tests/Foundation/FoundationViteTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,36 @@ public function testItThrowsWhenUnableToFindAssetChunkInBuildMode()
535535
ViteFacade::asset('resources/js/missing.js');
536536
}
537537

538+
public function testItDoesNotReturnHashInDevMode()
539+
{
540+
$this->makeViteHotFile();
541+
542+
$this->assertNull(ViteFacade::manifestHash());
543+
544+
$this->cleanViteHotFile();
545+
}
546+
547+
public function testItGetsHashInBuildMode()
548+
{
549+
$this->makeViteManifest(['a.js' => ['src' => 'a.js']]);
550+
551+
$this->assertSame('98ca5a789544599b562c9978f3147a0f', ViteFacade::manifestHash());
552+
553+
$this->cleanViteManifest();
554+
}
555+
556+
public function testItGetsDifferentHashesForDifferentManifestsInBuildMode()
557+
{
558+
$this->makeViteManifest(['a.js' => ['src' => 'a.js']]);
559+
$this->makeViteManifest(['b.js' => ['src' => 'b.js']], 'admin');
560+
561+
$this->assertSame('98ca5a789544599b562c9978f3147a0f', ViteFacade::manifestHash());
562+
$this->assertSame('928a60835978bae84e5381fbb08a38b2', ViteFacade::manifestHash('admin'));
563+
564+
$this->cleanViteManifest();
565+
$this->cleanViteManifest('admin');
566+
}
567+
538568
public function testViteCanSetEntryPointsWithFluentBuilder()
539569
{
540570
$this->makeViteManifest();

0 commit comments

Comments
 (0)