fix: il faut mettre l'url en partant de root mais pourquoi maintenant

This commit is contained in:
Christophe 2024-07-17 16:10:25 +02:00
parent b6f86ad302
commit 8bf80ffd8a

View file

@ -16,7 +16,7 @@ defined('_SERVEUR_MODE') || define('_SERVEUR_MODE', 'PROD');
// Prints all the html entries needed for Vite
function vite(string $entry, $port = "5134"): string {
function vite(string $entry, $port = 5134): string {
if (!empty($_SERVER['IS_DDEV_PROJECT'])) {
defined('VITE_HOST') || define('VITE_HOST', "https://" . $_SERVER['SERVER_ADDR'] . ":" .$port);
} else {
@ -31,19 +31,11 @@ function vite(string $entry, $port = "5134"): string {
cssTag($entry);
}
// Some dev/prod mechanism would exist in your project
function isDev(string $entry): bool {
// This method is very useful for the local server
// if we try to access it, and by any means, didn't started Vite yet
// it will fallback to load the production files from manifest
// so you still navigate your site as you intended!
static $exists = null;
if ($exists !== null) {
return $exists;
}
if ( _SERVEUR_MODE !== 'PROD') {
$handle = curl_init(VITE_HOST . "/" . $entry);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
@ -61,15 +53,14 @@ function isDev(string $entry): bool {
}
}
// Helpers to print tags
function jsTag(string $entry): string {
$url = isDev($entry) ? VITE_HOST . "/" . $entry : assetUrl($entry);
if (!$url) {
return "";
}
$url = isDev($entry)
? VITE_HOST . "/" . $entry
: assetUrl($entry);
if (!$url) {
return '';
}
if (isDev($entry)) {
return '<script type="module" src="' . VITE_HOST . '/@vite/client"></script>' . "\n"
. '<script type="module" src="' . $url . '"></script>';
@ -86,11 +77,11 @@ function jsPreloadImports(string $entry): string {
foreach (importsUrls($entry) as $url) {
$res .= '<link rel="modulepreload" href="' . $url . '">';
}
return $res;
}
function cssTag(string $entry): string {
// not needed on dev, it's inject by Vite
if (isDev($entry)) {
return "";
}
@ -99,21 +90,21 @@ function cssTag(string $entry): string {
foreach (cssUrls($entry) as $url) {
$tags .= '<link rel="stylesheet" href="' . $url . '">';
}
return $tags;
}
// Helpers to locate files
function getManifest(): array {
static $urlManifest;
//
if ($urlManifest === null) {
// vite V5
if (find_in_path("dist/.vite/manifest.json")) {
$urlManifest = find_in_path("dist/.vite/manifest.json");
if (find_in_path(_DIR_PLUGIN_GAMUTABLE . "dist/.vite/manifest.json")) {
$urlManifest = find_in_path(_DIR_PLUGIN_GAMUTABLE . "dist/.vite/manifest.json");
// vite V4
} else if (find_in_path("dist/manifest.json")) {
$urlManifest = find_in_path("dist/manifest.json");
} else if (find_in_path(_DIR_PLUGIN_GAMUTABLE . "dist/manifest.json")) {
$urlManifest = find_in_path(_DIR_PLUGIN_GAMUTABLE . "dist/manifest.json");
}
}
@ -123,6 +114,7 @@ function getManifest(): array {
} else {
die('pas de fichier manifest.json');
}
}
function assetUrl(string $entry): string {
@ -142,6 +134,7 @@ function importsUrls(string $entry): array {
$urls[] = find_in_path("dist/" . $manifest[$imports]["file"]);
}
}
return $urls;
}
@ -154,5 +147,7 @@ function cssUrls(string $entry): array {
$urls[] = find_in_path("dist/" . $file);
}
}
return $urls;
}