DEV Community

Cao Wen
Cao Wen

Posted on Edited on

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 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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

--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
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

The constraint is the part that makes this interesting — a 500 KB file built from six hand-concatenated modules, where every handler like livenetGift() is a global that inline onclick depends on, so a normal minifier renaming them to a, b, c breaks everything at runtime with "livenetGift is not defined". Stripping comments and whitespace without renaming identifiers is the right rule, and it is also the part most minifier defaults get wrong. What did that version of terser actually reject in the flags, and what did you end up using instead — a comments-only pass, or terser with mangling properly switched off?

Collapse
 
durianlive profile image
Cao Wen •

Good question. Terser 5.51.4 via npx rejected both-c false and-m false withERROR: 'false' is not a supported option , and--compress=false --mangle=false failed the same way in that version.

I stopped fighting the CLI and switched to esbuild:--minify-whitespace --legal-comments=none . It strips comments, blank lines and extra spaces, and leaves identifiers untouched — which is exactly the constraint here. The build script falls back to the unminified file if esbuild isn't available, so a registry outage never breaks the deploy.