Skip to content

Commit

Permalink
[10.x] Add content method to Vite (#47968)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
michael-rubel and jessarcher authored Aug 8, 2023
1 parent dd5c517 commit 060faa6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,30 @@ public function asset($asset, $buildDirectory = null)
return $this->assetPath($buildDirectory.'/'.$chunk['file']);
}

/**
* Get the content of a given asset.
*
* @param string $asset
* @param string|null $buildDirectory
* @return string
*
* @throws \Exception
*/
public function content($asset, $buildDirectory = null)
{
$buildDirectory ??= $this->buildDirectory;

$chunk = $this->chunk($this->manifest($buildDirectory), $asset);

$path = public_path($buildDirectory.'/'.$chunk['file']);

if (! is_file($path) || ! file_exists($path)) {
throw new Exception("Unable to locate file from Vite manifest: {$path}.");
}

return file_get_contents($path);
}

/**
* Generate an asset path for the application.
*
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 @@ -17,6 +17,7 @@
* @method static \Illuminate\Foundation\Vite usePreloadTagAttributes(callable|array|false $attributes)
* @method static \Illuminate\Support\HtmlString|void reactRefresh()
* @method static string asset(string $asset, string|null $buildDirectory = null)
* @method static string content(string $asset, string|null $buildDirectory = null)
* @method static string|null manifestHash(string|null $buildDirectory = null)
* @method static bool isRunningHot()
* @method static string toHtml()
Expand Down
45 changes: 45 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,31 @@ public function testItOnlyOutputsUniquePreloadTags()
$this->cleanViteManifest($buildDir);
}

public function testItRetrievesAssetContent()
{
$this->makeViteManifest();

$this->makeAsset('/app.versioned.js', 'some content');

$content = ViteFacade::content('resources/js/app.js');

$this->assertSame('some content', $content);

$this->cleanAsset('/app.versioned.js');

$this->cleanViteManifest();
}

public function testItThrowsWhenUnableToFindFileToRetrieveContent()
{
$this->makeViteManifest();

$this->expectException(Exception::class);
$this->expectExceptionMessage('Unable to locate file from Vite manifest: '.public_path('build/assets/app.versioned.js'));

ViteFacade::content('resources/js/app.js');
}

protected function makeViteManifest($contents = null, $path = 'build')
{
app()->usePublicPath(__DIR__);
Expand Down Expand Up @@ -1245,6 +1270,26 @@ protected function cleanViteManifest($path = 'build')
}
}

protected function makeAsset($asset, $content)
{
$path = public_path('build/assets');

if (! file_exists($path)) {
mkdir($path, recursive: true);
}

file_put_contents($path.'/'.$asset, $content);
}

protected function cleanAsset($asset)
{
$path = public_path('build/assets');

unlink($path.$asset);

rmdir($path);
}

protected function makeViteHotFile($path = null)
{
app()->usePublicPath(__DIR__);
Expand Down

0 comments on commit 060faa6

Please sign in to comment.