-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvite.config.ts
More file actions
225 lines (215 loc) · 6.37 KB
/
vite.config.ts
File metadata and controls
225 lines (215 loc) · 6.37 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
import { defineConfig } from 'vite';
import type { ViteDevServer, PluginOption } from 'vite';
import { execSync } from 'child_process';
import react from '@vitejs/plugin-react';
import svgr from 'vite-plugin-svgr';
import { wasm } from '@rollup/plugin-wasm';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
import inject from '@rollup/plugin-inject';
import topLevelAwait from 'vite-plugin-top-level-await';
import { VitePWA } from 'vite-plugin-pwa';
import { compression, defineAlgorithm } from 'vite-plugin-compression2';
import { constants as zlibConstants } from 'zlib';
import fs from 'fs';
import path from 'path';
import { cloudflare } from '@cloudflare/vite-plugin';
import { createRequire } from 'module';
import buildConfig from './build.config';
const packageJson = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf8')
) as {
version: string;
};
const normalizeShortSha = (value?: string): string | undefined => {
if (!value) return undefined;
const normalized = value.trim();
if (!normalized) return undefined;
return normalized.slice(0, 7);
};
const resolveBuildHash = (): string | undefined => {
const envHash = normalizeShortSha(
process.env.VITE_BUILD_HASH ??
process.env.GITHUB_SHA ??
process.env.CI_COMMIT_SHA ??
process.env.SOURCE_VERSION
);
if (envHash) return envHash;
try {
return normalizeShortSha(execSync('git rev-parse --short HEAD').toString('utf8'));
} catch {
return undefined;
}
};
const appVersion = packageJson.version;
const buildHash = resolveBuildHash();
const isReleaseTag = (() => {
const envVal = process.env.VITE_IS_RELEASE_TAG;
if (envVal !== undefined && envVal !== '') return envVal === 'true';
try {
const tag = execSync('git describe --exact-match --tags HEAD 2>/dev/null').toString().trim();
return tag.startsWith('v');
} catch {
return false;
}
})();
const copyFiles = {
targets: [
{
src: 'node_modules/@element-hq/element-call-embedded/dist/*',
dest: 'public/element-call',
},
{
src: 'node_modules/pdfjs-dist/build/pdf.worker.min.mjs',
dest: '',
rename: 'pdf.worker.min.js',
},
{
src: 'config.json',
dest: '',
},
{
src: 'public/manifest.json',
dest: '',
},
{
src: 'public/res/android',
dest: 'public/',
},
{
src: 'public/locales',
dest: 'public/',
},
],
};
const require = createRequire(import.meta.url);
function serverMatrixSdkCryptoWasm() {
return {
name: 'vite-plugin-serve-matrix-sdk-crypto-wasm',
configureServer(server: ViteDevServer) {
const resolvedPath = path.join(
path.dirname(require.resolve('@matrix-org/matrix-sdk-crypto-wasm')),
'pkg/matrix_sdk_crypto_wasm_bg.wasm'
);
server.middlewares.use((req, res, next) => {
if (!req.url?.endsWith('matrix_sdk_crypto_wasm_bg.wasm')) {
next();
return;
}
res.setHeader('Content-Type', 'application/wasm');
res.setHeader('Cache-Control', 'no-cache');
fs.createReadStream(resolvedPath).pipe(res);
});
},
};
}
export default defineConfig({
appType: 'spa',
publicDir: false,
base: buildConfig.base,
define: {
APP_VERSION: JSON.stringify(appVersion),
BUILD_HASH: JSON.stringify(buildHash ?? ''),
IS_RELEASE_TAG: JSON.stringify(isReleaseTag),
},
resolve: {
alias: {
$hooks: path.resolve(__dirname, 'src/app/hooks'),
$plugins: path.resolve(__dirname, 'src/app/plugins'),
$components: path.resolve(__dirname, 'src/app/components'),
$features: path.resolve(__dirname, 'src/app/features'),
$state: path.resolve(__dirname, 'src/app/state'),
$styles: path.resolve(__dirname, 'src/app/styles'),
$utils: path.resolve(__dirname, 'src/app/utils'),
$pages: path.resolve(__dirname, 'src/app/pages'),
$types: path.resolve(__dirname, 'src/types'),
$public: path.resolve(__dirname, 'public'),
$client: path.resolve(__dirname, 'src/client'),
},
},
server: {
port: 8080,
host: true,
fs: {
// Allow serving files from one level up to the project root
allow: ['..'],
},
},
plugins: [
serverMatrixSdkCryptoWasm(),
topLevelAwait({
// The export name of top-level await promise for each chunk module
promiseExportName: '__tla',
// The function to generate import names of top-level await promise in each chunk module
promiseImportName: (i) => `__tla_${i}`,
}),
viteStaticCopy(copyFiles),
vanillaExtractPlugin({ identifiers: 'debug' }),
wasm() as PluginOption,
react(),
svgr(),
VitePWA({
srcDir: 'src',
filename: 'sw.ts',
strategies: 'injectManifest',
injectRegister: false,
manifest: false,
injectManifest: {
injectionPoint: undefined,
},
devOptions: {
enabled: true,
type: 'module',
},
}),
cloudflare({
config: {
compatibility_date: '2026-03-03',
assets: {
not_found_handling: 'single-page-application',
},
},
}),
compression({
algorithms: [
defineAlgorithm('brotliCompress', {
params: { [zlibConstants.BROTLI_PARAM_QUALITY]: zlibConstants.BROTLI_MAX_QUALITY },
}),
],
include: /\.(html|xml|css|json|js|mjs|svg|yaml|yml|toml|wasm|txt|map)$/,
}),
],
optimizeDeps: {
// Rebuild dep optimizer cache on each dev start to avoid stale API shapes.
force: true,
// Keep matrix-widget-api prebundled so matrix-js-sdk can import its named exports in dev.
// Force CJS interop for stability across optimizer cache rebuilds.
include: [
'matrix-widget-api',
'workbox-precaching',
'@vanilla-extract/recipes/createRuntimeFn',
],
needsInterop: ['matrix-widget-api'],
esbuildOptions: {
define: {
global: 'globalThis',
},
plugins: [
// Enable esbuild polyfill plugins
NodeGlobalsPolyfillPlugin({
process: false,
buffer: true,
}),
],
},
},
build: {
outDir: 'dist',
sourcemap: true,
copyPublicDir: false,
rollupOptions: {
plugins: [inject({ Buffer: ['buffer', 'Buffer'] }) as PluginOption],
},
},
});