esbuild.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "php",
  53. "swift",
  54. ]
  55. languages.forEach((lang) => {
  56. const filename = `tree-sitter-${lang}.wasm`
  57. fs.copyFileSync(path.join(languageWasmDir, filename), path.join(targetDir, filename))
  58. })
  59. })
  60. },
  61. }
  62. const extensionConfig = {
  63. bundle: true,
  64. minify: production,
  65. sourcemap: !production,
  66. logLevel: "silent",
  67. plugins: [
  68. copyWasmFiles,
  69. /* add to the end of plugins array */
  70. esbuildProblemMatcherPlugin,
  71. ],
  72. entryPoints: ["src/extension.ts"],
  73. format: "cjs",
  74. sourcesContent: false,
  75. platform: "node",
  76. outfile: "dist/extension.js",
  77. external: ["vscode"],
  78. }
  79. async function main() {
  80. const extensionCtx = await esbuild.context(extensionConfig)
  81. if (watch) {
  82. await extensionCtx.watch()
  83. } else {
  84. await extensionCtx.rebuild()
  85. await extensionCtx.dispose()
  86. }
  87. }
  88. main().catch((e) => {
  89. console.error(e)
  90. process.exit(1)
  91. })