-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
51 lines (46 loc) · 1.35 KB
/
vite.config.ts
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
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import copy from 'rollup-plugin-copy';
import fs from 'fs';
import path from 'path';
// WASM content type plugin
const wasmContentTypePlugin = {
name: 'wasm-content-type-plugin',
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url.endsWith('.wasm')) {
res.setHeader('Content-Type', 'application/wasm');
const newPath = req.url.replace('deps', 'dist');
const targetPath = path.join(__dirname, newPath);
const wasmContent = fs.readFileSync(targetPath);
return res.end(wasmContent);
}
next();
});
},
};
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
const basePlugins = [react()];
if (command === 'serve') {
// Add the WASM content type plugin only for the 'serve' command
basePlugins.push(wasmContentTypePlugin);
}
// Copy plugin is added regardless of the command
basePlugins.push(
copy({
targets: [{ src: 'node_modules/**/*.wasm', dest: 'node_modules/.vite/dist' }],
copySync: true,
hook: 'buildStart',
})
);
return {
plugins: basePlugins,
resolve: {
alias: {
buffer: 'buffer',
},
},
// ... you can spread other shared or environment-specific configurations here
};
});