-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
99 lines (93 loc) · 3.08 KB
/
Copy pathvite.config.ts
File metadata and controls
99 lines (93 loc) · 3.08 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
import { cloudflare } from '@cloudflare/vite-plugin';
import tailwindcss from '@tailwindcss/vite';
import react from '@vitejs/plugin-react';
import { defineConfig, type Plugin } from 'vite';
interface GitHubPullRequest {
html_url?: unknown;
merged_at?: unknown;
number?: unknown;
title?: unknown;
}
const buildTimestampUtc = process.env.VITE_BUILD_TIMESTAMP ?? new Date().toISOString();
const buildPrUrl =
process.env.VITE_BUILD_PR_URL ??
'https://github.com/RideControlOrg/RideControl/pulls?q=is%3Apr+is%3Aclosed';
function deploymentVersionPlugin(version: string): Plugin {
return {
apply: 'build',
generateBundle() {
this.emitFile({
fileName: 'version.json',
source: `${JSON.stringify({ version })}\n`,
type: 'asset',
});
},
name: 'ride-control-deployment-version',
};
}
function validPullRequest(pullRequest: GitHubPullRequest) {
return (
typeof pullRequest.title === 'string' &&
typeof pullRequest.html_url === 'string' &&
typeof pullRequest.merged_at === 'string' &&
typeof pullRequest.number === 'number'
);
}
async function cloudflareRecentPullRequests() {
if (!(process.env.WORKERS_CI === '1' && process.env.WORKERS_CI_BRANCH === 'main')) {
return '[]';
}
try {
const headers: Record<string, string> = {
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
};
if (process.env.GITHUB_TOKEN) {
headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
}
const response = await fetch(
'https://api.github.com/repos/RideControlOrg/RideControl/pulls?state=closed&base=main&sort=updated&direction=desc&per_page=100',
{ headers, signal: AbortSignal.timeout(5000) }
);
if (!response.ok) {
throw new Error(`GitHub returned HTTP ${response.status}`);
}
const payload: unknown = await response.json();
if (!Array.isArray(payload)) {
throw new Error('GitHub returned an unexpected pull request response');
}
const pullRequests = (payload as GitHubPullRequest[])
.filter(validPullRequest)
.sort((left, right) => {
const rightTime = new Date(right.merged_at as string).getTime();
const leftTime = new Date(left.merged_at as string).getTime();
return rightTime - leftTime;
})
.slice(0, 10)
.map((pullRequest) => ({
mergedAt: pullRequest.merged_at,
number: pullRequest.number,
title: pullRequest.title,
url: pullRequest.html_url,
}));
return JSON.stringify(pullRequests);
} catch (error) {
console.warn('Could not include recent pull requests in this production build.', error);
return '[]';
}
}
export default defineConfig(async () => {
const recentPullRequests =
process.env.VITE_BUILD_RECENT_PRS ?? (await cloudflareRecentPullRequests());
return {
build: {
chunkSizeWarningLimit: 1000,
},
define: {
'import.meta.env.RIDE_CONTROL_BUILD_PR_URL': JSON.stringify(buildPrUrl),
'import.meta.env.RIDE_CONTROL_BUILD_RECENT_PRS': JSON.stringify(recentPullRequests),
'import.meta.env.RIDE_CONTROL_BUILD_TIMESTAMP_UTC': JSON.stringify(buildTimestampUtc),
},
plugins: [deploymentVersionPlugin(buildTimestampUtc), react(), tailwindcss(), cloudflare()],
};
});