Split ClojureScript builds with :modules
Keep initial ClojureScript bundles small by splitting compiler output into modules and loading them on demand.
02 May 2026, 23:31 UTC

The problem: one bundle keeps growing
A ClojureScript release build usually emits a single JavaScript file. As you add routes, charts, or admin screens, that file ships to every visitor even when they never open those features. Client-side parse and compile time rises with the bundle.
The useful takeaway: ClojureScript’s built-in :modules option can split compiler output into separate files, each with its own dependency graph. Your application then requests a module only when a route or feature actually needs it.
What :modules produces
A module is a group of namespaces compiled into one JavaScript file. The compiler records which module provides which namespace and emits a bootstrap loader script alongside the outputs. At runtime, your code asks the loader for a module by name; the loader fetches and evaluates that file, after which its namespaces resolve normally.
This is compiler-level code splitting. It does not require shadow-cljs, webpack, or a separate plugin, though those tools can wrap the same option.
Configure two modules
Run the compiler from your project root in a terminal. Replace , , and output paths with your own values. The configuration shape below shows two modules: a main module for core UI and an admin module loaded later.
{:deps {org.clojure/clojurescript {:mvn/version }}
:aliases {:build {:exec-fn cljs.build.api/build
:exec-args {:id
:output-to "target/main.js"
:modules {:main {:entries [my-app.core]
:output-to "target/main.js"}
:admin {:entries [my-app.admin.screen]
:deps #{:main}
:output-to "target/admin.js"}}}}}}
Where to run: the project directory containing deps.edn. Permissions: read access to source files and write access to the output directory. Risks: stale files in the output directory can hide configuration errors; clean it before verifying a new split.
:entries names the root namespaces for each module. :deps tells the compiler that :admin can share code already loaded by :main. Without that relationship, shared helpers may be duplicated in both files, increasing total bytes shipped.
Load a module when it is needed
The compiler emits a bootstrap loader script. In your application, trigger loading before rendering a lazy feature—for example, when a user navigates to an admin route. After the loader reports success, require the module’s namespaces as usual.
Lazy-loaded code is not fetched automatically. A network failure leaves its namespaces undefined, so guard the load call with error handling and show fallback UI instead of letting the application crash.
Check the result and watch for traps
- Open the browser devtools Network tab, reload the page, and confirm only the main module and bootstrap loader appear initially.
- Trigger the lazy route and confirm the second module file is requested only after the trigger.
- Evaluate a symbol from the lazy namespace in the console to confirm it resolves after loading.
- With :advanced optimizations, watch the compiler output for extern warnings. JavaScript APIs used inside lazy-loaded code need externs, or minification can break interop after the module loads.
Source maps for split modules can also be harder to correlate in browser dev tools than a single bundle.
Roll back if the split misfires
Compiling writes files to your output directory. To undo, delete the generated files and rebuild with the previous single-bundle configuration, or remove the :modules key and recompile.
A practical starting point
Pick your least-visited feature, give it its own module entry, and wire the loader to that feature’s navigation event. Measure the initial bundle size before and after. If the lazy module loads only on demand and its namespaces resolve after loading, the split is working.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.