esbuild.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import * as esbuild from "esbuild"
  2. import * as fs from "fs"
  3. import * as path from "path"
  4. import { fileURLToPath } from "url"
  5. import process from "node:process"
  6. import * as console from "node:console"
  7. import { copyPaths, copyWasms, copyLocales, setupLocaleWatcher } from "@roo-code/build"
  8. const __filename = fileURLToPath(import.meta.url)
  9. const __dirname = path.dirname(__filename)
  10. async function main() {
  11. const name = "extension"
  12. const production = process.argv.includes("--production")
  13. const watch = process.argv.includes("--watch")
  14. const minify = production
  15. const sourcemap = !production
  16. /**
  17. * @type {import('esbuild').BuildOptions}
  18. */
  19. const buildOptions = {
  20. bundle: true,
  21. minify,
  22. sourcemap,
  23. logLevel: "silent",
  24. format: "cjs",
  25. sourcesContent: false,
  26. platform: "node",
  27. }
  28. const srcDir = __dirname
  29. const buildDir = __dirname
  30. const distDir = path.join(buildDir, "dist")
  31. if (fs.existsSync(distDir)) {
  32. console.log(`[${name}] Cleaning dist directory: ${distDir}`)
  33. fs.rmSync(distDir, { recursive: true, force: true })
  34. }
  35. /**
  36. * @type {import('esbuild').Plugin[]}
  37. */
  38. const plugins = [
  39. {
  40. name: "copyFiles",
  41. setup(build) {
  42. build.onEnd(() => {
  43. copyPaths(
  44. [
  45. ["../README.md", "README.md"],
  46. ["../CHANGELOG.md", "CHANGELOG.md"],
  47. ["../LICENSE", "LICENSE"],
  48. ["../.env", ".env", { optional: true }],
  49. ["node_modules/vscode-material-icons/generated", "assets/vscode-material-icons"],
  50. ["../webview-ui/audio", "webview-ui/audio"],
  51. ],
  52. srcDir,
  53. buildDir,
  54. )
  55. })
  56. },
  57. },
  58. {
  59. name: "copyWasms",
  60. setup(build) {
  61. build.onEnd(() => copyWasms(srcDir, distDir))
  62. },
  63. },
  64. {
  65. name: "copyLocales",
  66. setup(build) {
  67. build.onEnd(() => copyLocales(srcDir, distDir))
  68. },
  69. },
  70. {
  71. name: "esbuild-problem-matcher",
  72. setup(build) {
  73. build.onStart(() => console.log("[esbuild-problem-matcher#onStart]"))
  74. build.onEnd((result) => {
  75. result.errors.forEach(({ text, location }) => {
  76. console.error(`✘ [ERROR] ${text}`)
  77. if (location && location.file) {
  78. console.error(` ${location.file}:${location.line}:${location.column}:`)
  79. }
  80. })
  81. console.log("[esbuild-problem-matcher#onEnd]")
  82. })
  83. },
  84. },
  85. ]
  86. /**
  87. * @type {import('esbuild').BuildOptions}
  88. */
  89. const extensionConfig = {
  90. ...buildOptions,
  91. plugins,
  92. entryPoints: ["extension.ts"],
  93. outfile: "dist/extension.js",
  94. external: ["vscode"],
  95. }
  96. /**
  97. * @type {import('esbuild').BuildOptions}
  98. */
  99. const workerConfig = {
  100. ...buildOptions,
  101. entryPoints: ["workers/countTokens.ts"],
  102. outdir: "dist/workers",
  103. }
  104. const [extensionCtx, workerCtx] = await Promise.all([
  105. esbuild.context(extensionConfig),
  106. esbuild.context(workerConfig),
  107. ])
  108. if (watch) {
  109. await Promise.all([extensionCtx.watch(), workerCtx.watch()])
  110. copyLocales(srcDir, distDir)
  111. setupLocaleWatcher(srcDir, distDir)
  112. } else {
  113. await Promise.all([extensionCtx.rebuild(), workerCtx.rebuild()])
  114. await Promise.all([extensionCtx.dispose(), workerCtx.dispose()])
  115. }
  116. }
  117. main().catch((e) => {
  118. console.error(e)
  119. process.exit(1)
  120. })