diff --git a/docs/src/guide/essentials/server-and-manifest-modes.md b/docs/src/guide/essentials/server-and-manifest-modes.md index 02f1fb4..05ba52a 100644 --- a/docs/src/guide/essentials/server-and-manifest-modes.md +++ b/docs/src/guide/essentials/server-and-manifest-modes.md @@ -38,3 +38,15 @@ Check the [development](/guide/essentials/development) documentation for more in You may need to replace the heartbeat implementation depending on your networking setup. To do so, you will need to bind your own implementation to the `Innocenzi\Vite\HeartbeatCheckers\HeartbeatChecker` interface. This can be done in the [configuration](/configuration/laravel-package#heartbeat-checker). + +## Overriding the mode + +If you have specific requirements and you prefer using your own logic to determine if the development server or the manifest should be used, you may call `Vite::useManifest()`. + +```php +Vite::useManifest(function (Configuration $configuration) { + return true; +}); +``` + +Its return value will determine whether the manifest will be used. If the callback returns `null` instead of a boolean value, it will be ignored and the normal checks will be performed. diff --git a/src/Configuration.php b/src/Configuration.php index acf2cdf..5c98abe 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -186,6 +186,16 @@ protected function findEntrypoints(): Collection */ protected function shouldUseManifest(): bool { + // If there is a strategy override, try to use that. + if (\is_callable(Vite::$useManifestCallback)) { + $result = \call_user_func(Vite::$useManifestCallback, $this); + + // Only override if the result is a boolean. + if (! \is_null($result)) { + return $result; + } + } + // If the development server is disabled, use the manifest. if (! $this->config('dev_server.enabled', true)) { return true; diff --git a/src/Vite.php b/src/Vite.php index d9cdb2a..57453c7 100644 --- a/src/Vite.php +++ b/src/Vite.php @@ -20,6 +20,11 @@ final class Vite */ public static Closure|null $makeStyleTagsCallback = null; + /** + * @var (Closure(Innocenzi\Vite\Configuration): bool|null) + */ + public static Closure|null $useManifestCallback = null; + /** * Gets the given configuration or the default one. */ @@ -30,14 +35,6 @@ public function config(string $name = null): Configuration return $this->configs[$name] ??= new Configuration($name); } - /** - * Sets whether the manifest should be used when testing. - */ - public static function useManifest(bool $useManifest = true): void - { - config()->set('vite.testing.use_manifest', $useManifest); - } - /** * Sets the logic for creating a script tag. * @@ -57,6 +54,16 @@ public static function makeStyleTagsUsing(Closure $callback = null): void { static::$makeStyleTagsCallback = $callback; } + + /** + * Sets the logic for determining if the manifest should be used. + * + * @param (Closure(Innocenzi\Vite\Configuration): bool|null) $callback + */ + public static function useManifest(Closure $callback = null): void + { + static::$useManifestCallback = $callback; + } /** * Execute a method against the default configuration. diff --git a/tests/Features/ConfigurationTest.php b/tests/Features/ConfigurationTest.php index ed97c1e..fd505d0 100644 --- a/tests/Features/ConfigurationTest.php +++ b/tests/Features/ConfigurationTest.php @@ -1,8 +1,12 @@ Vite::$useManifestCallback = null); it('uses the default configuration when not specifying one', function () { expect(vite()->getClientScriptTag()) @@ -100,3 +104,47 @@ expect(vite()->getAssetUrl('/my-custom-asset.txt')) ->toContain('https://s3.us-west-2.amazonaws.com/12345678/build/my-custom-asset'); }); + +it('respects the mode override in production', function () { + set_env('production'); + + expect(vite()->usesManifest())->toBeTrue(); + + Vite::useManifest(fn () => false); + + expect(vite()->usesManifest())->toBeFalse(); +}); + +it('respects the mode override in development', function () { + with_dev_server(reacheable: true); + set_env('local'); + + expect(vite()->usesManifest())->toBeFalse(); + + Vite::useManifest(fn () => true); + + expect(vite()->usesManifest())->toBeTrue(); + + config()->set('vite.configs.default.dev_server.enabled', false); + + Vite::useManifest(function (Configuration $cfg) { + return $cfg->getConfig('dev_server.enabled'); + }); + + expect(vite()->usesManifest())->toBeFalse(); +}); + +it('does not override the mode if returning null from the callback', function () { + set_env('production'); + expect(vite()->usesManifest())->toBeTrue(); + + Vite::useManifest(fn () => false); + expect(vite()->usesManifest())->toBeFalse(); + + Vite::useManifest(fn () => null); + expect(vite()->usesManifest())->toBeTrue(); + + with_dev_server(reacheable: true); + set_env('local'); + expect(vite()->usesManifest())->toBeFalse(); +}); diff --git a/tests/Features/DevelopmentServerTest.php b/tests/Features/DevelopmentServerTest.php index 8a507c5..cc5cc21 100644 --- a/tests/Features/DevelopmentServerTest.php +++ b/tests/Features/DevelopmentServerTest.php @@ -1,7 +1,5 @@ set('vite.testing.use_manifest', false); expect(vite()->usesManifest())->toBeFalse(); - - Vite::useManifest(); - expect(vite()->usesManifest())->toBeTrue(); }); it('uses the manifest when the development server is disabled', function () {