29 lines
1.1 KiB
JavaScript
29 lines
1.1 KiB
JavaScript
// Author: fruit.com.cn
|
|
import { defineConfig, loadEnv } from 'vite';
|
|
import react from '@vitejs/plugin-react';
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
|
|
/** 把 public 里的 __SITE_URL__ 占位符替换成 .env 的 VITE_SITE_URL,域名只改一处。 */
|
|
function siteUrlPlugin() {
|
|
return {
|
|
name: 'site-url-replace',
|
|
apply: 'build',
|
|
closeBundle() {
|
|
const env = loadEnv('production', process.cwd(), '');
|
|
const url = (env.VITE_SITE_URL || '').replace(/\/$/, '');
|
|
if (!url) return;
|
|
for (const file of ['sitemap.xml', 'robots.txt']) {
|
|
const path = resolve('dist', file);
|
|
try {
|
|
writeFileSync(path, readFileSync(path, 'utf8').replaceAll('__SITE_URL__', url));
|
|
} catch {
|
|
// 文件不存在时忽略
|
|
}
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
export default defineConfig({ plugins: [react(), siteUrlPlugin()], server: { proxy: { '/app': { target: 'http://127.0.0.1:8081', changeOrigin: true }, '/files': { target: 'http://127.0.0.1:8081', changeOrigin: true } } } });
|