Patrick Dubroy
@dubroy
What are some good patterns for permanently including debugging code in a JS lib?

Eg: verbose logging, saving intermediate artifacts to disk, etc. But minimal / no perf impact in prod build.

I've done this in different ways, but never settled on patterns I'm really happy with.
Nov 14, 2023 · 2

I figure out a solution for this (using Vite). Seems decent so far.

Written up here in case anyone else finds it useful: github.com/pdubroy/til/bl…
Tweet image

TLDR:

1️⃣ Use process.env.FIZZBUZZ_DEBUG
2️⃣ In dev, use Vite's `define` option to replace this with the build-time value of that var
3️⃣ In prod, replace with ""
4️⃣ Use `external` option so dev builds can import "fs" if it's available
5️⃣ In prod, install a noop shim for "fs"

https://twitter.com/dubroy/status/1724509780369088542 ∙ Archived on 2025-03-28.

← Twitter Archive: 2023