vite.config.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { resolve } from "path"
  2. import fs from "fs"
  3. import { defineConfig, type Plugin } from "vite"
  4. import react from "@vitejs/plugin-react"
  5. import tailwindcss from "@tailwindcss/vite"
  6. function wasmPlugin(): Plugin {
  7. return {
  8. name: "wasm",
  9. async load(id: string) {
  10. if (id.endsWith(".wasm")) {
  11. const wasmBinary = await import(id)
  12. return `
  13. const wasmModule = new WebAssembly.Module(${wasmBinary.default});
  14. export default wasmModule;
  15. `
  16. }
  17. },
  18. }
  19. }
  20. // Custom plugin to write the server port to a file
  21. const writePortToFile = () => {
  22. return {
  23. name: "write-port-to-file",
  24. configureServer(server) {
  25. // Write the port to a file when the server starts
  26. server.httpServer?.once("listening", () => {
  27. const address = server.httpServer.address()
  28. const port = typeof address === "object" && address ? address.port : null
  29. if (port) {
  30. // Write to a file in the project root
  31. const portFilePath = resolve(__dirname, "../.vite-port")
  32. fs.writeFileSync(portFilePath, port.toString())
  33. console.log(`[Vite Plugin] Server started on port ${port}`)
  34. console.log(`[Vite Plugin] Port information written to ${portFilePath}`)
  35. } else {
  36. console.warn("[Vite Plugin] Could not determine server port")
  37. }
  38. })
  39. },
  40. }
  41. }
  42. // https://vitejs.dev/config/
  43. export default defineConfig({
  44. plugins: [react(), tailwindcss(), writePortToFile(), wasmPlugin()],
  45. resolve: {
  46. alias: {
  47. "@": resolve(__dirname, "./src"),
  48. "@src": resolve(__dirname, "./src"),
  49. "@roo": resolve(__dirname, "../src"),
  50. },
  51. },
  52. build: {
  53. outDir: "build",
  54. reportCompressedSize: false,
  55. rollupOptions: {
  56. output: {
  57. entryFileNames: `assets/[name].js`,
  58. chunkFileNames: `assets/[name].js`,
  59. assetFileNames: `assets/[name].[ext]`,
  60. },
  61. },
  62. },
  63. server: {
  64. hmr: {
  65. host: "localhost",
  66. protocol: "ws",
  67. },
  68. cors: {
  69. origin: "*",
  70. methods: "*",
  71. allowedHeaders: "*",
  72. },
  73. },
  74. define: {
  75. "process.platform": JSON.stringify(process.platform),
  76. "process.env.VSCODE_TEXTMATE_DEBUG": JSON.stringify(process.env.VSCODE_TEXTMATE_DEBUG),
  77. },
  78. optimizeDeps: {
  79. exclude: ["@vscode/codicons", "vscode-oniguruma", "shiki"],
  80. },
  81. assetsInclude: ["**/*.wasm"],
  82. })