esbuild.config.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // NOTE: @kilocode/agent-runtime is intentionally NOT external - it must be bundled
  85. // so that import.meta.url in extension-paths.ts resolves to the CLI's dist folder
  86. "@anthropic-ai/bedrock-sdk",
  87. "@anthropic-ai/sdk",
  88. "@anthropic-ai/vertex-sdk",
  89. "@aws-sdk/client-bedrock-runtime",
  90. "@aws-sdk/credential-providers",
  91. "@google/genai",
  92. "@lmstudio/sdk",
  93. "@mistralai/mistralai",
  94. "@modelcontextprotocol/sdk",
  95. "@qdrant/js-client-rest",
  96. "@vscode/codicons",
  97. "@vscode/ripgrep",
  98. "async-mutex",
  99. "axios",
  100. "chalk",
  101. "cheerio",
  102. "chokidar",
  103. "clone-deep",
  104. "commander",
  105. "default-shell",
  106. "delay",
  107. "diff",
  108. "diff-match-patch",
  109. "dotenv",
  110. "fast-deep-equal",
  111. "fast-glob",
  112. "fast-xml-parser",
  113. "fastest-levenshtein",
  114. "fs-extra",
  115. "fuse.js",
  116. "fzf",
  117. "get-folder-size",
  118. "google-auth-library",
  119. "gray-matter",
  120. "i18next",
  121. "ignore",
  122. "is-wsl",
  123. "isbinaryfile",
  124. "jotai",
  125. "jsdom",
  126. "json5",
  127. "jwt-decode",
  128. "lodash.debounce",
  129. "lru-cache",
  130. "mammoth",
  131. "marked",
  132. "marked-terminal",
  133. "monaco-vscode-textmate-theme-converter",
  134. "node-cache",
  135. "node-ipc",
  136. "ollama",
  137. "openai",
  138. "os-name",
  139. "p-limit",
  140. "p-wait-for",
  141. "pdf-parse",
  142. "pkce-challenge",
  143. "pretty-bytes",
  144. "proper-lockfile",
  145. "ps-list",
  146. "puppeteer-chromium-resolver",
  147. "puppeteer-core",
  148. "react",
  149. "reconnecting-eventsource",
  150. "sanitize-filename",
  151. "say",
  152. "semver",
  153. "serialize-error",
  154. "shiki",
  155. "simple-git",
  156. "socket.io-client",
  157. "sound-play",
  158. "stream-json",
  159. "strip-bom",
  160. "tiktoken",
  161. "tmp",
  162. "tree-sitter-wasms",
  163. "turndown",
  164. "uri-js",
  165. "uuid",
  166. "vscode-material-icons",
  167. "web-tree-sitter",
  168. "workerpool",
  169. "xlsx",
  170. "yaml",
  171. "zod",
  172. ],
  173. sourcemap: false,
  174. minify: false,
  175. treeShaking: true,
  176. logLevel: "info",
  177. plugins: [esbuildProblemMatcherPlugin, afterBuildPlugin],
  178. alias: {
  179. 'is-in-ci': path.resolve(__dirname, 'src/patches/is-in-ci.ts'),
  180. }
  181. }
  182. if (process.argv.includes("--watch")) {
  183. const ctx = await esbuild.context(config)
  184. await ctx.watch()
  185. console.log("Watching for changes...")
  186. } else {
  187. // Single build
  188. await esbuild.build(config)
  189. }