Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: support generating custom manifest paths #297

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
['\\', '//'],
'/',
Expand Down
15 changes: 15 additions & 0 deletions src/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/Features/ManifestTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Innocenzi\Vite\Configuration;
use Innocenzi\Vite\Exceptions\ManifestNotFoundException;
use Innocenzi\Vite\Exceptions\NoSuchEntrypointException;
use Innocenzi\Vite\Manifest;
use Innocenzi\Vite\Vite;

it('guesses the configuration name from the manifest path', function () {
set_vite_config('config-name', [
Expand Down Expand Up @@ -119,3 +121,15 @@
->toContain('<link rel="modulepreload" href="http://localhost/with-nested-imports/D.js" />')
->toContain('<link rel="stylesheet" href="http://localhost/with-nested-imports/D.css" />');
});

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');
});