Stripping comments from a 500 KB JS bundle without breaking inline onclick
Our widget ships as one hand-concatenated file: six source modules are joined into shell.js, and the server serves that file directly. No bundler, no module graph, no build server. That worked fine until the file reache
Our widget ships as one hand-concatenated file: six source modules are joined into shell.js, and the server serves that file directly. No bundler, no module graph, no build server.
That worked fine until the file reached ~500 KB and every internal note, fix log, and TODO was publicly readable. The fix sounds trivial — strip comments — until you remember the page calls global functions from inline onclick attributes.
The constraint that rules out normal minification
The widget's markup is generated as strings, and buttons look like this:
<button onclick="livenetGift()">Send</button>
Every handler is a global function on window. A normal minifier renames those functions to a, b, c, and every inline handler breaks at runtime with livenetGift is not defined.
So the rule is: remove comments and whitespace, never rename identifiers.
Attempt 1: terser
npx terser shell.js -c false -m false --comments false
That version of terser rejected the flags:
Terser is built for full minification; disabling both compression and mangling is not a supported path in every release.
Attempt 2: esbuild with whitespace-only minification
npx --yes esbuild shell.js \
--minify-whitespace \
--legal-comments=none \
--outfile=shell.min.js
--minify-whitespace removes comments, blank lines, and extra spaces. It does not rename identifiers or rewrite syntax. --legal-comments=none drops license comments too.
Result: same function names, same behaviour, no comments.
Verify before shipping
Two cheap checks in the build script:
node --check shell.min.js
grep -c "function livenetGift" shell.min.js
The first catches syntax errors. The second catches the failure mode that matters: a handler that used to exist but no longer does.
Make the build fail safe
The build script should never leave you without a working file:
$tmp = Join-Path $scriptDir "shell.__min.js"
& npx --yes esbuild $outFile --minify-whitespace --legal-comments=none --outfile=$tmp
if ($LASTEXITCODE -eq 0) {
Move-Item -Force $tmp $outFile
} else {
Remove-Item $tmp -ErrorAction SilentlyContinue
Write-Host "esbuild unavailable - keeping unminified build"
}
If npx cannot reach the registry, the build keeps the unminified file instead of failing. A slightly larger bundle is better than a broken deploy.
What this does not solve
- Stack traces now point to different line numbers. Keep the unminified file for local development.
- It is not obfuscation. Anyone can still read the logic; they just cannot read your comments.
- It does not replace a real bundler. The concatenation order is still manual.
For a small widget served as a single file, that trade-off is fine. The build stays a shell script, the page keeps working, and the internal notes stay internal.
This build serves the widget at durianlive.com. If you're evaluating live shopping tools, the merchant-facing checklist — commission, viewer caps, replay ownership — is here: Choosing a live shopping plugin without commission.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.