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

[10.x] Add content method to Vite #47968

Merged
merged 10 commits into from
Aug 8, 2023
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 for an 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 at path: {$path}.");
michael-rubel marked this conversation as resolved.
Show resolved Hide resolved
}

return file_get_contents($path);
michael-rubel marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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 at path: '.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