vite.config.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import path, { resolve } from "path"
  2. import fs from "fs"
  3. import { execSync } from "child_process"
  4. import { defineConfig, type PluginOption, type Plugin } from "vite"
  5. import react from "@vitejs/plugin-react"
  6. import tailwindcss from "@tailwindcss/vite"
  7. import { sourcemapPlugin } from "./src/vite-plugins/sourcemapPlugin"
  8. function getGitSha() {
  9. let gitSha: string | undefined = undefined
  10. try {
  11. gitSha = execSync("git rev-parse HEAD").toString().trim()
  12. } catch (_error) {
  13. // Do nothing.
  14. }
  15. return gitSha
  16. }
  17. const wasmPlugin = (): Plugin => ({
  18. name: "wasm",
  19. async load(id) {
  20. if (id.endsWith(".wasm")) {
  21. const wasmBinary = await import(id)
  22. return `
  23. const wasmModule = new WebAssembly.Module(${wasmBinary.default});
  24. export default wasmModule;
  25. `
  26. }
  27. },
  28. })
  29. const persistPortPlugin = (): Plugin => ({
  30. name: "write-port-to-file",
  31. configureServer(viteDevServer) {
  32. viteDevServer?.httpServer?.once("listening", () => {
  33. const address = viteDevServer?.httpServer?.address()
  34. const port = address && typeof address === "object" ? address.port : null
  35. if (port) {
  36. fs.writeFileSync(resolve(__dirname, "..", ".vite-port"), port.toString())
  37. console.log(`[Vite Plugin] Server started on port ${port}`)
  38. } else {
  39. console.warn("[Vite Plugin] Could not determine server port")
  40. }
  41. })
  42. },
  43. })
  44. // https://vitejs.dev/config/
  45. export default defineConfig(({ mode }) => {
  46. let outDir = "../src/webview-ui/build"
  47. // kilocode_change start - read package.json fresh every time to avoid caching issues
  48. const getPkg = () => {
  49. try {
  50. return JSON.parse(fs.readFileSync(path.join(__dirname, "..", "src", "package.json"), "utf8"))
  51. } catch (error) {
  52. throw new Error(`Could not read package.json: ${error}`)
  53. }
  54. }
  55. const pkg = getPkg()
  56. // kilocode_change end
  57. const gitSha = getGitSha()
  58. const define: Record<string, any> = {
  59. "process.platform": JSON.stringify(process.platform),
  60. "process.env.VSCODE_TEXTMATE_DEBUG": JSON.stringify(process.env.VSCODE_TEXTMATE_DEBUG),
  61. "process.env.PKG_NAME": JSON.stringify(pkg.name),
  62. "process.env.PKG_VERSION": JSON.stringify(pkg.version),
  63. "process.env.PKG_OUTPUT_CHANNEL": JSON.stringify("Kilo-Code"),
  64. ...(gitSha ? { "process.env.PKG_SHA": JSON.stringify(gitSha) } : {}),
  65. }
  66. // TODO: We can use `@roo-code/build` to generate `define` once the
  67. // monorepo is deployed.
  68. if (mode === "nightly") {
  69. outDir = "../apps/vscode-nightly/build/webview-ui/build"
  70. const nightlyPkg = JSON.parse(
  71. fs.readFileSync(path.join(__dirname, "..", "apps", "vscode-nightly", "package.nightly.json"), "utf8"),
  72. )
  73. define["process.env.PKG_NAME"] = JSON.stringify(nightlyPkg.name)
  74. define["process.env.PKG_VERSION"] = JSON.stringify(nightlyPkg.version)
  75. define["process.env.PKG_OUTPUT_CHANNEL"] = JSON.stringify("Kilo-Code-Nightly")
  76. }
  77. const plugins: PluginOption[] = [react(), tailwindcss(), persistPortPlugin(), wasmPlugin(), sourcemapPlugin()]
  78. return {
  79. plugins,
  80. resolve: {
  81. alias: {
  82. "@": resolve(__dirname, "./src"),
  83. "@src": resolve(__dirname, "./src"),
  84. "@roo": resolve(__dirname, "../src/shared"),
  85. },
  86. },
  87. build: {
  88. outDir,
  89. emptyOutDir: true,
  90. reportCompressedSize: false,
  91. // Generate complete source maps with original TypeScript sources
  92. sourcemap: true,
  93. // Ensure source maps are properly included in the build
  94. minify: mode === "production" ? "esbuild" : false,
  95. rollupOptions: {
  96. external: ["vscode"], // kilocode_change: we inadvertently import vscode into the webview: @roo/modes => src/shared/modes => ../core/prompts/sections/custom-instructions
  97. output: {
  98. entryFileNames: `assets/[name].js`,
  99. chunkFileNames: (chunkInfo) => {
  100. if (chunkInfo.name === "mermaid-bundle") {
  101. return `assets/mermaid-bundle.js`
  102. }
  103. // Default naming for other chunks, ensuring uniqueness from entry
  104. return `assets/chunk-[hash].js`
  105. },
  106. assetFileNames: (assetInfo) => {
  107. if (
  108. assetInfo.name &&
  109. (assetInfo.name.endsWith(".woff2") ||
  110. assetInfo.name.endsWith(".woff") ||
  111. assetInfo.name.endsWith(".ttf"))
  112. ) {
  113. return "assets/fonts/[name][extname]"
  114. }
  115. // Ensure source maps are included in the build
  116. if (assetInfo.name && assetInfo.name.endsWith(".map")) {
  117. return "assets/[name]"
  118. }
  119. return "assets/[name][extname]"
  120. },
  121. manualChunks: (id, { getModuleInfo }) => {
  122. // Consolidate all mermaid code and its direct large dependencies (like dagre)
  123. // into a single chunk. The 'channel.js' error often points to dagre.
  124. if (
  125. id.includes("node_modules/mermaid") ||
  126. id.includes("node_modules/dagre") || // dagre is a common dep for graph layout
  127. id.includes("node_modules/cytoscape") // another potential graph lib
  128. // Add other known large mermaid dependencies if identified
  129. ) {
  130. return "mermaid-bundle"
  131. }
  132. // Check if the module is part of any explicitly defined mermaid-related dynamic import
  133. // This is a more advanced check if simple path matching isn't enough.
  134. const moduleInfo = getModuleInfo(id)
  135. if (moduleInfo?.importers.some((importer) => importer.includes("node_modules/mermaid"))) {
  136. return "mermaid-bundle"
  137. }
  138. if (
  139. moduleInfo?.dynamicImporters.some((importer) => importer.includes("node_modules/mermaid"))
  140. ) {
  141. return "mermaid-bundle"
  142. }
  143. },
  144. },
  145. },
  146. },
  147. server: {
  148. host: "0.0.0.0", // kilocode_change
  149. hmr: {
  150. // host: "localhost", kilocode_change
  151. protocol: "ws",
  152. },
  153. cors: {
  154. origin: "*",
  155. methods: "*",
  156. allowedHeaders: "*",
  157. },
  158. },
  159. define,
  160. optimizeDeps: {
  161. include: [
  162. "mermaid",
  163. "dagre", // Explicitly include dagre for pre-bundling
  164. // Add other known large mermaid dependencies if identified
  165. ],
  166. exclude: ["@vscode/codicons", "vscode-oniguruma", "shiki", "vscode" /*kilocode_change*/],
  167. },
  168. assetsInclude: ["**/*.wasm", "**/*.wav"],
  169. }
  170. })