Split ClojureScript Output with :modules Code Splitting
Use the ClojureScript :modules compiler option to split advanced-compilation output into loadable JavaScript chunks instead of one monolithic bundle.
10 Jul 2026, 08:48 UTC

The problem and the useful answer
Under :optimizations :advanced, the ClojureScript compiler normally emits a single JavaScript bundle. Every visitor downloads every namespace, including code for routes and features they never open.
The :modules compiler option partitions namespaces into separate loadable chunks. The browser loads a bootstrap loader first, then fetches additional chunks only when their namespaces are required. This helps route-heavy applications avoid downloading unused code on the first visit.
How :modules partitions output
The compiler reads a map of module names. Each module lists root namespaces in :entries and an output path in :output-to. The compiler emits a bootstrap loader plus one JavaScript file per module. Inside each file it uses Closure-style goog.provide and goog.require, not native ES import or export.
Worked configuration
This example splits a core namespace from a reports namespace. Confirm the exact :modules schema against your ClojureScript compiler version; field names can differ between releases.
;; build.clj
(ns build
(:require [cljs.build.api :as b]))
(defn -main []
(b/build "src"
{:output-to "target/app.js"
:output-dir "target/cljs-out"
:optimizations :advanced
:modules {:core {:entries [app.core]
:output-to "target/js/core.js"
:module-dir "target/js/modules"}
:reports {:entries [app.reports]
:output-to "target/js/reports.js"
:module-dir "target/js/modules"}}
:externs ["externs.js"]}))
;; src/app/core.cljs
(ns app.core)
(defn ^:export init []
(js/console.log "core loaded"))
(defn ^:export load-reports []
(goog.require "app.reports")
(app.reports/show!))
;; src/app/reports.cljs
(ns app.reports)
(defn ^:export show! []
(js/console.log "reports chunk loaded"))
/* externs.js */
var app = {};
app.core = {};
app.core.init = function() {};
app.core.loadReports = function() {};
app.reports = {};
app.reports.show = function() {};
With advanced compilation, the compiler renames JavaScript properties. If app.core calls app.reports/show! from another chunk, both chunks must agree on the mangled name. Externs declare the shared API so the compiler preserves those names across modules.
Serving chunks and load order
Serve the bootstrap loader and every module chunk from the same origin. Load the bootstrap script before any page code that calls goog.require. Loading a chunk before its bootstrap produces a goog is not defined error. The loader script is responsible for fetching additional chunks; do not load module files with plain <script> tags unless your loader expects that order.
Limits and common mistakes
| Issue | Effect |
|---|---|
A namespace is omitted from every :entries list | Its code is emitted in no chunk, so goog.require fails at runtime. |
| Missing or incomplete externs | Advanced compilation renames shared properties differently in each chunk, breaking interop between modules. |
| Chunk loads before bootstrap loader | Browser reports goog is not defined. |
| Expecting native ES modules | :modules emits Closure-style goog.provide and goog.require; it cannot be consumed directly as ES import/export without a loader or bundler. |
| CSS, images, or other assets | :modules only partitions ClojureScript-compiled JavaScript namespaces; it does not split non-JS assets. |
Verify the build
- Run the build from the project root. The command depends on your tooling; for a Clojure build script it is typically
clj build.cljorclojure -M:build. - List the output directory:
ls target/js. You should see the bootstrap file,core.js, andreports.js. - Open the built page in a browser. Open DevTools Network, trigger the action that loads reports, and confirm
reports.jsappears only after that interaction. - Watch the compiler console for warnings about unreachable module entries or namespaces assigned to more than one module.
Command context and risks
- Where to run: Terminal in the project root, where
build.cljor yourdeps.ednlives. - Permissions: Write access to the configured output directory, for example
target/js. - Placeholder: Replace
target/jsandsrcwith your actual source and output paths. - Risk: Stale chunks remain in the output directory if you rename or remove namespaces but do not rebuild. Clean the output directory before deploying.
Undo the change
Because this is a build configuration, rollback means removing the :modules key and returning to a single :output-to bundle, then deleting the generated target/js directory so stale chunks are not deployed.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.