Premier commit
This commit is contained in:
107
lib/Vite.php
Normal file
107
lib/Vite.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
namespace app\lib;
|
||||
|
||||
use Piko;
|
||||
|
||||
class Vite
|
||||
{
|
||||
// For a real-world example check here:
|
||||
// https://github.com/wp-bond/bond/blob/master/src/Tooling/Vite.php
|
||||
// https://github.com/wp-bond/boilerplate/tree/master/app/themes/boilerplate
|
||||
|
||||
// you might check @vitejs/plugin-legacy if you need to support older browsers
|
||||
// https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
|
||||
|
||||
/**
|
||||
* Prints all the html entries needed for Vite
|
||||
*/
|
||||
|
||||
public static function vite(string $entry): string
|
||||
{
|
||||
return implode("\n", [
|
||||
static::jsTag($entry),
|
||||
static::jsPreloadImports($entry),
|
||||
static::cssTag($entry)
|
||||
]);
|
||||
}
|
||||
|
||||
// Helpers to print tags
|
||||
private static function jsTag(string $entry): string
|
||||
{
|
||||
if (strpos($entry, '.js') === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$url = getenv('VITE_ENV') === 'dev'
|
||||
? getenv('VITE_HOST') . Piko::getAlias('@vite_web/' . $entry)
|
||||
: static::assetUrl($entry);
|
||||
|
||||
if (!$url) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '<script type="module" crossorigin src="' . $url . '"></script>';
|
||||
}
|
||||
|
||||
private static function jsPreloadImports(string $entry): string
|
||||
{
|
||||
if (getenv('VITE_ENV') === 'dev') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$res = '';
|
||||
|
||||
foreach (static::importsUrls($entry) as $url) {
|
||||
$res .= '<link rel="modulepreload" href="'. $url. '">';
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
private static function cssTag(string $entry): string
|
||||
{
|
||||
// Not needed on dev, it's inject by Vite
|
||||
if (getenv('VITE_ENV') === 'dev' || strpos($entry, '.css') === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$url = static::assetUrl($entry);
|
||||
|
||||
if (!$url) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '<link rel="stylesheet" type="text/css" href="' . $url . '">';
|
||||
}
|
||||
|
||||
// Helpers to locate files
|
||||
private static function getManifest(): array
|
||||
{
|
||||
$content = file_get_contents(Piko::getAlias('@webroot/manifest.json'));
|
||||
|
||||
return json_decode($content, true);
|
||||
}
|
||||
|
||||
private static function assetUrl(string $entry): string
|
||||
{
|
||||
$manifest = static::getManifest();
|
||||
|
||||
return isset($manifest[$entry])
|
||||
? Piko::getAlias('@web/' . $manifest[$entry]['file'])
|
||||
: '';
|
||||
}
|
||||
|
||||
private static function importsUrls(string $entry): array
|
||||
{
|
||||
$urls = [];
|
||||
$manifest = static::getManifest();
|
||||
|
||||
if (!empty($manifest[$entry]['imports'])) {
|
||||
foreach ($manifest[$entry]['imports'] as $imports) {
|
||||
$urls[] = Piko::getAlias('@web/' . $manifest[$imports]['file']);
|
||||
}
|
||||
}
|
||||
|
||||
return $urls;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user