From 51421d05f0a2d0bf59ef194e30f287369f4d2275 Mon Sep 17 00:00:00 2001 From: Enzo Innocenzi Date: Wed, 20 Jul 2022 16:20:56 +0200 Subject: [PATCH] feat: support generating custom manifest paths --- src/Configuration.php | 10 ++++++++++ src/Vite.php | 15 +++++++++++++++ tests/Features/ManifestTest.php | 14 ++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/Configuration.php b/src/Configuration.php index 92f8f43..ac50241 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -52,6 +52,16 @@ public function getManifest(): ?Manifest */ public function getManifestPath(): string { + // If there is a strategy override, try to use that. + if (\is_callable(Vite::$findManifestPathWith)) { + $result = \call_user_func(Vite::$findManifestPathWith, $this); + + // Only override if there is a result. + if (!\is_null($result)) { + return $result; + } + } + return str_replace( ['\\', '//'], '/', diff --git a/src/Vite.php b/src/Vite.php index 1b8cf8a..fc0f261 100644 --- a/src/Vite.php +++ b/src/Vite.php @@ -33,6 +33,11 @@ final class Vite */ public static Closure|null $useManifestCallback = null; + /** + * @var (Closure(Innocenzi\Vite\Configuration): bool|null) + */ + public static Closure|null $findManifestPathWith = null; + /** * Gets the given configuration or the default one. */ @@ -83,6 +88,16 @@ public static function useManifest(Closure $callback = null): void static::$useManifestCallback = $callback; } + /** + * Sets the logic for finding the manifest path. + * + * @param (Closure(Innocenzi\Vite\Configuration): bool|null) $callback + */ + public static function findManifestPathWith(Closure $callback = null): void + { + static::$findManifestPathWith = $callback; + } + /** * Execute a method against the default configuration. */ diff --git a/tests/Features/ManifestTest.php b/tests/Features/ManifestTest.php index 1817455..7fb7a27 100644 --- a/tests/Features/ManifestTest.php +++ b/tests/Features/ManifestTest.php @@ -1,8 +1,10 @@ toContain('') ->toContain(''); }); + +it('can override the manifest path name generation', function () { + set_fixtures_path('builds'); + set_env('production'); + + Vite::findManifestPathWith(function (Configuration $configuration) { + return $configuration->getConfig('build_path') . '/owo/manifest.json'; + }); + + set_vite_config('default', ['build_path' => '/build']); + expect(vite()->getManifestPath())->toBe('/build/owo/manifest.json'); +});