Skip to content

Commit

Permalink
🩹 Look for Vite manifest (#432)
Browse files Browse the repository at this point in the history
* 🩹 Look for Vite manifest

* format

* 🧑‍💻 Resolve the Vite manifest using asset middleware

* 🎨 Prevent double build paths when resolving a Vite asset

* 🎨 Improve the Vite asset path replacement

* ✅ Fix asset tests

---------

Co-authored-by: Brandon <[email protected]>
  • Loading branch information
retlehs and Log1x authored Feb 9, 2025
1 parent e7a15b8 commit 86b0f36
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 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 @@ -91,6 +93,7 @@ protected function resolve(string $name, ?array $config): ManifestContract

$path = $config['path'];
$url = $config['url'];

$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'),
];
}
}
4 changes: 3 additions & 1 deletion src/Roots/Acorn/Assets/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Foundation\Vite as FoundationVite;

use function Roots\asset;

class Vite extends FoundationVite
{
/**
Expand All @@ -15,6 +17,6 @@ class Vite extends FoundationVite
*/
protected function assetPath($path, $secure = null)
{
return \Roots\asset($path)->uri();
return str_replace('/build/build/', '/build/', asset($path)->uri());
}
}
12 changes: 12 additions & 0 deletions tests/Assets/AssetsHelpersTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Facade;
use Roots\Acorn\Tests\Test\TestCase;

use function Roots\asset;
Expand All @@ -8,8 +9,13 @@

uses(TestCase::class);

beforeEach(function () {
Facade::setFacadeApplication(new \Roots\Acorn\Application);
});

it('asset() can access the default manifest', function () {
$app = new \Roots\Acorn\Application;

$app->singleton('config', fn () => new \Illuminate\Config\Repository([
'assets' => [
'default' => 'app',
Expand All @@ -29,13 +35,15 @@
],
],
]));

$app->register(\Roots\Acorn\Assets\AssetsServiceProvider::class);

assertMatchesSnapshot(asset('app.js')->uri());
});

it('asset() can access a specified manifest', function () {
$app = new \Roots\Acorn\Application;

$app->singleton('config', fn () => new \Illuminate\Config\Repository([
'assets' => [
'default' => 'app',
Expand All @@ -55,6 +63,7 @@
],
],
]));

$app->register(\Roots\Acorn\Assets\AssetsServiceProvider::class);

assertMatchesSnapshot(asset('editor.js', 'editor')->uri());
Expand All @@ -81,13 +90,15 @@
],
],
]));

$app->register(\Roots\Acorn\Assets\AssetsServiceProvider::class);

assertMatchesSnapshot(bundle('app')->js()->toJson());
});

it('bundle() can access a specified manifest', function () {
$app = new \Roots\Acorn\Application;

$app->singleton('config', fn () => new \Illuminate\Config\Repository([
'assets' => [
'default' => 'app',
Expand All @@ -107,6 +118,7 @@
],
],
]));

$app->register(\Roots\Acorn\Assets\AssetsServiceProvider::class);

assertMatchesSnapshot(bundle('editor', 'editor')->js()->toJson());
Expand Down

0 comments on commit 86b0f36

Please sign in to comment.