-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.mjs
More file actions
129 lines (118 loc) · 3.92 KB
/
vite.config.mjs
File metadata and controls
129 lines (118 loc) · 3.92 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
import { defineConfig } from 'vite';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import process from 'process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Plugin to inject modulepreload hints for game module chunks.
* That allows the browser to preload game modules in parallel during booting the game.
* @returns {import('vite').Plugin} Vite plugin
*/
function gameModulePreloadPlugin() {
return {
name: 'game-module-preload',
transformIndexHtml: {
order: 'post',
handler(html, { bundle }) {
if (!bundle) {
return html;
}
// Find all chunks that are game modules (from GameLoader)
const gameModuleChunks = Object.values(bundle).filter(
(chunk) =>
chunk.type === 'chunk' &&
chunk.facadeModuleId?.includes('/game/') &&
chunk.facadeModuleId?.endsWith('/main.mjs') &&
chunk.facadeModuleId?.endsWith('Worker.mjs'),
);
if (gameModuleChunks.length === 0) {
return html;
}
const preloadLinks = gameModuleChunks
.map((chunk) => `\t\t<link rel="modulepreload" href="/${chunk.fileName}" />`)
.join('\n');
return html.replace('</head>', `\n${preloadLinks}\n\t</head>`);
},
},
};
}
export default defineConfig(({ mode }) => ({
esbuild: {
drop: mode === 'production' ? ['debugger'] : [],
pure: mode === 'production' ? ['console.log', 'console.debug', 'console.info', 'console.assert', 'console.trace'] : [],
},
build: {
outDir: resolve(__dirname, 'dist/browser'),
emptyOutDir: true,
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
},
output: {
entryFileNames: 'libs/[name]-[hash].js',
chunkFileNames: 'libs/[name]-[hash].js',
assetFileNames: 'libs/[name]-[hash][extname]',
manualChunks(id) {
// bundle shared code into a single chunk
if (id.includes('/source/shared/')) {
return 'shared';
}
// vendor packages from node_modules
if (id.includes('/node_modules/')) {
return 'vendor';
}
// keep game modules as separate chunks (they are dynamically loaded by the GameModule runtime)
if (id.includes('/source/game/')) {
// extract the gamedir name
const gameMatch = id.match(/\/source\/game\/([^/]+)\//);
if (gameMatch) {
return `game-${gameMatch[1]}`;
}
}
// anything else
return null;
},
},
},
copyPublicDir: true,
sourcemap: mode !== 'production',
chunkSizeWarningLimit: 1000,
minify: mode === 'production' ? 'esbuild' : false,
reportCompressedSize: true,
target: 'es2022',
},
worker: {
format: 'es',
rollupOptions: {
output: {
entryFileNames: 'libs/worker-[name]-[hash].js',
chunkFileNames: 'libs/worker-[name]-[hash].js',
},
},
},
plugins: [
gameModulePreloadPlugin(),
],
define: {
'__BUILD_SIGNALING_URL__': JSON.stringify(
process.env.VITE_SIGNALING_URL || '',
),
'__BUILD_CDN_URL_PATTERN__': JSON.stringify(
process.env.VITE_CDN_URL_PATTERN || '',
),
'__BUILD_MODE__': JSON.stringify(mode),
'__BUILD_TIMESTAMP__': JSON.stringify(new Date().toISOString()),
'__BUILD_COMMIT_HASH__': JSON.stringify(process.env.WORKERS_CI_COMMIT_SHA?.substring(0, 7) || null),
'__BUILD_GAME_DIR__': JSON.stringify(process.env.VITE_GAME_DIR || null),
'__BUILD_BASE_DIR__': JSON.stringify(process.env.VITE_BASE_DIR || null),
'__DEV__': JSON.stringify(mode !== 'production'),
},
resolve: {
alias: {
'@': resolve(__dirname, 'source'),
},
extensions: ['.ts', '.mts', '.mjs', '.js', '.json'],
preserveSymlinks: process.env.VITE_PRESERVE_SYMLINKS === 'true',
},
}));