-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
100 lines (90 loc) · 3.1 KB
/
Copy pathvite.config.js
File metadata and controls
100 lines (90 loc) · 3.1 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
import { readdirSync, readFileSync, writeFileSync, existsSync, cpSync, mkdirSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { defineConfig } from 'vite';
const root = resolve(import.meta.dirname);
const games = Object.fromEntries(
readdirSync(resolve(root, 'games'))
.filter((dir) => existsSync(resolve(root, 'games', dir, 'index.html')))
.map((dir) => [dir, resolve(root, 'games', dir, 'index.html')]),
);
export default defineConfig({
base: './',
server: {
port: 5173,
open: '/',
},
plugins: [
{
name: 'copy-dist-assets',
closeBundle() {
const dist = resolve(root, 'dist');
const engineDir = resolve(root, 'engine');
const distEngine = resolve(dist, 'engine');
if (!existsSync(distEngine)) mkdirSync(distEngine, { recursive: true });
for (const f of readdirSync(engineDir)) {
if (f.endsWith('.min.js')) {
cpSync(resolve(engineDir, f), resolve(distEngine, f));
}
}
const manifestPath = resolve(root, 'public', 'games.json');
if (!existsSync(manifestPath)) return;
const manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'));
// Fix base files for production (remove non-existent source files)
manifest.cache.gameBaseFiles = ["index.html"];
const seen = new Set();
for (const game of manifest.games || []) {
// 1. Parse built HTML to find hashed assets
const gameHtmlPath = resolve(dist, game.path, 'index.html');
const gameAssets = [];
if (existsSync(gameHtmlPath)) {
const html = readFileSync(gameHtmlPath, 'utf-8');
// Find all href=".../assets/..." and src=".../assets/..."
const regex = /(?:href|src)="(\.\.\/\.\.\/assets\/[^"]+)"/g;
let match;
while ((match = regex.exec(html)) !== null) {
// Convert ../../assets/foo.js to assets/foo.js
gameAssets.push(match[1].replace('../../', ''));
}
}
// 2. Add found assets to extraCacheFiles
if (!game.extraCacheFiles) game.extraCacheFiles = [];
game.extraCacheFiles.push(...gameAssets);
// 3. Process and copy all extraCacheFiles
for (const file of game.extraCacheFiles) {
if (seen.has(file)) continue;
seen.add(file);
// If it's a hashed asset, it's already in dist/assets/, we don't need to copy from src
if (file.startsWith('assets/')) continue;
const src = resolve(root, file);
const dst = resolve(dist, file);
if (existsSync(src)) {
if (!existsSync(dirname(dst))) mkdirSync(dirname(dst), { recursive: true });
cpSync(src, dst);
}
}
}
// Save the updated games.json to dist/ so the PWA fetch can see the hashed assets
writeFileSync(resolve(dist, 'games.json'), JSON.stringify(manifest, null, 2));
},
},
],
build: {
target: 'es2020',
minify: 'esbuild',
cssMinify: true,
sourcemap: false,
reportCompressedSize: true,
chunkSizeWarningLimit: 600,
rollupOptions: {
input: {
main: resolve(root, 'index.html'),
...games,
},
output: {
manualChunks(id) {
if (id.includes('node_modules/peerjs')) return 'peerjs';
},
},
},
},
});