Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Vite application manager #45257

Closed
wants to merge 11 commits into from
Closed
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
151 changes: 151 additions & 0 deletions src/Illuminate/Contracts/Foundation/Vite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Illuminate\Contracts\Foundation;

use Illuminate\Contracts\Support\Htmlable;

interface Vite extends Htmlable
{
/**
* Apply configuration to the Vite instance.
*
* @param array $config
* @return $this
*/
public function configure($config);

/**
* Get the preloaded assets.
*
* @return array
*/
public function preloadedAssets();

/**
* Get the Content Security Policy nonce applied to all generated tags.
*
* @return string|null
*/
public function cspNonce();

/**
* Generate or set a Content Security Policy nonce to apply to all generated tags.
*
* @param string|null $nonce
* @return string
*/
public function useCspNonce($nonce = null);

/**
* Use the given key to detect integrity hashes in the manifest.
*
* @param string|false $key
* @return $this
*/
public function useIntegrityKey($key);

/**
* Set the Vite entry points.
*
* @param array $entryPoints
* @return $this
*/
public function withEntryPoints($entryPoints);

/**
* Set the filename for the manifest file.
*
* @param string $filename
* @return $this
*/
public function useManifestFilename($filename);

/**
* Get the Vite "hot" file path.
*
* @return string
*/
public function hotFile();

/**
* Set the Vite "hot" file path.
*
* @param string $path
* @return $this
*/
public function useHotFile($path);

/**
* Set the Vite build directory.
*
* @param string $path
* @return $this
*/
public function useBuildDirectory($path);

/**
* Use the given callback to resolve attributes for script tags.
*
* @param (callable(string, string, ?array, ?array): array)|array $attributes
* @return $this
*/
public function useScriptTagAttributes($attributes);

/**
* Use the given callback to resolve attributes for style tags.
*
* @param (callable(string, string, ?array, ?array): array)|array $attributes
* @return $this
*/
public function useStyleTagAttributes($attributes);

/**
* Use the given callback to resolve attributes for preload tags.
*
* @param (callable(string, string, ?array, ?array): array)|array $attributes
* @return $this
*/
public function usePreloadTagAttributes($attributes);

/**
* Generate Vite tags for an entrypoint.
*
* @param string|string[] $entrypoints
* @param string|null $buildDirectory
* @return \Illuminate\Support\HtmlString
*
* @throws \Exception
*/
public function __invoke($entrypoints, $buildDirectory = null);

/**
* Generate React refresh runtime script.
*
* @return \Illuminate\Support\HtmlString|void
*/
public function reactRefresh();

/**
* Get the URL for an asset.
*
* @param string $asset
* @param string|null $buildDirectory
* @return string
*/
public function asset($asset, $buildDirectory = null);

/**
* Get a unique hash representing the current manifest, or null if there is no manifest.
*
* @param string|null $buildDirectory
* @return string|null
*/
public function manifestHash($buildDirectory = null);

/**
* Determine if the HMR server is running.
*
* @return bool
*/
public function isRunningHot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Illuminate\Foundation\Http\HtmlDumper;
use Illuminate\Foundation\MaintenanceModeManager;
use Illuminate\Foundation\Precognition;
use Illuminate\Foundation\Vite;
use Illuminate\Http\Request;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\AggregateServiceProvider;
Expand All @@ -33,15 +32,7 @@ class FoundationServiceProvider extends AggregateServiceProvider
protected $providers = [
FormRequestServiceProvider::class,
ParallelTestingServiceProvider::class,
];

/**
* The singletons to register into the container.
*
* @var array
*/
public $singletons = [
Vite::class => Vite::class,
ViteServiceProvider::class,
];

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Illuminate/Foundation/Providers/ViteServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Foundation\Providers;

use Illuminate\Contracts\Foundation\Vite as ViteContract;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Foundation\Vite;
use Illuminate\Foundation\ViteManager;
use Illuminate\Support\ServiceProvider;

class ViteServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('vite', fn ($app) => new ViteManager($app));
$this->app->singleton('vite.app', fn ($app) => $app['vite']->app());
$this->app->bind(ViteContract::class, 'vite.app');
$this->app->bind(Vite::class, 'vite.app');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['vite', 'vite.app', ViteContract::class, Vite::class];
}
}
40 changes: 37 additions & 3 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Illuminate\Foundation;

use Exception;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Foundation\Vite as ViteContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use ReflectionClass;
use ReflectionMethod;

class Vite implements Htmlable
class Vite implements ViteContract
{
use Macroable;

Expand Down Expand Up @@ -90,6 +93,37 @@ class Vite implements Htmlable
*/
protected static $manifests = [];

/**
* Apply configuration to the Vite instance.
*
* @param array $config
* @return $this
*/
public function configure($config)
{
$class = new ReflectionClass($this);
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
$currentMethod = __FUNCTION__;

$methodKeyMap = [
'useCspNonce' => 'nonce',
'useScriptTagAttributes' => 'tag_attributes.script',
'useStyleTagAttributes' => 'tag_attributes.style',
'usePreloadTagAttributes' => 'tag_attributes.preload',
];

collect($methods)->filter(fn (ReflectionMethod $method) => ! $method->isStatic()
&& $method->getName() !== $currentMethod
&& preg_match('/^(use|with)/', $method->getName())
)->each(function (ReflectionMethod $method) use ($config, $methodKeyMap) {
$key = $methodKeyMap[$method->getName()]
?? str($method->getName())->after('use')->after('with')->snake()->value();
Arr::has($config, $key) && $method->invoke($this, Arr::get($config, $key));
});

return $this;
}

/**
* Get the preloaded assets.
*
Expand All @@ -113,7 +147,7 @@ public function cspNonce()
/**
* Generate or set a Content Security Policy nonce to apply to all generated tags.
*
* @param ?string $nonce
* @param string|null $nonce
* @return string
*/
public function useCspNonce($nonce = null)
Expand Down
Loading