esbuild.config.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* eslint-disable no-undef */
  2. import esbuild from "esbuild"
  3. import path from 'node:path';
  4. import { fileURLToPath } from 'node:url';
  5. import { chmodSync, mkdirSync, copyFileSync } from "fs"
  6. import { rimrafSync } from "rimraf"
  7. // ESM Polyfill
  8. const __filename = fileURLToPath(import.meta.url);
  9. const __dirname = path.dirname(__filename);
  10. // Function to copy post-build files
  11. function copyPostBuildFiles() {
  12. try {
  13. mkdirSync("dist/config", { recursive: true })
  14. copyFileSync("src/config/schema.json", "dist/config/schema.json")
  15. copyFileSync("package.dist.json", "dist/package.json")
  16. copyFileSync("npm-shrinkwrap.dist.json", "dist/npm-shrinkwrap.json")
  17. copyFileSync("README.md", "dist/README.md")
  18. try {
  19. copyFileSync(".env", "dist/.env")
  20. copyFileSync(".env", "dist/kilocode/.env")
  21. } catch {
  22. // .env might not exist, that's okay
  23. }
  24. console.log("✓ Post-build files copied")
  25. } catch (err) {
  26. console.error("Error copying post-build files:", err)
  27. }
  28. }
  29. function removeUnneededFiles() {
  30. rimrafSync("dist/kilocode/webview-ui")
  31. rimrafSync("dist/kilocode/assets")
  32. console.log("✓ Unneeded files removed")
  33. }
  34. const afterBuildPlugin = {
  35. name: "after-build",
  36. setup(build) {
  37. build.onEnd((result) => {
  38. if (result.errors.length > 0) return
  39. copyPostBuildFiles()
  40. removeUnneededFiles()
  41. try {
  42. chmodSync("dist/index.js", 0o755)
  43. console.log("✓ dist/index.js made executable")
  44. } catch (err) {
  45. console.error("Error making dist/index.js executable:", err)
  46. }
  47. })
  48. },
  49. }
  50. // Problem matcher plugin for VS Code task integration
  51. const esbuildProblemMatcherPlugin = {
  52. name: "esbuild-problem-matcher",
  53. setup(build) {
  54. build.onStart(() => console.log("[esbuild-problem-matcher#onStart]"))
  55. build.onEnd((result) => {
  56. result.errors.forEach(({ text, location }) => {
  57. console.error(`✘ [ERROR] ${text}`)
  58. if (location && location.file) {
  59. console.error(` ${location.file}:${location.line}:${location.column}:`)
  60. }
  61. })
  62. console.log("[esbuild-problem-matcher#onEnd]")
  63. })
  64. },
  65. }
  66. const config = {
  67. entryPoints: ["src/index.ts"],
  68. bundle: true,
  69. platform: "node",
  70. target: "node20",
  71. format: "esm",
  72. outfile: "dist/index.js",
  73. banner: {
  74. js: `import { createRequire as __createRequire__ } from 'module';
  75. import { fileURLToPath as __fileURLToPath__ } from 'url';
  76. import { dirname as __dirname__ } from 'path';
  77. const require = __createRequire__(import.meta.url);
  78. const __filename = __fileURLToPath__(import.meta.url);
  79. const __dirname = __dirname__(__filename);
  80. `,
  81. },
  82. external: [
  83. // Keep these as external dependencies (will be installed via npm)
  84. "@anthropic-ai/bedrock-sdk",
  85. "@anthropic-ai/sdk",
  86. "@anthropic-ai/vertex-sdk",
  87. "@aws-sdk/client-bedrock-runtime",
  88. "@aws-sdk/credential-providers",
  89. "@google/genai",
  90. "@lmstudio/sdk",
  91. "@mistralai/mistralai",
  92. "@modelcontextprotocol/sdk",
  93. "@qdrant/js-client-rest",
  94. "@vscode/codicons",
  95. "@vscode/ripgrep",
  96. "async-mutex",
  97. "axios",
  98. "chalk",
  99. "cheerio",
  100. "chokidar",
  101. "clone-deep",
  102. "commander",
  103. "default-shell",
  104. "delay",
  105. "diff",
  106. "diff-match-patch",
  107. "dotenv",
  108. "fast-deep-equal",
  109. "fast-glob",
  110. "fast-xml-parser",
  111. "fastest-levenshtein",
  112. "fs-extra",
  113. "fuse.js",
  114. "fzf",
  115. "get-folder-size",
  116. "google-auth-library",
  117. "gray-matter",
  118. "i18next",
  119. "ignore",
  120. "is-wsl",
  121. "isbinaryfile",
  122. "jotai",
  123. "jsdom",
  124. "json5",
  125. "jwt-decode",
  126. "lodash.debounce",
  127. "lru-cache",
  128. "mammoth",
  129. "marked",
  130. "marked-terminal",
  131. "monaco-vscode-textmate-theme-converter",
  132. "node-cache",
  133. "node-ipc",
  134. "ollama",
  135. "openai",
  136. "os-name",
  137. "p-limit",
  138. "p-wait-for",
  139. "pdf-parse",
  140. "pkce-challenge",
  141. "pretty-bytes",
  142. "proper-lockfile",
  143. "ps-list",
  144. "puppeteer-chromium-resolver",
  145. "puppeteer-core",
  146. "react",
  147. "reconnecting-eventsource",
  148. "sanitize-filename",
  149. "say",
  150. "semver",
  151. "serialize-error",
  152. "shiki",
  153. "simple-git",
  154. "socket.io-client",
  155. "sound-play",
  156. "stream-json",
  157. "strip-bom",
  158. "tiktoken",
  159. "tmp",
  160. "tree-sitter-wasms",
  161. "turndown",
  162. "uri-js",
  163. "uuid",
  164. "vscode-material-icons",
  165. "web-tree-sitter",
  166. "workerpool",
  167. "xlsx",
  168. "yaml",
  169. "zod",
  170. ],
  171. sourcemap: false,
  172. minify: false,
  173. treeShaking: true,
  174. logLevel: "info",
  175. plugins: [esbuildProblemMatcherPlugin, afterBuildPlugin],
  176. alias: {
  177. 'is-in-ci': path.resolve(__dirname, 'src/patches/is-in-ci.ts'),
  178. }
  179. }
  180. if (process.argv.includes("--watch")) {
  181. const ctx = await esbuild.context(config)
  182. await ctx.watch()
  183. console.log("Watching for changes...")
  184. } else {
  185. // Single build
  186. await esbuild.build(config)
  187. }