Skip to content

Commit

Permalink
use vite prod env file
Browse files Browse the repository at this point in the history
  • Loading branch information
mac authored and mac committed Oct 4, 2024
1 parent 4100ef4 commit 3b6bce6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 30 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_UMAMI_SCRIPT=
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_UMAMI_SCRIPT=<script defer src="https://cloud.umami.is/script.js" data-website-id="ddd3bcb8-a0eb-45a2-b7e9-8cee5abe39af"></script>
34 changes: 4 additions & 30 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import preact from '@preact/preset-vite';
declare const process: {
env: {
GITHUB_PAGES?: string;
NODE_ENV?: string;
};
};

// 检查是否是 GitHub Pages 环境
const isGitHubPages = process.env.GITHUB_PAGES === 'true';
// 检查是否是生产环境
const isProduction = process.env.NODE_ENV === 'production';

export default defineConfig({
plugins: [ preact() ],
server: {
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
Expand All @@ -31,29 +26,8 @@ export default defineConfig({
},
},
optimizeDeps: {
include: ['pikafish'],
include: [ 'pikafish' ],
},
// 根据环境动态设置 base
base: isGitHubPages ? '/xiangqi-analysis/' : '/',
// 添加 HTML 替换逻辑
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
},
plugins: [
preact(),
{
name: 'html-transform',
transformIndexHtml(html) {
// 只在生产环境中注入统计脚本
if (isProduction) {
return html.replace(
'%VITE_UMAMI_SCRIPT%',
'<script defer src="https://cloud.umami.is/script.js" data-website-id="ddd3bcb8-a0eb-45a2-b7e9-8cee5abe39af"></script>'
);
}
// 在开发环境中移除占位符
return html.replace('%VITE_UMAMI_SCRIPT%', '');
},
},
],
});
base: process.env.GITHUB_PAGES === 'true' ? '/xiangqi-analysis/' : '/',
});

0 comments on commit 3b6bce6

Please sign in to comment.