esbuild.js 2.1 KB

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