44 lines
2.0 KiB
PHP
44 lines
2.0 KiB
PHP
<?php
|
|
// กำหนดว่า Environment ปัจจุบันเป็น Development หรือไม่
|
|
// (ควรตั้งค่าเป็น false เมื่อนำไปใช้งานบนเซิร์ฟเวอร์จริง)
|
|
define('IS_DEV', false);
|
|
define('VITE_HOST', 'http://localhost:5173');
|
|
|
|
function vite($entry = 'src/js/app.js') {
|
|
$html = '';
|
|
|
|
// ตั้งค่า Base URL ให้เป็น path สัมพัทธ์ (Relative Path) เพื่อรองรับการรันในโฟลเดอร์ย่อย (เช่น /intranet/.../)
|
|
$dist_url = 'dist/';
|
|
|
|
if (IS_DEV) {
|
|
$html .= '<script type="module" src="' . VITE_HOST . '/@vite/client"></script>';
|
|
$html .= '<script type="module" src="' . VITE_HOST . '/' . $entry . '"></script>';
|
|
} else {
|
|
// ในโหมด Production เราจะอ่านไฟล์จาก manifest.json ที่ถูก Build แล้ว
|
|
$manifestPath = __DIR__ . '/../dist/.vite/manifest.json';
|
|
|
|
if (file_exists($manifestPath)) {
|
|
$manifest = json_decode(file_get_contents($manifestPath), true);
|
|
|
|
if (isset($manifest[$entry])) {
|
|
$file = $manifest[$entry]['file'];
|
|
|
|
// ตรวจสอบว่ามีไฟล์ CSS ที่ผูกกับ JS ตัวนี้หรือไม่
|
|
if (isset($manifest[$entry]['css'])) {
|
|
foreach ($manifest[$entry]['css'] as $cssFile) {
|
|
$html .= '<link rel="stylesheet" href="' . $dist_url . $cssFile . '">';
|
|
}
|
|
}
|
|
|
|
// โหลดไฟล์ JS หลัก
|
|
$html .= '<script type="module" src="' . $dist_url . $file . '?v=' . time() . '"></script>';
|
|
}
|
|
} else {
|
|
$html .= '<!-- Vite Manifest Not Found! Please run `npm run build` -->';
|
|
}
|
|
}
|
|
|
|
return $html;
|
|
}
|
|
?>
|