Uploading Honeybadger source maps with esbuild and Sprockets

Curated ago by @jeremysmithco

Description

If you are using Honeybadger for JS error monitoring on your Rails app, you may want to upload source maps so they can be applied to JS stack traces.

Here’s how you can do that if you are using esbuild, Sprockets, and deploying to Heroku. The source map will be uploaded to Honeybadger during the build phase, via a Rake task enhancing the assets:precompile step.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import * as esbuild from "esbuild"
import path from "path"

const config = {
  entryPoints: ["application.js"],
  bundle: true,
  outdir: path.join(process.cwd(), "app/assets/builds"),
  absWorkingDir: path.join(process.cwd(), "app/javascript"),
  minify: true,
  sourcemap: (process.env.RAILS_ENV == "production") ? "external" : "linked",
}

if (process.argv.includes("--watch")) {
  let context = await esbuild.context({...config, logLevel: 'info'})
  context.watch()
} else {
  esbuild.build(config)
}

This is setting the sourcemap option to external for production, but linked for development. See esbuild sourcemap options.