Skip to content

Commit

Permalink
[9.x] Add manifestHash function to Illuminate\Foundation\Vite (la…
Browse files Browse the repository at this point in the history
…ravel#44136)

* feat: add `hash` function to Vite

* chore: rename `hash` to `manifestHash`

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
innocenzi and taylorotwell authored Sep 15, 2022
1 parent b7e096a commit 74cad57
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,26 @@ protected function manifestPath($buildDirectory)
return public_path($buildDirectory.'/manifest.json');
}

/**
* Get a unique hash representing the current manifest, or null if there is no manifest.
*
* @return string|null
*/
public function manifestHash($buildDirectory = null)
{
$buildDirectory ??= $this->buildDirectory;

if ($this->isRunningHot()) {
return null;
}

if (! is_file($path = $this->manifestPath($buildDirectory))) {
return null;
}

return md5_file($path) ?: null;
}

/**
* Get the chunk for the given entry point / asset.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/**
* @method static string useCspNonce(?string $nonce = null)
* @method static string|null cspNonce()
* @method static string|null manifestHash(?string $buildDirectory = null)
* @method static string asset(string $asset, string|null $buildDirectory)
* @method static string hotFile()
* @method static \Illuminate\Foundation\Vite useBuildDirectory(string $path)
Expand Down
30 changes: 30 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,36 @@ public function testItThrowsWhenUnableToFindAssetChunkInBuildMode()
ViteFacade::asset('resources/js/missing.js');
}

public function testItDoesNotReturnHashInDevMode()
{
$this->makeViteHotFile();

$this->assertNull(ViteFacade::manifestHash());

$this->cleanViteHotFile();
}

public function testItGetsHashInBuildMode()
{
$this->makeViteManifest(['a.js' => ['src' => 'a.js']]);

$this->assertSame('98ca5a789544599b562c9978f3147a0f', ViteFacade::manifestHash());

$this->cleanViteManifest();
}

public function testItGetsDifferentHashesForDifferentManifestsInBuildMode()
{
$this->makeViteManifest(['a.js' => ['src' => 'a.js']]);
$this->makeViteManifest(['b.js' => ['src' => 'b.js']], 'admin');

$this->assertSame('98ca5a789544599b562c9978f3147a0f', ViteFacade::manifestHash());
$this->assertSame('928a60835978bae84e5381fbb08a38b2', ViteFacade::manifestHash('admin'));

$this->cleanViteManifest();
$this->cleanViteManifest('admin');
}

public function testViteCanSetEntryPointsWithFluentBuilder()
{
$this->makeViteManifest();
Expand Down

0 comments on commit 74cad57

Please sign in to comment.