Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add manifestHash function to Illuminate\Foundation\Vite #44136

Merged
merged 3 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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