feat: 区分开发和生产环境配置并完善加载与回归测试

This commit is contained in:
zhanjunhao
2026-10-27 15:55:20 +08:00
parent e74008239d
commit 599966060f
9 changed files with 271 additions and 113 deletions
+146
View File
@@ -0,0 +1,146 @@
/*
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 { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
import { loadConfigFromFile, loadEnv, createServer } from 'vite';
const root = fileURLToPath(new URL('../', import.meta.url));
const html = '<!--umami--><!--Google Analytics-->';
async function fixture(run) {
const dir = await mkdtemp(path.join(root, '.env-test-'));
try {
await writeFile(
path.join(dir, 'vite.config.js'),
await readFile(path.join(root, 'vite.config.js')),
);
await writeFile(
path.join(dir, '.env.development'),
'DEV_PROXY_TARGET=http://localhost:13001\nVITE_REACT_APP_SERVER_URL=https://dev.example.com\nVITE_CLOUD_BRAIN_UI_ENABLED=true\nVITE_UMAMI_WEBSITE_ID=dev-id\nVITE_UMAMI_SCRIPT_URL=https://dev.example.com/script.js\n',
);
await writeFile(
path.join(dir, '.env.production'),
'VITE_REACT_APP_SERVER_URL=https://prod.example.com\nVITE_CLOUD_BRAIN_UI_ENABLED=false\nVITE_GOOGLE_ANALYTICS_ID=G-PROD\n',
);
await run(dir);
} finally {
await rm(dir, { recursive: true, force: true });
}
}
async function config(dir, mode) {
return (
await loadConfigFromFile(
{ command: mode === 'development' ? 'serve' : 'build', mode },
path.join(dir, 'vite.config.js'),
)
).config;
}
function analytics(config) {
return config.plugins
.find((plugin) => plugin.name === 'inject-analytics')
.transformIndexHtml(html);
}
test('development and production isolate API, feature flag and analytics settings', async () => {
await fixture(async (dir) => {
const dev = await config(dir, 'development');
const prod = await config(dir, 'production');
assert.match(analytics(dev), /dev-id/);
assert.match(analytics(dev), /https:\/\/dev.example.com\/script.js/);
assert.doesNotMatch(analytics(dev), /G-PROD/);
assert.match(analytics(prod), /G-PROD/);
assert.doesNotMatch(analytics(prod), /dev-id/);
for (const proxy of Object.values(dev.server.proxy)) {
assert.equal(proxy.target, 'http://localhost:13001');
assert.equal(proxy.changeOrigin, true);
}
assert.equal(dev.server.proxy['/v1'].ws, true);
for (const [mode, url, flag] of [
['development', 'https://dev.example.com', 'true'],
['production', 'https://prod.example.com', 'false'],
]) {
const env = loadEnv(mode, dir);
assert.equal(env.VITE_REACT_APP_SERVER_URL, url);
assert.equal(env.VITE_CLOUD_BRAIN_UI_ENABLED, flag);
assert.equal(env.DEV_PROXY_TARGET, undefined);
}
});
});
test('mode local overrides and CI variables reach the Vite plugins and proxy', async () => {
await fixture(async (dir) => {
await writeFile(
path.join(dir, '.env.development.local'),
'DEV_PROXY_TARGET=http://localhost:13002\nVITE_UMAMI_WEBSITE_ID=local-id\n',
);
assert.match(analytics(await config(dir, 'development')), /local-id/);
assert.equal(
(await config(dir, 'development')).server.proxy['/api'].target,
'http://localhost:13002',
);
const previous = process.env.VITE_UMAMI_WEBSITE_ID;
try {
process.env.VITE_UMAMI_WEBSITE_ID = 'ci-id';
assert.match(analytics(await config(dir, 'development')), /ci-id/);
} finally {
if (previous === undefined) delete process.env.VITE_UMAMI_WEBSITE_ID;
else process.env.VITE_UMAMI_WEBSITE_ID = previous;
}
});
});
test('checked-in defaults preserve same-origin behavior and disable analytics', async () => {
for (const mode of ['development', 'production']) {
const env = loadEnv(mode, root);
assert.equal(env.VITE_REACT_APP_SERVER_URL, '');
assert.equal(env.VITE_CLOUD_BRAIN_UI_ENABLED, 'false');
assert.equal(analytics(await config(root, mode)), html);
}
});
test('development server transforms the HTML and runtime configuration modules', async () => {
const server = await createServer({
root,
server: { middlewareMode: true },
optimizeDeps: { noDiscovery: true, include: [] },
});
try {
const output = await server.transformIndexHtml(
'/',
await readFile(path.join(root, 'index.html'), 'utf8'),
);
assert.match(output, /@vite\/client/);
for (const url of [
'/src/helpers/config.js',
'/src/config/cloudBrainUi.js',
]) {
const transformed = await server.transformRequest(url);
assert.match(transformed.code, /VITE_CLOUD_BRAIN_UI_ENABLED/);
assert.doesNotMatch(transformed.code, /134\.175\.227\.134/);
}
} finally {
await server.close();
}
});