Skip to content

Commit d016714

Browse files
committed
refactor: improve Vite configuration to dynamically determine branch name and slugify for GitHub Pages
1 parent 678b21f commit d016714

File tree

2 files changed

+66
-41
lines changed

2 files changed

+66
-41
lines changed

dist/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
5-
<link rel="icon" type="image/png" href="/demo-dapp-with-wallet/apple-touch-icon.png" />
5+
<link rel="icon" type="image/png" href="/demo-dapp-with-wallet/relayers/apple-touch-icon.png" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<meta property="og:site_name" content="Demo Dapp with @tonconnect/ui-react">
88
<title>Demo Dapp with @tonconnect/ui-react</title>
99

1010
<script data-consolejs-channel="57e95ad4-4a89-7cf9-106a-51106eb4d73d" src="https://remotejs.com/agent/agent.js"></script>
11-
<script type="module" crossorigin src="/demo-dapp-with-wallet/assets/index-BGCWMan1.js"></script>
12-
<link rel="stylesheet" crossorigin href="/demo-dapp-with-wallet/assets/index-C6qF6Nog.css">
11+
<script type="module" crossorigin src="/demo-dapp-with-wallet/relayers/assets/index-BGCWMan1.js"></script>
12+
<link rel="stylesheet" crossorigin href="/demo-dapp-with-wallet/relayers/assets/index-C6qF6Nog.css">
1313
</head>
1414
<body>
1515
<div id="root"></div>

vite.config.ts

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,64 @@
1-
import { defineConfig } from 'vite';
2-
import react from '@vitejs/plugin-react';
3-
import { nodePolyfills } from 'vite-plugin-node-polyfills';
4-
5-
const branch = process.env.BRANCH_NAME || 'main';
6-
const basePath =
7-
branch === 'main'
8-
? '/demo-dapp-with-wallet/'
9-
: `/demo-dapp-with-wallet/${branch}/`;
10-
11-
export default defineConfig({
12-
plugins: [
13-
react(),
14-
nodePolyfills({
15-
globals: { Buffer: true },
16-
protocolImports: true,
17-
}),
18-
],
19-
build: {
20-
outDir: 'dist',
21-
},
22-
resolve: {
23-
alias: {
24-
crypto: 'crypto-browserify',
25-
'vite-plugin-node-polyfills/shims/process': 'process',
26-
'node:process': 'process',
27-
'vite-plugin-node-polyfills/shims/buffer': 'buffer',
28-
'node:buffer': 'buffer',
29-
},
30-
},
31-
optimizeDeps: { include: ['process', 'buffer'] },
32-
// В проде (GH Pages) используем динамический base
33-
base: process.env.GH_PAGES ? basePath : './',
34-
server: {
35-
fs: {
36-
allow: ['../sdk', './'],
1+
// vite.config.ts
2+
import { defineConfig } from 'vite'
3+
import react from '@vitejs/plugin-react'
4+
import { nodePolyfills } from 'vite-plugin-node-polyfills'
5+
import { execSync } from 'node:child_process'
6+
7+
function getBranch() {
8+
// 1) приоритет у BRANCH_NAME (если передан из CI)
9+
if (process.env.BRANCH_NAME) return process.env.BRANCH_NAME
10+
11+
// 2) локально берём из git
12+
try {
13+
let name = execSync('git rev-parse --abbrev-ref HEAD', {
14+
stdio: ['ignore', 'pipe', 'ignore'],
15+
}).toString().trim()
16+
17+
// если detached HEAD — подставим короткий SHA
18+
if (name === 'HEAD') {
19+
name = execSync('git rev-parse --short HEAD', {
20+
stdio: ['ignore', 'pipe', 'ignore'],
21+
}).toString().trim()
22+
}
23+
return name
24+
} catch {
25+
return 'main' // запасной вариант, если git недоступен
26+
}
27+
}
28+
29+
// опционально: нормализуем под имя папки на Pages
30+
const slugify = (s: string) =>
31+
s.toLowerCase().replace(/[/\s]+/g, '-').replace(/[^a-z0-9._-]/g, '-')
32+
33+
export default defineConfig(({ command }) => {
34+
const isBuild = command === 'build'
35+
const branch = getBranch()
36+
const slug = slugify(branch)
37+
38+
return {
39+
plugins: [react(), nodePolyfills({ globals: { Buffer: true }, protocolImports: true })],
40+
build: { outDir: 'dist' },
41+
resolve: {
42+
alias: {
43+
crypto: 'crypto-browserify',
44+
'vite-plugin-node-polyfills/shims/process': 'process',
45+
'node:process': 'process',
46+
'vite-plugin-node-polyfills/shims/buffer': 'buffer',
47+
'node:buffer': 'buffer',
48+
},
3749
},
38-
},
39-
});
50+
optimizeDeps: { include: ['process', 'buffer'] },
51+
52+
// dev: '/', build: базовый путь по ветке
53+
base: isBuild
54+
? (branch === 'main'
55+
? '/demo-dapp-with-wallet/'
56+
: `/demo-dapp-with-wallet/${slug}/`)
57+
: '/',
58+
59+
server: { fs: { allow: ['../sdk', './'] } },
60+
61+
// (необязательно) Пробросить имя ветки в клиентский код:
62+
define: { __APP_BRANCH__: JSON.stringify(branch) },
63+
}
64+
})

0 commit comments

Comments
 (0)