vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /// <reference types="vitest/config" />
  2. import { writeFileSync } from "node:fs"
  3. import tailwindcss from "@tailwindcss/vite"
  4. import react from "@vitejs/plugin-react-swc"
  5. import { resolve } from "path"
  6. import { defineConfig, type Plugin, ViteDevServer } from "vite"
  7. // Custom plugin to write the server port to a file
  8. const writePortToFile = (): Plugin => {
  9. return {
  10. name: "write-port-to-file",
  11. configureServer(server: ViteDevServer) {
  12. server.httpServer?.once("listening", () => {
  13. const address = server.httpServer?.address()
  14. const port = typeof address === "object" && address ? address.port : null
  15. if (port) {
  16. const portFilePath = resolve(__dirname, ".vite-port")
  17. writeFileSync(portFilePath, port.toString())
  18. } else {
  19. console.warn("[writePortToFile] Could not determine server port")
  20. }
  21. })
  22. },
  23. }
  24. }
  25. const isDevBuild = process.argv.includes("--dev-build")
  26. // Valid platforms, these should the keys in platform-configs.json
  27. const VALID_PLATFORMS = ["vscode", "standalone"]
  28. const platform = process.env.PLATFORM || "vscode" // Default to vscode
  29. if (!VALID_PLATFORMS.includes(platform)) {
  30. throw new Error(`Invalid PLATFORM "${platform}". Must be one of: ${VALID_PLATFORMS.join(", ")}`)
  31. }
  32. console.log("Building webview for", platform)
  33. export default defineConfig({
  34. plugins: [react(), tailwindcss(), writePortToFile()],
  35. test: {
  36. environment: "jsdom",
  37. globals: true,
  38. setupFiles: ["./src/setupTests.ts"],
  39. coverage: {
  40. provider: "v8",
  41. reportOnFailure: true,
  42. reporter: ["html", "lcov", "text"],
  43. reportsDirectory: "./coverage",
  44. exclude: [
  45. "**/*.{spec,test}.{js,jsx,ts,tsx,mjs,cjs}",
  46. "**/*.d.ts",
  47. "**/vite-env.d.ts",
  48. "**/*.{config,setup}.{js,ts,mjs,cjs}",
  49. "**/*.{css,scss,sass,less,styl}",
  50. "**/*.{svg,png,jpg,jpeg,gif,ico}",
  51. "**/*.{json,yaml,yml}",
  52. "**/__mocks__/**",
  53. "node_modules/**",
  54. "build/**",
  55. "coverage/**",
  56. "dist/**",
  57. "public/**",
  58. "src/services/grpc-client.ts",
  59. ],
  60. },
  61. },
  62. build: {
  63. outDir: "build",
  64. reportCompressedSize: false,
  65. // Only minify in production build
  66. minify: !isDevBuild,
  67. // Enable inline source maps for dev build
  68. sourcemap: isDevBuild ? "inline" : false,
  69. rollupOptions: {
  70. output: {
  71. inlineDynamicImports: true,
  72. entryFileNames: `assets/[name].js`,
  73. chunkFileNames: `assets/[name].js`,
  74. assetFileNames: `assets/[name].[ext]`,
  75. // Disable compact output for dev build
  76. compact: !isDevBuild,
  77. // Add generous formatting for dev build
  78. ...(isDevBuild && {
  79. generatedCode: {
  80. constBindings: false,
  81. objectShorthand: false,
  82. arrowFunctions: false,
  83. },
  84. }),
  85. },
  86. },
  87. chunkSizeWarningLimit: 100000,
  88. },
  89. server: {
  90. port: 25463,
  91. hmr: {
  92. host: "localhost",
  93. protocol: "ws",
  94. },
  95. cors: {
  96. origin: "*",
  97. methods: "*",
  98. allowedHeaders: "*",
  99. },
  100. },
  101. define: {
  102. __PLATFORM__: JSON.stringify(platform),
  103. process: JSON.stringify({
  104. platform: JSON.stringify(process?.platform),
  105. env: {
  106. NODE_ENV: JSON.stringify(process?.env?.IS_DEV ? "development" : "production"),
  107. CLINE_ENVIRONMENT: JSON.stringify(process?.env?.CLINE_ENVIRONMENT ?? "production"),
  108. IS_DEV: JSON.stringify(process?.env?.IS_DEV),
  109. IS_TEST: JSON.stringify(process?.env?.IS_TEST),
  110. CI: JSON.stringify(process?.env?.CI),
  111. // PostHog environment variables
  112. TELEMETRY_SERVICE_API_KEY: JSON.stringify(process?.env?.TELEMETRY_SERVICE_API_KEY),
  113. ERROR_SERVICE_API_KEY: JSON.stringify(process?.env?.ERROR_SERVICE_API_KEY),
  114. },
  115. }),
  116. },
  117. resolve: {
  118. alias: {
  119. "@": resolve(__dirname, "./src"),
  120. "@components": resolve(__dirname, "./src/components"),
  121. "@context": resolve(__dirname, "./src/context"),
  122. "@shared": resolve(__dirname, "../src/shared"),
  123. "@utils": resolve(__dirname, "./src/utils"),
  124. },
  125. },
  126. })