Skip to content
Closed
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
33 changes: 32 additions & 1 deletion src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class Vite implements Htmlable
*/
protected $preloadTagAttributesResolvers = [];

/**
* The public path resolver.
*
* @var callable|null
*/
protected $publicPathResolver = null;

/**
* The preloaded assets.
*
Expand Down Expand Up @@ -215,6 +222,19 @@ public function createAssetPathsUsing($resolver)
return $this;
}

/**
* Resolve public paths using the provided resolver.
*
* @param callable|null $resolver
* @return $this
*/
public function createPublicPathsUsing($resolver)
{
$this->publicPathResolver = $resolver;

return $this;
}

/**
* Get the Vite "hot" file path.
*
Expand Down Expand Up @@ -897,7 +917,7 @@ public function content($asset, $buildDirectory = null)

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

$path = public_path($buildDirectory.'/'.$chunk['file']);
$path = $this->publicPath($buildDirectory.'/'.$chunk['file']);

if (! is_file($path) || ! file_exists($path)) {
throw new ViteException("Unable to locate file from Vite manifest: {$path}.");
Expand All @@ -918,6 +938,17 @@ protected function assetPath($path, $secure = null)
return ($this->assetPathResolver ?? asset(...))($path, $secure);
}

/**
* Generate a public path for an asset.
*
* @param string $path
* @return string
*/
protected function publicPath($path)
{
return ($this->publicPathResolver ?? public_path(...))($path);
}

/**
* Get the manifest file for the given build directory.
*
Expand Down
37 changes: 33 additions & 4 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,35 @@ public function testViteCanAssetPath()
$this->cleanViteManifest($buildDir);
}

public function testViteCanResolvePublicPath()
{
$this->makeViteManifest([
'resources/style.css' => [
'src' => 'resources/style.css',
'file' => 'assets/style.css?v=version',
],
], $buildDir = Str::random());
$vite = app(Vite::class)->useBuildDirectory($buildDir);
$this->makeAsset('/style.css', 'some content', $buildDir);

// default behaviour: exception caused by appended version param
$this->assertThrows(fn () => $vite->content('resources/style.css'), ViteException::class);

// custom behaviour: works because we strip the version param
$vite->createPublicPathsUsing(fn ($path) => public_path(Str::before($path, '?')));

$this->assertDoesntThrow(fn () => $vite->content('resources/style.css'));
$this->assertSame('some content', $vite->content('resources/style.css'));

// restore default behaviour...
$vite->createPublicPathsUsing(null);

$this->assertThrows(fn () => $vite->content('resources/style.css'), ViteException::class);

$this->cleanAsset('/style.css', $buildDir);
$this->cleanViteManifest($buildDir);
}

public function testViteIsMacroable()
{
$this->makeViteManifest([
Expand Down Expand Up @@ -1733,9 +1762,9 @@ protected function cleanViteManifest($path = 'build')
}
}

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

if (! file_exists($path)) {
mkdir($path, recursive: true);
Expand All @@ -1744,9 +1773,9 @@ protected function makeAsset($asset, $content)
file_put_contents($path.'/'.$asset, $content);
}

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

unlink($path.$asset);

Expand Down