|
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 | + }, |
37 | 49 | }, |
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