fix: il faut mettre l'url en partant de root mais pourquoi maintenant
This commit is contained in:
parent
b6f86ad302
commit
8bf80ffd8a
1 changed files with 19 additions and 24 deletions
39
inc/vite.php
39
inc/vite.php
|
@ -16,7 +16,7 @@ defined('_SERVEUR_MODE') || define('_SERVEUR_MODE', 'PROD');
|
||||||
|
|
||||||
// Prints all the html entries needed for Vite
|
// 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'])) {
|
if (!empty($_SERVER['IS_DDEV_PROJECT'])) {
|
||||||
defined('VITE_HOST') || define('VITE_HOST', "https://" . $_SERVER['SERVER_ADDR'] . ":" .$port);
|
defined('VITE_HOST') || define('VITE_HOST', "https://" . $_SERVER['SERVER_ADDR'] . ":" .$port);
|
||||||
} else {
|
} else {
|
||||||
|
@ -31,19 +31,11 @@ function vite(string $entry, $port = "5134"): string {
|
||||||
cssTag($entry);
|
cssTag($entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some dev/prod mechanism would exist in your project
|
|
||||||
|
|
||||||
function isDev(string $entry): bool {
|
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;
|
static $exists = null;
|
||||||
if ($exists !== null) {
|
if ($exists !== null) {
|
||||||
return $exists;
|
return $exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _SERVEUR_MODE !== 'PROD') {
|
if ( _SERVEUR_MODE !== 'PROD') {
|
||||||
$handle = curl_init(VITE_HOST . "/" . $entry);
|
$handle = curl_init(VITE_HOST . "/" . $entry);
|
||||||
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
|
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 {
|
function jsTag(string $entry): string {
|
||||||
$url = isDev($entry) ? VITE_HOST . "/" . $entry : assetUrl($entry);
|
$url = isDev($entry)
|
||||||
|
? VITE_HOST . "/" . $entry
|
||||||
|
: assetUrl($entry);
|
||||||
|
|
||||||
if (!$url) {
|
if (!$url) {
|
||||||
return "";
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDev($entry)) {
|
if (isDev($entry)) {
|
||||||
return '<script type="module" src="' . VITE_HOST . '/@vite/client"></script>' . "\n"
|
return '<script type="module" src="' . VITE_HOST . '/@vite/client"></script>' . "\n"
|
||||||
. '<script type="module" src="' . $url . '"></script>';
|
. '<script type="module" src="' . $url . '"></script>';
|
||||||
|
@ -86,11 +77,11 @@ function jsPreloadImports(string $entry): string {
|
||||||
foreach (importsUrls($entry) as $url) {
|
foreach (importsUrls($entry) as $url) {
|
||||||
$res .= '<link rel="modulepreload" href="' . $url . '">';
|
$res .= '<link rel="modulepreload" href="' . $url . '">';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
function cssTag(string $entry): string {
|
function cssTag(string $entry): string {
|
||||||
// not needed on dev, it's inject by Vite
|
|
||||||
if (isDev($entry)) {
|
if (isDev($entry)) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -99,21 +90,21 @@ function cssTag(string $entry): string {
|
||||||
foreach (cssUrls($entry) as $url) {
|
foreach (cssUrls($entry) as $url) {
|
||||||
$tags .= '<link rel="stylesheet" href="' . $url . '">';
|
$tags .= '<link rel="stylesheet" href="' . $url . '">';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tags;
|
return $tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helpers to locate files
|
|
||||||
|
|
||||||
function getManifest(): array {
|
function getManifest(): array {
|
||||||
static $urlManifest;
|
static $urlManifest;
|
||||||
|
//
|
||||||
if ($urlManifest === null) {
|
if ($urlManifest === null) {
|
||||||
// vite V5
|
// 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
|
// vite V4
|
||||||
} else if (find_in_path("dist/manifest.json")) {
|
} else if (find_in_path(_DIR_PLUGIN_GAMUTABLE . "dist/manifest.json")) {
|
||||||
$urlManifest = find_in_path("dist/manifest.json");
|
$urlManifest = find_in_path(_DIR_PLUGIN_GAMUTABLE . "dist/manifest.json");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +114,7 @@ function getManifest(): array {
|
||||||
} else {
|
} else {
|
||||||
die('pas de fichier manifest.json');
|
die('pas de fichier manifest.json');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function assetUrl(string $entry): string {
|
function assetUrl(string $entry): string {
|
||||||
|
@ -142,6 +134,7 @@ function importsUrls(string $entry): array {
|
||||||
$urls[] = find_in_path("dist/" . $manifest[$imports]["file"]);
|
$urls[] = find_in_path("dist/" . $manifest[$imports]["file"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $urls;
|
return $urls;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,5 +147,7 @@ function cssUrls(string $entry): array {
|
||||||
$urls[] = find_in_path("dist/" . $file);
|
$urls[] = find_in_path("dist/" . $file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $urls;
|
return $urls;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue