Debugging vs. Bundle Size: Choosing ClojureScript Compilation Levels
Develop with :simple and source maps, ship with :advanced after annotating interop. A practical guide to ClojureScript compilation levels and debugging.
30 Nov 2026, 06:05 UTC

Debugging vs. Bundle Size: Choosing ClojureScript Compilation Levels
When a ClojureScript application misbehaves in the browser, the fastest path to a fix is a source map that lands on the original .cljs line. When the same application ships to production, the fastest path to a small bundle is aggressive renaming and dead-code elimination. These goals conflict. The useful takeaway: develop with :simple optimizations and source maps enabled, then switch to :advanced for production builds only after you have annotated every JavaScript interop boundary.
What the optimization levels actually change
ClojureScript compiles to JavaScript. The compiler option :optimizations controls how much the emitted JavaScript is rewritten.
:whitespaceremoves only whitespace and comments from the generated JavaScript. Every ClojureScript form remains visible in the output. This makes the bundle large, but the mapping between generated code and source is almost one-to-one.:simplerenames local ClojureScript symbols to short JavaScript identifiers and removes some redundant wrappers. The output is smaller than:whitespaceand still readable enough for debugging, especially when paired with a source map.:advancedrenames global symbols, inlines constants, and prunes unused code. The bundle is smallest, but names that the compiler believes are external can be mangled unless you tell it otherwise.
Source maps are JSON files that map positions in the generated JavaScript back to positions in the original ClojureScript files. They are generated when :source-map true is present in the compiler options and the build produces a .map file alongside the .js output.
A worked configuration for development
The following compiler options produce a debuggable build. Place them in whatever build configuration your project uses—a tools.build task, a Leiningen cljsbuild profile, or a direct cljs.main invocation. The examples assume a recent ClojureScript release and the Clojure CLI.
{:output-to "target/main.js"
:output-dir "target/out"
:optimizations :simple
:source-map true
:target :browser
:main 'my-app.core}If you invoke the compiler directly from the project root, the command looks like this:
# Run from the project root; requires Clojure CLI and the deps declared in deps.edn
clojure -M -m cljs.main --compile my-app.core \
--output-to target/main.js \
--output-dir target/out \
--optimizations simpleExpected check: after the command finishes, target/main.js and target/main.js.map should both exist. Open the page in a browser, open developer tools, and set a breakpoint on a known line in my_app/core.cljs. The debugger should pause there, not in the generated JavaScript.
Risk: if :source-map is omitted or set to false, the .map file will not be produced and the browser will only show renamed JavaScript.
Why advanced compilation breaks interop
Under :advanced, the compiler aggressively renames properties and function names. A plain JavaScript interop call such as (js/console.log "hi") is usually safe because console and log are well-known externs. A call to a less common library—say (js/MyLib.doThing)—can be mangled if the compiler does not know that MyLib is external.
The fix is to annotate the form with the :^js metadata hint or supply an externs file. For example:
(defn call-lib [x]
(.doThing ^js/MyLib x))Without the hint, the compiled output may reference a renamed symbol that does not exist at runtime, producing a TypeError such as Cannot read properties of undefined. This is the most common failure when moving from :simple to :advanced.
Limits of source maps
Source maps are not magic. Their accuracy depends on the optimization level:
- With
:whitespace, line and column positions in the map usually match the original source almost exactly. - With
:simple, positions are close, but inlined or reordered code may shift a breakpoint by a few lines. - With
:advanced, heavy inlining and constant folding can make some mappings approximate. You may still see original file names and rough locations, but stepping through code can feel jumpy.
Another limitation is deployment. If your web server or CDN strips //# sourceMappingURL= comments or rewrites paths, the browser will not load the map. You may need to post-process the .map file or configure the server to serve it with the correct MIME type.
Common mistakes beyond optimization flags
Compilation failures and odd runtime behavior often come from namespace structure, not optimization settings. Cyclic dependencies—where namespace A requires B and B requires A—cause the compiler to fail or emit code that behaves unexpectedly. Incorrect :require ordering in a namespace can lead to symbols being undefined at load time even though the code looks correct. These issues appear at every optimization level.
Practical verification checklist
- Run the build and confirm both
main.jsandmain.js.mapexist. - Open the application in a browser and check the developer tools Sources panel for the original ClojureScript file names.
- Set a breakpoint on a line you know executes. Reload and confirm the debugger stops at the expected line.
- For production, rebuild with
:advancedand smoke-test every JavaScript interop boundary. A missing:^jshint will not fail at compile time; it fails at runtime.
If the breakpoint lands in the wrong file or the wrong line, the source map is stale or misconfigured. Rebuild from a clean output directory before investigating further.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.