esbuild.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const extensionConfig = {
  59. bundle: true,
  60. minify: production,
  61. sourcemap: !production,
  62. logLevel: "silent",
  63. plugins: [
  64. copyWasmFiles,
  65. /* add to the end of plugins array */
  66. esbuildProblemMatcherPlugin,
  67. ],
  68. entryPoints: ["src/extension.ts"],
  69. format: "cjs",
  70. sourcesContent: false,
  71. platform: "node",
  72. outfile: "dist/extension.js",
  73. external: ["vscode"],
  74. }
  75. async function main() {
  76. const extensionCtx = await esbuild.context(extensionConfig)
  77. if (watch) {
  78. await extensionCtx.watch()
  79. } else {
  80. await extensionCtx.rebuild()
  81. await extensionCtx.dispose()
  82. }
  83. }
  84. main().catch((e) => {
  85. console.error(e)
  86. process.exit(1)
  87. })