refactor(config): 统一后端地址的构建配置

移除运行时配置脚本,优先使用构建环境变量,未配置时使用同源地址。同步更新环境变量示例和部署说明。
This commit is contained in:
zhanjunhao
2026-10-22 12:50:16 +08:00
parent 67d08b69c2
commit 5f48bfda12
7 changed files with 88 additions and 77 deletions
+1 -5
View File
@@ -18,9 +18,5 @@ For commercial licensing, please contact support@quantumnous.com
*/
export function getServerURL() {
return (
window.__APP_CONFIG__?.SERVER_URL ||
import.meta.env.VITE_REACT_APP_SERVER_URL ||
''
);
return import.meta.env.VITE_REACT_APP_SERVER_URL || '';
}
+58
View File
@@ -0,0 +1,58 @@
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { transformWithEsbuild } from 'vite';
const source = await readFile(new URL('./config.js', import.meta.url), 'utf8');
async function loadConfig(env) {
const { code } = await transformWithEsbuild(source, 'config.js', {
loader: 'js',
define: { 'import.meta.env': JSON.stringify(env) },
});
return import(
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
);
}
for (const [name, env, expected] of [
['missing backend URL uses same origin', {}, ''],
['empty backend URL uses same origin', { VITE_REACT_APP_SERVER_URL: '' }, ''],
[
'configured backend URL works without a window global',
{ VITE_REACT_APP_SERVER_URL: 'https://api.example.com' },
'https://api.example.com',
],
]) {
test(name, async () => {
const { getServerURL } = await loadConfig(env);
assert.equal(getServerURL(), expected);
});
}
test('HTML no longer loads the removed runtime configuration script', async () => {
const html = await readFile(
new URL('../../index.html', import.meta.url),
'utf8',
);
assert.doesNotMatch(html, /<script\b[^>]*\bsrc=["']\/config\.js["']/i);
});