2
0

esbuild.js 2.1 KB

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