Skip to content

Commit 060faa6

Browse files
[10.x] Add content method to Vite (#47968)
* Add `Vite::content()` method. * Add test for `content` method 🧪 * Add `content` method to the facade. * Styling. * Add file existence check 🛡 * Throw an exception instead of returning an empty string. * Remove redundant code in test 🧹 * Apply suggestion ✨ Co-authored-by: Jess Archer <[email protected]> * Fix test based on applied suggestion 🧪 --------- Co-authored-by: Jess Archer <[email protected]>
1 parent dd5c517 commit 060faa6

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/Illuminate/Foundation/Vite.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,30 @@ public function asset($asset, $buildDirectory = null)
655655
return $this->assetPath($buildDirectory.'/'.$chunk['file']);
656656
}
657657

658+
/**
659+
* Get the content of a given asset.
660+
*
661+
* @param string $asset
662+
* @param string|null $buildDirectory
663+
* @return string
664+
*
665+
* @throws \Exception
666+
*/
667+
public function content($asset, $buildDirectory = null)
668+
{
669+
$buildDirectory ??= $this->buildDirectory;
670+
671+
$chunk = $this->chunk($this->manifest($buildDirectory), $asset);
672+
673+
$path = public_path($buildDirectory.'/'.$chunk['file']);
674+
675+
if (! is_file($path) || ! file_exists($path)) {
676+
throw new Exception("Unable to locate file from Vite manifest: {$path}.");
677+
}
678+
679+
return file_get_contents($path);
680+
}
681+
658682
/**
659683
* Generate an asset path for the application.
660684
*

src/Illuminate/Support/Facades/Vite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* @method static \Illuminate\Foundation\Vite usePreloadTagAttributes(callable|array|false $attributes)
1818
* @method static \Illuminate\Support\HtmlString|void reactRefresh()
1919
* @method static string asset(string $asset, string|null $buildDirectory = null)
20+
* @method static string content(string $asset, string|null $buildDirectory = null)
2021
* @method static string|null manifestHash(string|null $buildDirectory = null)
2122
* @method static bool isRunningHot()
2223
* @method static string toHtml()

tests/Foundation/FoundationViteTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,31 @@ public function testItOnlyOutputsUniquePreloadTags()
11841184
$this->cleanViteManifest($buildDir);
11851185
}
11861186

1187+
public function testItRetrievesAssetContent()
1188+
{
1189+
$this->makeViteManifest();
1190+
1191+
$this->makeAsset('/app.versioned.js', 'some content');
1192+
1193+
$content = ViteFacade::content('resources/js/app.js');
1194+
1195+
$this->assertSame('some content', $content);
1196+
1197+
$this->cleanAsset('/app.versioned.js');
1198+
1199+
$this->cleanViteManifest();
1200+
}
1201+
1202+
public function testItThrowsWhenUnableToFindFileToRetrieveContent()
1203+
{
1204+
$this->makeViteManifest();
1205+
1206+
$this->expectException(Exception::class);
1207+
$this->expectExceptionMessage('Unable to locate file from Vite manifest: '.public_path('build/assets/app.versioned.js'));
1208+
1209+
ViteFacade::content('resources/js/app.js');
1210+
}
1211+
11871212
protected function makeViteManifest($contents = null, $path = 'build')
11881213
{
11891214
app()->usePublicPath(__DIR__);
@@ -1245,6 +1270,26 @@ protected function cleanViteManifest($path = 'build')
12451270
}
12461271
}
12471272

1273+
protected function makeAsset($asset, $content)
1274+
{
1275+
$path = public_path('build/assets');
1276+
1277+
if (! file_exists($path)) {
1278+
mkdir($path, recursive: true);
1279+
}
1280+
1281+
file_put_contents($path.'/'.$asset, $content);
1282+
}
1283+
1284+
protected function cleanAsset($asset)
1285+
{
1286+
$path = public_path('build/assets');
1287+
1288+
unlink($path.$asset);
1289+
1290+
rmdir($path);
1291+
}
1292+
12481293
protected function makeViteHotFile($path = null)
12491294
{
12501295
app()->usePublicPath(__DIR__);

0 commit comments

Comments
 (0)