Skip to content

Commit

Permalink
[9.x] Adding option for custom manifest filename on Vite Facade (#45007)
Browse files Browse the repository at this point in the history
* Adding option for custom manifest filename on Vite Facade

* formatting

Co-authored-by: Tim MacDonald <hello@timacdonald.me>
  • Loading branch information
ngstwr and timacdonald committed Nov 21, 2022
1 parent 7add440 commit 171d0dc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -669,7 +689,7 @@ protected function manifest($buildDirectory)
*/
protected function manifestPath($buildDirectory)
{
return public_path($buildDirectory.'/manifest.json');
return public_path($buildDirectory.'/'.$this->manifestFilename);
}

/**
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 \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()
Expand Down
28 changes: 28 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<link rel="modulepreload" href="https://example.com/'.$buildDir.'/assets/app-from-custom-manifest.versioned.js" />'
.'<script type="module" src="https://example.com/'.$buildDir.'/assets/app-from-custom-manifest.versioned.js"></script>',
$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__);
Expand Down

0 comments on commit 171d0dc

Please sign in to comment.