-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.ts
More file actions
executable file
·115 lines (108 loc) · 3.43 KB
/
Copy pathvite.config.ts
File metadata and controls
executable file
·115 lines (108 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2026 rezky_nightky
// SPDX-License-Identifier: GPL-3.0-only
import { sveltekit } from '@sveltejs/kit/vite';
import { visualizer } from 'rollup-plugin-visualizer';
import { type UserConfig, type Plugin } from 'vite';
import { createRequire } from 'node:module';
import { execFileSync } from 'child_process';
import path from 'path';
/**
* Pins resolution for @noble/hashes/argon2.js whose exports field
* fails in some Vite versions during production builds.
*
* CRITICAL: createRequire MUST be scoped to the project root (process.cwd()),
* NOT import.meta.url. Vite may bundle/relocate the config, and bun's
* require won't walk up from a temp directory to find packages.
*/
function pinCryptoDeps(): Plugin {
const projectRoot = process.cwd();
const req = createRequire(path.resolve(projectRoot, 'package.json'));
return {
name: 'pin-crypto-deps',
enforce: 'pre',
resolveId(id) {
if (id === '@noble/hashes/argon2.js') {
return req.resolve('@noble/hashes/argon2.js');
}
if (id === '@zxcvbn-ts/core') {
// Pin to ESM entry — CJS entry causes "exports is not defined" in browser
return req.resolve('@zxcvbn-ts/core/dist/index.esm.js');
}
if (id === '@zxcvbn-ts/language-common') {
return req.resolve('@zxcvbn-ts/language-common/dist/index.esm.js');
}
}
};
}
// Get commit info — prefer Vercel env vars (reliable in Vercel builds),
// fall back to local git commands for local dev.
function getLocalGitInfo(): { sha: string; branch: string } {
// Vercel injects these during builds; they are always accurate.
const vercelSha = process.env.VERCEL_GIT_COMMIT_SHA;
const vercelRef = process.env.VERCEL_GIT_COMMIT_REF;
// Use the full 40-character SHA so it can be exactly compared with the
// GitHub API response (also 40 chars). Short SHA (7 chars) caused a
// false-positive update loop because `===` could never match.
const sha = vercelSha
? vercelSha
: (() => {
try {
return execFileSync('git', ['rev-parse', 'HEAD'], {
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore']
}).trim();
} catch {
return 'unknown';
}
})();
const branch = vercelRef
? vercelRef
: (() => {
try {
return execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore']
}).trim();
} catch {
return 'main';
}
})();
return { sha, branch };
}
// Export async config function
export default function config({ mode }: { mode: string }): UserConfig {
const commitInfo = getLocalGitInfo();
return {
plugins: [
pinCryptoDeps(),
sveltekit(),
...(mode === 'analyze'
? [
visualizer({
filename: '.reports/stats.html',
template: 'treemap',
gzipSize: true,
brotliSize: true,
open: false
})
]
: [])
],
define: {
__GIT_COMMIT_ID__: JSON.stringify(commitInfo.sha),
__GIT_BRANCH__: JSON.stringify(commitInfo.branch),
__BUILD_TIME__: JSON.stringify(new Date().toISOString()),
__IS_VERCEL_DEPLOYMENT__: JSON.stringify(process.env.VERCEL === '1')
},
build: {
target: 'es2022',
minify: 'esbuild',
sourcemap: false,
// 700 KB warning threshold catches real regressions early.
// Known heavy lazy chunks (acceptable):
// @zxcvbn-ts/language-common: ~465 KB raw / ~229 KB gzip (password dictionary)
// These are dynamically imported only when the encryption modal opens.
chunkSizeWarningLimit: 700
}
};
}