esbuild.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const esbuild = require("esbuild")
  2. const fs = require("fs")
  3. const path = require("path")
  4. const production = process.argv.includes("--production")
  5. const watch = process.argv.includes("--watch")
  6. /**
  7. * @type {import('esbuild').Plugin}
  8. */
  9. const esbuildProblemMatcherPlugin = {
  10. name: "esbuild-problem-matcher",
  11. setup(build) {
  12. build.onStart(() => {
  13. console.log("[watch] build started")
  14. })
  15. build.onEnd((result) => {
  16. result.errors.forEach(({ text, location }) => {
  17. console.error(`✘ [ERROR] ${text}`)
  18. console.error(` ${location.file}:${location.line}:${location.column}:`)
  19. })
  20. console.log("[watch] build finished")
  21. })
  22. },
  23. }
  24. const copyWasmFiles = {
  25. name: "copy-wasm-files",
  26. setup(build) {
  27. build.onEnd(() => {
  28. // tree sitter
  29. const sourceDir = path.join(__dirname, "node_modules", "web-tree-sitter")
  30. const targetDir = path.join(__dirname, "dist")
  31. // Copy tree-sitter.wasm
  32. fs.copyFileSync(path.join(sourceDir, "tree-sitter.wasm"), path.join(targetDir, "tree-sitter.wasm"))
  33. // Copy language-specific WASM files
  34. const languageWasmDir = path.join(__dirname, "node_modules", "tree-sitter-wasms", "out")
  35. const languages = [
  36. "typescript",
  37. "tsx",
  38. "python",
  39. "rust",
  40. "javascript",
  41. "go",
  42. "cpp",
  43. "c",
  44. "c_sharp",
  45. "ruby",
  46. "java",
  47. "php",
  48. "swift",
  49. "kotlin",
  50. ]
  51. languages.forEach((lang) => {
  52. const filename = `tree-sitter-${lang}.wasm`
  53. fs.copyFileSync(path.join(languageWasmDir, filename), path.join(targetDir, filename))
  54. })
  55. })
  56. },
  57. }
  58. // Simple function to copy locale files
  59. function copyLocaleFiles() {
  60. const srcDir = path.join(__dirname, "src", "i18n", "locales")
  61. const destDir = path.join(__dirname, "dist", "i18n", "locales")
  62. const outDir = path.join(__dirname, "out", "i18n", "locales")
  63. // Ensure source directory exists before proceeding
  64. if (!fs.existsSync(srcDir)) {
  65. console.warn(`Source locales directory does not exist: ${srcDir}`)
  66. return // Exit early if source directory doesn't exist
  67. }
  68. // Create destination directories
  69. fs.mkdirSync(destDir, { recursive: true })
  70. try {
  71. fs.mkdirSync(outDir, { recursive: true })
  72. } catch (e) {}
  73. // Function to copy directory recursively
  74. function copyDir(src, dest) {
  75. const entries = fs.readdirSync(src, { withFileTypes: true })
  76. for (const entry of entries) {
  77. const srcPath = path.join(src, entry.name)
  78. const destPath = path.join(dest, entry.name)
  79. if (entry.isDirectory()) {
  80. // Create directory and copy contents
  81. fs.mkdirSync(destPath, { recursive: true })
  82. copyDir(srcPath, destPath)
  83. } else {
  84. // Copy the file
  85. fs.copyFileSync(srcPath, destPath)
  86. }
  87. }
  88. }
  89. // Copy files to dist directory
  90. copyDir(srcDir, destDir)
  91. console.log("Copied locale files to dist/i18n/locales")
  92. // Copy to out directory for debugging
  93. try {
  94. copyDir(srcDir, outDir)
  95. console.log("Copied locale files to out/i18n/locales")
  96. } catch (e) {
  97. console.warn("Could not copy to out directory:", e.message)
  98. }
  99. }
  100. // Set up file watcher if in watch mode
  101. function setupLocaleWatcher() {
  102. if (!watch) return
  103. const localesDir = path.join(__dirname, "src", "i18n", "locales")
  104. // Ensure the locales directory exists before setting up watcher
  105. if (!fs.existsSync(localesDir)) {
  106. console.warn(`Cannot set up watcher: Source locales directory does not exist: ${localesDir}`)
  107. return
  108. }
  109. console.log(`Setting up watcher for locale files in ${localesDir}`)
  110. // Use a debounce mechanism
  111. let debounceTimer = null
  112. const debouncedCopy = () => {
  113. if (debounceTimer) clearTimeout(debounceTimer)
  114. debounceTimer = setTimeout(() => {
  115. console.log("Locale files changed, copying...")
  116. copyLocaleFiles()
  117. }, 300) // Wait 300ms after last change before copying
  118. }
  119. // Watch the locales directory
  120. try {
  121. fs.watch(localesDir, { recursive: true }, (eventType, filename) => {
  122. if (filename && filename.endsWith(".json")) {
  123. console.log(`Locale file ${filename} changed, triggering copy...`)
  124. debouncedCopy()
  125. }
  126. })
  127. console.log("Watcher for locale files is set up")
  128. } catch (error) {
  129. console.error(`Error setting up watcher for ${localesDir}:`, error.message)
  130. }
  131. }
  132. const copyLocalesFiles = {
  133. name: "copy-locales-files",
  134. setup(build) {
  135. build.onEnd(() => {
  136. copyLocaleFiles()
  137. })
  138. },
  139. }
  140. const extensionConfig = {
  141. bundle: true,
  142. minify: production,
  143. sourcemap: !production,
  144. logLevel: "silent",
  145. plugins: [
  146. copyWasmFiles,
  147. copyLocalesFiles,
  148. /* add to the end of plugins array */
  149. esbuildProblemMatcherPlugin,
  150. {
  151. name: "alias-plugin",
  152. setup(build) {
  153. build.onResolve({ filter: /^pkce-challenge$/ }, (args) => {
  154. return { path: require.resolve("pkce-challenge/dist/index.browser.js") }
  155. })
  156. },
  157. },
  158. ],
  159. entryPoints: ["src/extension.ts"],
  160. format: "cjs",
  161. sourcesContent: false,
  162. platform: "node",
  163. outfile: "dist/extension.js",
  164. external: ["vscode"],
  165. }
  166. async function main() {
  167. const extensionCtx = await esbuild.context(extensionConfig)
  168. if (watch) {
  169. // Start the esbuild watcher
  170. await extensionCtx.watch()
  171. // Copy and watch locale files
  172. console.log("Copying locale files initially...")
  173. copyLocaleFiles()
  174. // Set up the watcher for locale files
  175. setupLocaleWatcher()
  176. } else {
  177. await extensionCtx.rebuild()
  178. await extensionCtx.dispose()
  179. }
  180. }
  181. main().catch((e) => {
  182. console.error(e)
  183. process.exit(1)
  184. })