refactor: 移除运行时后端配置并统一使用构建变量
删除 public/config.js 及 HTML 加载引用,后端地址改为构建变量优先、否则同源。补充环境变量示例、配置回归测试与部署迁移说明。 验证:npm run check 通过,183 项测试通过,生产构建成功。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -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 || '';
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
Reference in New Issue
Block a user