vite.config.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. babel: {
  81. plugins: [["babel-plugin-react-compiler", { target: "18" }]],
  82. },
  83. }),
  84. tailwindcss(),
  85. persistPortPlugin(),
  86. wasmPlugin(),
  87. sourcemapPlugin(),
  88. cssPerEntryPlugin(), // kilocode_change: enable per-entry CSS files
  89. ]
  90. return {
  91. plugins,
  92. resolve: {
  93. alias: {
  94. "@": resolve(__dirname, "./src"),
  95. "@src": resolve(__dirname, "./src"),
  96. "@roo": resolve(__dirname, "../src/shared"),
  97. },
  98. },
  99. build: {
  100. outDir,
  101. emptyOutDir: true,
  102. reportCompressedSize: false,
  103. // Generate complete source maps with original TypeScript sources
  104. sourcemap: true,
  105. // Ensure source maps are properly included in the build
  106. minify: mode === "production" ? "esbuild" : false,
  107. // Use a single combined CSS bundle so both webviews share styles
  108. cssCodeSplit: true, // kilocode_change: changed to true to enable cssPerEntryPlugin
  109. rollupOptions: {
  110. // Externalize vscode module - it's imported by file-search.ts which is
  111. // dynamically imported by roo-config/index.ts, but should never be bundled
  112. // in the webview since it's not available in the browser context
  113. external: ["vscode"],
  114. input: {
  115. index: resolve(__dirname, "index.html"), // kilocode_change - DO NOT CHANGE
  116. "agent-manager": resolve(__dirname, "agent-manager.html"), // kilocode_change
  117. "browser-panel": resolve(__dirname, "browser-panel.html"),
  118. },
  119. output: {
  120. entryFileNames: `assets/[name].js`,
  121. chunkFileNames: (chunkInfo) => {
  122. if (chunkInfo.name === "mermaid-bundle") {
  123. return `assets/mermaid-bundle.js`
  124. }
  125. // Default naming for other chunks, ensuring uniqueness from entry
  126. return `assets/chunk-[hash].js`
  127. },
  128. assetFileNames: (assetInfo) => {
  129. const name = assetInfo.name || ""
  130. // kilocode_change start - cssPerEntryPlugin
  131. // Force all CSS into a single predictable file used by both webviews
  132. // if (name.endsWith(".css")) {
  133. // return "assets/index.css"
  134. //}
  135. // kilocode_change end
  136. if (name.endsWith(".woff2") || name.endsWith(".woff") || name.endsWith(".ttf")) {
  137. return "assets/fonts/[name][extname]"
  138. }
  139. // Ensure source maps are included in the build
  140. if (name.endsWith(".map")) {
  141. return "assets/[name]"
  142. }
  143. return "assets/[name][extname]"
  144. },
  145. manualChunks: (id, { getModuleInfo }) => {
  146. // Consolidate all mermaid code and its direct large dependencies (like dagre)
  147. // into a single chunk. The 'channel.js' error often points to dagre.
  148. if (
  149. id.includes("node_modules/mermaid") ||
  150. id.includes("node_modules/dagre") || // dagre is a common dep for graph layout
  151. id.includes("node_modules/cytoscape") // another potential graph lib
  152. // Add other known large mermaid dependencies if identified
  153. ) {
  154. return "mermaid-bundle"
  155. }
  156. // Check if the module is part of any explicitly defined mermaid-related dynamic import
  157. // This is a more advanced check if simple path matching isn't enough.
  158. const moduleInfo = getModuleInfo(id)
  159. if (moduleInfo?.importers.some((importer) => importer.includes("node_modules/mermaid"))) {
  160. return "mermaid-bundle"
  161. }
  162. if (
  163. moduleInfo?.dynamicImporters.some((importer) => importer.includes("node_modules/mermaid"))
  164. ) {
  165. return "mermaid-bundle"
  166. }
  167. },
  168. },
  169. },
  170. },
  171. server: {
  172. host: "0.0.0.0", // kilocode_change
  173. hmr: {
  174. // host: "localhost", kilocode_change
  175. protocol: "ws",
  176. },
  177. cors: {
  178. origin: "*",
  179. methods: "*",
  180. allowedHeaders: "*",
  181. },
  182. },
  183. define,
  184. optimizeDeps: {
  185. include: [
  186. "mermaid",
  187. "dagre", // Explicitly include dagre for pre-bundling
  188. // Add other known large mermaid dependencies if identified
  189. ],
  190. exclude: ["@vscode/codicons", "vscode-oniguruma", "shiki", "vscode" /*kilocode_change*/],
  191. },
  192. assetsInclude: ["**/*.wasm", "**/*.wav"],
  193. }
  194. })