vite.config.ts 6.7 KB

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