esbuild.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const esbuild = require("esbuild")
  2. const production = process.argv.includes("--production")
  3. const watch = process.argv.includes("--watch")
  4. /**
  5. * @type {import('esbuild').Plugin}
  6. */
  7. const esbuildProblemMatcherPlugin = {
  8. name: "esbuild-problem-matcher",
  9. setup(build) {
  10. build.onStart(() => {
  11. console.log("[watch] build started")
  12. })
  13. build.onEnd((result) => {
  14. result.errors.forEach(({ text, location }) => {
  15. console.error(`✘ [ERROR] ${text}`)
  16. console.error(` ${location.file}:${location.line}:${location.column}:`)
  17. })
  18. console.log("[watch] build finished")
  19. })
  20. },
  21. }
  22. const baseConfig = {
  23. bundle: true,
  24. minify: production,
  25. sourcemap: !production,
  26. logLevel: "silent",
  27. plugins: [
  28. /* add to the end of plugins array */
  29. esbuildProblemMatcherPlugin,
  30. ],
  31. }
  32. const extensionConfig = {
  33. ...baseConfig,
  34. entryPoints: ["src/extension.ts"],
  35. format: "cjs",
  36. sourcesContent: false,
  37. platform: "node",
  38. outfile: "dist/extension.js",
  39. external: ["vscode"],
  40. }
  41. const webviewConfig = {
  42. ...baseConfig,
  43. target: "es2020",
  44. format: "esm",
  45. entryPoints: ["src/webview/main.ts"],
  46. outfile: "dist/webview.js",
  47. }
  48. async function main() {
  49. const extensionCtx = await esbuild.context(extensionConfig)
  50. const webviewCtx = await esbuild.context(webviewConfig)
  51. if (watch) {
  52. await extensionCtx.watch()
  53. await webviewCtx.watch()
  54. } else {
  55. await extensionCtx.rebuild()
  56. await extensionCtx.dispose()
  57. await webviewCtx.rebuild()
  58. await webviewCtx.dispose()
  59. }
  60. }
  61. main().catch((e) => {
  62. console.error(e)
  63. process.exit(1)
  64. })