Skip to content

Commit

Permalink
feat: support overriding mode check
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Feb 2, 2022
1 parent 74fc619 commit d3b962a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 13 deletions.
12 changes: 12 additions & 0 deletions docs/src/guide/essentials/server-and-manifest-modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 10 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 15 additions & 8 deletions src/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*
Expand All @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions tests/Features/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

use Illuminate\Routing\UrlGenerator;
use Innocenzi\Vite\Configuration;
use Innocenzi\Vite\Exceptions\NoBuildPathException;
use Innocenzi\Vite\Exceptions\NoSuchConfigurationException;
use Innocenzi\Vite\Vite;

afterAll(fn () => Vite::$useManifestCallback = null);

it('uses the default configuration when not specifying one', function () {
expect(vite()->getClientScriptTag())
Expand Down Expand Up @@ -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();
});
5 changes: 0 additions & 5 deletions tests/Features/DevelopmentServerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Innocenzi\Vite\Vite;

it('uses the development server by default', function () {
with_dev_server();
set_env('local');
Expand All @@ -20,9 +18,6 @@

config()->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 () {
Expand Down

0 comments on commit d3b962a

Please # to comment.