esbuild.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. name: "alias-plugin",
  69. setup(build) {
  70. build.onResolve({ filter: /^pkce-challenge$/ }, (args) => {
  71. return { path: require.resolve("pkce-challenge/dist/index.browser.js") }
  72. })
  73. },
  74. },
  75. ],
  76. entryPoints: ["src/extension.ts"],
  77. format: "cjs",
  78. sourcesContent: false,
  79. platform: "node",
  80. outfile: "dist/extension.js",
  81. external: ["vscode"],
  82. }
  83. async function main() {
  84. const extensionCtx = await esbuild.context(extensionConfig)
  85. if (watch) {
  86. await extensionCtx.watch()
  87. } else {
  88. await extensionCtx.rebuild()
  89. await extensionCtx.dispose()
  90. }
  91. }
  92. main().catch((e) => {
  93. console.error(e)
  94. process.exit(1)
  95. })