Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Resolve the Vite manifest using asset middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Feb 7, 2025
1 parent f76cfe9 commit 2959c9a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/Roots/Acorn/Assets/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Roots\Acorn\Assets\Exceptions\ManifestNotFoundException;
use Roots\Acorn\Assets\Middleware\LaravelMixMiddleware;
use Roots\Acorn\Assets\Middleware\RootsBudMiddleware;
use Roots\Acorn\Assets\Middleware\ViteMiddleware;

/**
* Manage assets manifests
Expand Down Expand Up @@ -37,6 +38,7 @@ class Manager
*/
protected $middleware = [
RootsBudMiddleware::class,
ViteMiddleware::class,
LaravelMixMiddleware::class,
];

Expand Down Expand Up @@ -92,11 +94,6 @@ protected function resolve(string $name, ?array $config): ManifestContract
$path = $config['path'];
$url = $config['url'];

$viteManifest = $path.'/build/manifest.json';
if (file_exists($viteManifest)) {
return new Manifest($path, $url, [], ['vite' => ['js' => [], 'css' => []]]);
}

$assets = isset($config['assets']) ? $this->getJsonManifest($config['assets']) : [];
$bundles = isset($config['bundles']) ? $this->getJsonManifest($config['bundles']) : [];

Expand Down
4 changes: 4 additions & 0 deletions src/Roots/Acorn/Assets/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public function __construct(string $path, string $uri, array $assets = [], ?arra
$this->bundles = $bundles;

foreach ($assets as $original => $revved) {
if (is_array($revved)) {
$revved = $revved['file'];
}

$this->assets[$this->normalizeRelativePath($original)] = $this->normalizeRelativePath($revved);
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/Roots/Acorn/Assets/Middleware/ViteMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Roots\Acorn\Assets\Middleware;

use Illuminate\Support\Facades\Vite;

class ViteMiddleware
{
/**
* Handle the manifest config.
*
* @param array $config
* @return array
*/
public function handle($config)
{
if (! Vite::manifestHash()) {
return $config;
}

return [
'url' => get_theme_file_uri('public/build'),
'path' => public_path('build'),
'bundles' => public_path('build/manifest.json'),
'assets' => public_path('build/manifest.json'),
];
}
}

0 comments on commit 2959c9a

Please sign in to comment.