esbuild.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // tiktoken
  29. fs.copyFileSync(
  30. path.join(__dirname, "node_modules", "tiktoken", "tiktoken_bg.wasm"),
  31. path.join(__dirname, "dist", "tiktoken_bg.wasm")
  32. )
  33. // tree sitter
  34. const sourceDir = path.join(__dirname, "node_modules", "web-tree-sitter")
  35. const targetDir = path.join(__dirname, "dist")
  36. // Copy tree-sitter.wasm
  37. fs.copyFileSync(path.join(sourceDir, "tree-sitter.wasm"), path.join(targetDir, "tree-sitter.wasm"))
  38. // Copy language-specific WASM files
  39. const languageWasmDir = path.join(__dirname, "node_modules", "tree-sitter-wasms", "out")
  40. const languages = [
  41. "typescript",
  42. "tsx",
  43. "python",
  44. "rust",
  45. "javascript",
  46. "go",
  47. "cpp",
  48. "c",
  49. "c_sharp",
  50. "ruby",
  51. "java",
  52. "swift",
  53. ]
  54. languages.forEach((lang) => {
  55. const filename = `tree-sitter-${lang}.wasm`
  56. fs.copyFileSync(path.join(languageWasmDir, filename), path.join(targetDir, filename))
  57. })
  58. })
  59. },
  60. }
  61. const extensionConfig = {
  62. bundle: true,
  63. minify: production,
  64. sourcemap: !production,
  65. logLevel: "silent",
  66. plugins: [
  67. copyWasmFiles,
  68. /* add to the end of plugins array */
  69. esbuildProblemMatcherPlugin,
  70. ],
  71. entryPoints: ["src/extension.ts"],
  72. format: "cjs",
  73. sourcesContent: false,
  74. platform: "node",
  75. outfile: "dist/extension.js",
  76. external: ["vscode"],
  77. }
  78. async function main() {
  79. const extensionCtx = await esbuild.context(extensionConfig)
  80. if (watch) {
  81. await extensionCtx.watch()
  82. } else {
  83. await extensionCtx.rebuild()
  84. await extensionCtx.dispose()
  85. }
  86. }
  87. main().catch((e) => {
  88. console.error(e)
  89. process.exit(1)
  90. })