1
0

vite.config.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { defineConfig } from 'vite';
  2. import react from '@vitejs/plugin-react';
  3. import fs from 'node:fs';
  4. import path from 'node:path';
  5. import { DatabaseSync } from 'node:sqlite';
  6. const outDir = path.resolve(__dirname, '../web/dist');
  7. const BACKEND_TARGET = 'http://localhost:2053';
  8. function resolveDBPath() {
  9. const envFolder = process.env.XUI_DB_FOLDER;
  10. if (envFolder) {
  11. const abs = path.isAbsolute(envFolder)
  12. ? envFolder
  13. : path.resolve(__dirname, '..', envFolder);
  14. return path.join(abs, 'x-ui.db');
  15. }
  16. const repoSubDB = path.resolve(__dirname, '..', 'x-ui', 'x-ui.db');
  17. if (fs.existsSync(repoSubDB)) return repoSubDB;
  18. const repoDB = path.resolve(__dirname, '..', 'x-ui.db');
  19. if (fs.existsSync(repoDB)) return repoDB;
  20. return '/etc/x-ui/x-ui.db';
  21. }
  22. const PANEL_API_PREFIXES = ['panel/api/', 'panel/setting/', 'panel/xray/', 'panel/csrf-token'];
  23. let cachedBasePath = '/';
  24. function readBasePathFromDB() {
  25. const dbPath = resolveDBPath();
  26. let db;
  27. try {
  28. db = new DatabaseSync(dbPath, { readOnly: true });
  29. } catch (_e) {
  30. return '/';
  31. }
  32. try {
  33. const row = db.prepare('SELECT value FROM settings WHERE key = ?').get('webBasePath');
  34. let value = row && typeof row.value === 'string' ? row.value : '/';
  35. if (!value.startsWith('/')) value = '/' + value;
  36. if (!value.endsWith('/')) value += '/';
  37. return value;
  38. } catch (_e) {
  39. return '/';
  40. } finally {
  41. db.close();
  42. }
  43. }
  44. function refreshBasePath() {
  45. cachedBasePath = readBasePathFromDB();
  46. return cachedBasePath;
  47. }
  48. function readPanelVersion() {
  49. try {
  50. const versionFile = path.resolve(__dirname, '..', 'config', 'version');
  51. return fs.readFileSync(versionFile, 'utf8').trim();
  52. } catch (_e) {
  53. return '';
  54. }
  55. }
  56. // `apply: 'serve'` keeps the injection out of `vite build` — dist.go
  57. // already injects webBasePath and version at runtime in production.
  58. function injectBasePathPlugin() {
  59. return {
  60. name: 'xui-inject-base-path',
  61. apply: 'serve',
  62. transformIndexHtml(html) {
  63. const basePath = refreshBasePath();
  64. const escaped = basePath.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
  65. const version = readPanelVersion().replace(/\\/g, '\\\\').replace(/"/g, '\\"');
  66. const tag = `<script>window.X_UI_BASE_PATH="${escaped}";window.X_UI_CUR_VER="${version}";</script>`;
  67. return html.replace('</head>', `${tag}</head>`);
  68. },
  69. };
  70. }
  71. function bypassMigratedRoute(req) {
  72. if (req.method !== 'GET') return undefined;
  73. const url = req.url.split('?')[0];
  74. const basePath = refreshBasePath();
  75. if (url === basePath) return '/login.html';
  76. if (url.startsWith(basePath)) {
  77. const stripped = url.slice(basePath.length);
  78. for (const prefix of PANEL_API_PREFIXES) {
  79. if (prefix.endsWith('/')) {
  80. if (stripped.startsWith(prefix)) return undefined;
  81. } else if (stripped === prefix || stripped.startsWith(prefix + '/')) {
  82. return undefined;
  83. }
  84. }
  85. if (stripped === 'panel' || stripped === 'panel/' || stripped.startsWith('panel/')) {
  86. return '/index.html';
  87. }
  88. }
  89. return undefined;
  90. }
  91. function rewriteToBackend(p) {
  92. if (cachedBasePath === '/' || p.startsWith(cachedBasePath)) return p;
  93. return cachedBasePath + p.replace(/^\//, '');
  94. }
  95. function makeBackendProxy(target) {
  96. return {
  97. target,
  98. changeOrigin: true,
  99. rewrite: rewriteToBackend,
  100. bypass: bypassMigratedRoute,
  101. configure(proxy) {
  102. let warned = false;
  103. proxy.on('error', (err, req) => {
  104. const codes = new Set();
  105. if (err && err.code) codes.add(err.code);
  106. if (err && Array.isArray(err.errors)) {
  107. for (const inner of err.errors) {
  108. if (inner && inner.code) codes.add(inner.code);
  109. }
  110. }
  111. const offline = codes.has('ECONNREFUSED') || codes.has('ECONNRESET');
  112. if (offline) {
  113. if (!warned) {
  114. warned = true;
  115. // eslint-disable-next-line no-console
  116. console.warn(
  117. `[proxy] backend ${target} is not reachable — start the Go server (e.g. \`go run main.go\`) to forward ${req?.url || 'requests'}.`,
  118. );
  119. }
  120. return;
  121. }
  122. // eslint-disable-next-line no-console
  123. console.error('[proxy]', err);
  124. });
  125. },
  126. };
  127. }
  128. export default defineConfig({
  129. plugins: [react(), injectBasePathPlugin()],
  130. resolve: {
  131. alias: {
  132. '@': path.resolve(__dirname, 'src'),
  133. },
  134. },
  135. experimental: {
  136. renderBuiltUrl(filename, { hostType }) {
  137. if (hostType === 'js') {
  138. return {
  139. runtime: `((window.X_UI_BASE_PATH||'/')+${JSON.stringify(filename)})`,
  140. };
  141. }
  142. return undefined;
  143. },
  144. },
  145. build: {
  146. outDir,
  147. emptyOutDir: true,
  148. sourcemap: true,
  149. target: 'es2020',
  150. chunkSizeWarningLimit: 1500,
  151. rollupOptions: {
  152. input: {
  153. index: path.resolve(__dirname, 'index.html'),
  154. login: path.resolve(__dirname, 'login.html'),
  155. subpage: path.resolve(__dirname, 'subpage.html'),
  156. },
  157. output: {
  158. manualChunks(id) {
  159. if (!id.includes('node_modules')) return undefined;
  160. if (id.includes('/node_modules/antd/')) return 'vendor-antd';
  161. if (id.includes('/@ant-design/icons/') || id.includes('/@ant-design/icons-svg/')) return 'vendor-icons';
  162. if (
  163. id.includes('/node_modules/@rc-component/')
  164. || id.includes('/node_modules/rc-')
  165. || id.includes('/@ant-design/cssinjs')
  166. || id.includes('/@ant-design/colors')
  167. || id.includes('/@ant-design/fast-color')
  168. || id.includes('/@ant-design/react-slick')
  169. || id.includes('/@ctrl/tinycolor')
  170. ) return 'vendor-antd';
  171. if (
  172. id.includes('/node_modules/react-i18next/')
  173. || id.includes('/node_modules/i18next/')
  174. ) return 'vendor-i18next';
  175. if (
  176. id.includes('/node_modules/react/')
  177. || id.includes('/node_modules/react-dom/')
  178. || id.includes('/node_modules/scheduler/')
  179. ) return 'vendor-react';
  180. if (
  181. id.includes('/node_modules/codemirror/')
  182. || id.includes('/node_modules/@codemirror/')
  183. || id.includes('/node_modules/@lezer/')
  184. ) return 'vendor-codemirror';
  185. if (id.includes('/node_modules/persian-calendar-suite/')) return 'vendor-jalali';
  186. if (id.includes('/node_modules/otpauth/')) return 'vendor-otpauth';
  187. if (id.includes('/node_modules/@tanstack/')) return 'vendor-tanstack';
  188. if (id.includes('/node_modules/react-router')) return 'vendor-router';
  189. if (
  190. id.includes('/node_modules/swagger-ui-react/')
  191. || id.includes('/node_modules/swagger-ui/')
  192. || id.includes('/node_modules/swagger-client/')
  193. ) return 'vendor-swagger';
  194. if (
  195. id.includes('/node_modules/recharts/')
  196. || id.includes('/node_modules/victory-vendor/')
  197. || id.includes('/node_modules/d3-')
  198. ) return 'vendor-recharts';
  199. if (id.includes('dayjs')) return 'vendor-dayjs';
  200. if (id.includes('axios')) return 'vendor-axios';
  201. return 'vendor';
  202. },
  203. },
  204. },
  205. },
  206. server: {
  207. port: 5173,
  208. strictPort: true,
  209. proxy: {
  210. '^/(?:[^/]+/)?(login|logout|getTwoFactorEnable|csrf-token|panel|server)(?:/|$)': makeBackendProxy(BACKEND_TARGET),
  211. '^/$': makeBackendProxy(BACKEND_TARGET),
  212. '^/[^/]+/$': makeBackendProxy(BACKEND_TARGET),
  213. '^/(?:[^/]+/)?ws$': {
  214. target: 'ws://localhost:2053',
  215. ws: true,
  216. changeOrigin: true,
  217. rewrite: rewriteToBackend,
  218. },
  219. },
  220. },
  221. });