-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
296 lines (279 loc) · 9.55 KB
/
Copy pathvite.config.ts
File metadata and controls
296 lines (279 loc) · 9.55 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { fileURLToPath, URL } from 'url';
import { createHash } from 'crypto';
import { execSync } from 'child_process';
import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs';
import path from 'path';
const projectRoot = fileURLToPath(new URL('.', import.meta.url));
const ootmmCoreRoot = fileURLToPath(
new URL('./OoTMM/packages/core/src', import.meta.url),
);
const publicImagesRoot = fileURLToPath(
new URL('./public/images', import.meta.url),
);
const publicImageExtensions = new Set([
'.avif',
'.gif',
'.jpg',
'.jpeg',
'.png',
'.svg',
'.webp',
]);
// NOTE: @ootmm/core/* modules are intentionally NOT listed in
// `optimizeDeps.include`. They are resolved by the aliases below to plain
// TypeScript sources inside ./OoTMM, so pre-bundling them would freeze a
// snapshot of the OoTMM sources in node_modules/.vite that is never
// invalidated when the OoTMM checkout changes (the OoTMM settings, items and
// logic data change on every OoTMM version bump, e.g. new shuffle settings).
// Importing them as regular source keeps dev and build always in sync with
// the checked-out OoTMM revision.
function isPublicImagePath(filePath: string): boolean {
return (
filePath.startsWith(publicImagesRoot) &&
publicImageExtensions.has(path.extname(filePath).toLowerCase())
);
}
function appendCurrentPublicImageVersion(url: string): string {
const publicImageMatch = /^(\/|\.\/)?(images\/.*)$/.exec(url);
if (!publicImageMatch) {
return url;
}
const prefix = publicImageMatch[1] ?? '';
const publicPath = publicImageMatch[2];
const hashIndex = publicPath.indexOf('#');
const hash = hashIndex >= 0 ? publicPath.slice(hashIndex) : '';
const urlWithoutHash =
hashIndex >= 0 ? publicPath.slice(0, hashIndex) : publicPath;
const queryIndex = urlWithoutHash.indexOf('?');
const pathname =
queryIndex >= 0 ? urlWithoutHash.slice(0, queryIndex) : urlWithoutHash;
const query = queryIndex >= 0 ? urlWithoutHash.slice(queryIndex) : '';
if (query && new URLSearchParams(query.slice(1)).has('v')) {
return url;
}
const filePath = fileURLToPath(
new URL(`./public/${pathname}`, import.meta.url),
);
if (!existsSync(filePath)) {
return url;
}
const version = createHash('sha256')
.update(readFileSync(filePath))
.digest('hex')
.slice(0, 12);
return `${prefix}${pathname}${query}${query ? '&' : '?'}v=${version}${hash}`;
}
function publicImageAssetVersionPlugin() {
return {
name: 'tlt-public-image-asset-versions',
configureServer(server) {
server.watcher.add(publicImagesRoot);
server.watcher.on('all', (_event, filePath) => {
if (!isPublicImagePath(filePath)) {
return;
}
try {
execSync('node scripts/generate_public_image_asset_versions.ts', {
cwd: projectRoot,
stdio: 'inherit',
});
} catch (error) {
console.warn(
'Failed to regenerate public image asset versions',
error,
);
}
server.ws.send({ type: 'full-reload' });
});
},
transformIndexHtml: {
order: 'post',
handler(html) {
return html.replace(
/(?:\/|\.\/)?images\/[^"'()\s]+/g,
appendCurrentPublicImageVersion,
);
},
},
generateBundle(_options, bundle) {
for (const asset of Object.values(bundle)) {
if (asset.type !== 'asset' || !asset.fileName.endsWith('.html')) {
continue;
}
const source =
typeof asset.source === 'string'
? asset.source
: Buffer.from(asset.source).toString('utf8');
asset.source = source.replace(
/(?:\/|\.\/)?images\/[^"'()\s]+/g,
appendCurrentPublicImageVersion,
);
}
},
writeBundle(options) {
const outputDirectory = path.resolve(projectRoot, options.dir ?? 'dist');
const indexHtmlPath = path.join(outputDirectory, 'index.html');
if (!existsSync(indexHtmlPath)) {
return;
}
const html = readFileSync(indexHtmlPath, 'utf8');
const versionedHtml = html.replace(
/(?:\/|\.\/)?images\/[^"'()\s]+/g,
appendCurrentPublicImageVersion,
);
if (versionedHtml !== html) {
writeFileSync(indexHtmlPath, versionedHtml, 'utf8');
}
},
};
}
// BusinessAlex's map-icon and song-event assets are opt-in. Both the restricted
// and the MIT fallback sets are committed under public/images/ and copied into
// dist/ by Vite. After a build we prune whichever set is NOT active so the built
// output only serves the assets it is licensed to serve. (Dev `vite serve` reads
// public/ directly and never copies it, so nothing to prune there.)
function assetSetPruningPlugin(useRestrictedAssets: boolean) {
const inactiveDirs = useRestrictedAssets
? ['images/fallback']
: ['images/map_icons', 'images/song_events'];
return {
name: 'tlt-asset-set-pruning',
apply: 'build' as const,
writeBundle(options: { dir?: string }) {
const outputDirectory = path.resolve(projectRoot, options.dir ?? 'dist');
for (const relativeDir of inactiveDirs) {
const target = path.join(outputDirectory, relativeDir);
if (existsSync(target)) {
rmSync(target, { recursive: true, force: true });
}
}
},
};
}
function readGitMetadata(
command: string,
fallback: string,
description: string,
): string {
try {
const value = execSync(command, {
cwd: projectRoot,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
}).trim();
if (value) {
return value;
}
console.warn(`Read empty ${description}; using "${fallback}"`);
return fallback;
} catch {
console.warn(`Failed to read ${description}; using "${fallback}"`);
return fallback;
}
}
const buildCommitDate = readGitMetadata(
'git log -1 --format=%cs',
'unknown',
'TLT build commit date',
);
const buildCommitHash = readGitMetadata(
'git rev-parse --short HEAD',
'unknown',
'TLT build commit hash',
);
const ootmmVersionTag =
readGitMetadata(
'git -C OoTMM tag --merged HEAD --sort=-version:refname',
'unknown',
'OoTMM version tag',
)
.split('\n')
.map((tag) => tag.trim())
.find((tag) => tag.length > 0) ?? 'unknown';
export default defineConfig(({ mode }) => {
// Reads shell env (process.env, e.g. `FLAG=TRUE npm run build`) and .env files
// (loadEnv with an empty prefix also picks up non-VITE_ vars, e.g. a gitignored
// .env.local). Only the exact value TRUE opts into the restricted assets.
const env = loadEnv(mode, projectRoot, '');
const useRestrictedAssets =
(process.env.I_HAVE_ASKED_BUSINESSALEX_FOR_PERMISSION_FOR_THE_IMAGE_FILES ??
env.I_HAVE_ASKED_BUSINESSALEX_FOR_PERMISSION_FOR_THE_IMAGE_FILES) ===
'TRUE';
console.log(
useRestrictedAssets
? '[tlt] Map-icon/song-event assets: RESTRICTED set (BusinessAlex; permission asserted via I_HAVE_ASKED_BUSINESSALEX_FOR_PERMISSION_FOR_THE_IMAGE_FILES=TRUE).'
: '[tlt] Map-icon/song-event assets: MIT fallback set (default). Set I_HAVE_ASKED_BUSINESSALEX_FOR_PERMISSION_FOR_THE_IMAGE_FILES=TRUE only if you have permission to use the restricted assets.',
);
return {
// Use relative asset paths so built files work from any subfolder.
base: './',
plugins: [
publicImageAssetVersionPlugin(),
assetSetPruningPlugin(useRestrictedAssets),
vue(),
],
resolve: {
extensions: ['.ts', '.tsx', '.mjs', '.js', '.jsx', '.json'],
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@packs/ootmm': fileURLToPath(
new URL('./packs/ootmm/src', import.meta.url),
),
'@ootmm/data': fileURLToPath(
new URL('./scripts/ootmm_data_bridge.ts', import.meta.url),
),
'@ootmm/core/settings': fileURLToPath(
new URL('./OoTMM/packages/core/src/settings', import.meta.url),
),
'@ootmm/core/items': fileURLToPath(
new URL('./OoTMM/packages/core/src/items', import.meta.url),
),
'@ootmm/core/logic/entrance': fileURLToPath(
new URL(
'./OoTMM/packages/logic/src/solver/entrances.ts',
import.meta.url,
),
),
'@ootmm/core/logic/is-shuffled': fileURLToPath(
new URL('./OoTMM/packages/logic/src/helpers.ts', import.meta.url),
),
'@ootmm/core/logic': fileURLToPath(
new URL('./OoTMM/packages/logic/src', import.meta.url),
),
'@ootmm/core/monitor': fileURLToPath(
new URL('./OoTMM/packages/core/src/monitor.ts', import.meta.url),
),
'@ootmm/core/names': fileURLToPath(
new URL(
'./OoTMM/packages/generator/lib/combo/names.ts',
import.meta.url,
),
),
'@ootmm/core': ootmmCoreRoot,
},
},
define: {
'process.env.VERSION': JSON.stringify('dev'),
'process.env.__IS_BROWSER__': JSON.stringify(true),
__TLT_BUILD_COMMIT_DATE__: JSON.stringify(buildCommitDate),
__TLT_BUILD_COMMIT_HASH__: JSON.stringify(buildCommitHash),
__TLT_OOTMM_VERSION_TAG__: JSON.stringify(ootmmVersionTag),
__TLT_USE_RESTRICTED_ASSETS__: JSON.stringify(useRestrictedAssets),
},
server: {
proxy: {
'/coop/ws': {
target: 'ws://127.0.0.1:8765',
ws: true,
rewrite: () => '/',
},
'/coop/healthz': {
target: 'http://127.0.0.1:8765',
rewrite: () => '/healthz',
},
},
},
};
});