Files

132 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2022-12-20 20:47:51 +05:30
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { wasm } from '@rollup/plugin-wasm';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
2023-01-30 15:20:53 +11:00
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
import inject from '@rollup/plugin-inject';
import topLevelAwait from 'vite-plugin-top-level-await';
2024-09-07 21:45:55 +08:00
import { VitePWA } from 'vite-plugin-pwa';
2025-05-18 10:53:56 +05:30
import fs from 'fs';
import path from 'path';
import buildConfig from './build.config';
2022-12-20 20:47:51 +05:30
const copyFiles = {
targets: [
2023-10-06 13:44:06 +11:00
{
2024-05-12 16:14:34 +10:00
src: 'node_modules/pdfjs-dist/build/pdf.worker.min.mjs',
2023-10-06 13:44:06 +11:00
dest: '',
2024-05-12 16:14:34 +10:00
rename: 'pdf.worker.min.js',
2023-10-06 13:44:06 +11:00
},
2022-12-20 20:47:51 +05:30
{
2024-01-21 23:50:56 +11:00
src: 'netlify.toml',
2022-12-20 20:47:51 +05:30
dest: '',
},
{
src: 'config.json',
dest: '',
},
{
src: 'public/manifest.json',
dest: '',
},
2022-12-20 20:47:51 +05:30
{
src: 'public/res/android',
dest: 'public/',
},
{
src: 'public/locales',
dest: 'public/',
},
2022-12-20 20:47:51 +05:30
],
};
2022-12-20 20:47:51 +05:30
2025-05-18 10:53:56 +05:30
function serverMatrixSdkCryptoWasm(wasmFilePath) {
return {
name: 'vite-plugin-serve-matrix-sdk-crypto-wasm',
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url === wasmFilePath) {
const resolvedPath = path.join(path.resolve(), "/node_modules/@matrix-org/matrix-sdk-crypto-wasm/pkg/matrix_sdk_crypto_wasm_bg.wasm");
if (fs.existsSync(resolvedPath)) {
res.setHeader('Content-Type', 'application/wasm');
res.setHeader('Cache-Control', 'no-cache');
const fileStream = fs.createReadStream(resolvedPath);
fileStream.pipe(res);
} else {
res.writeHead(404);
res.end('File not found');
}
} else {
next();
}
});
},
};
}
2022-12-20 20:47:51 +05:30
export default defineConfig({
appType: 'spa',
publicDir: false,
2024-01-21 23:50:56 +11:00
base: buildConfig.base,
2022-12-20 20:47:51 +05:30
server: {
port: 8080,
host: true,
2025-05-18 10:53:56 +05:30
fs: {
// Allow serving files from one level up to the project root
allow: ['..'],
},
2022-12-20 20:47:51 +05:30
},
plugins: [
2025-05-18 10:53:56 +05:30
serverMatrixSdkCryptoWasm('/node_modules/.vite/deps/pkg/matrix_sdk_crypto_wasm_bg.wasm'),
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}`,
}),
2022-12-20 20:47:51 +05:30
viteStaticCopy(copyFiles),
2023-06-12 21:15:23 +10:00
vanillaExtractPlugin(),
2022-12-20 20:47:51 +05:30
wasm(),
react(),
2024-09-07 21:45:55 +08:00
VitePWA({
srcDir: 'src',
filename: 'sw.ts',
strategies: 'injectManifest',
injectRegister: false,
manifest: false,
injectManifest: {
injectionPoint: undefined,
},
2024-09-09 18:45:20 +10:00
devOptions: {
enabled: true,
type: 'module'
}
2024-09-07 21:45:55 +08:00
}),
2022-12-20 20:47:51 +05:30
],
2023-01-30 15:20:53 +11:00
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
},
plugins: [
// Enable esbuild polyfill plugins
NodeGlobalsPolyfillPlugin({
process: false,
buffer: true,
}),
],
},
2023-01-30 15:20:53 +11:00
},
2022-12-20 20:47:51 +05:30
build: {
outDir: 'dist',
sourcemap: true,
copyPublicDir: false,
2023-01-30 15:20:53 +11:00
rollupOptions: {
plugins: [inject({ Buffer: ['buffer', 'Buffer'] })],
},
2022-12-20 20:47:51 +05:30
},
});