Hotload Libraries into a running REPL
add-libs
"hot-loads" one or more libraries into a running REPL, avoiding the need to restart the REPL just to use a new library with the project.
Although restarting a REPL is usually very quick, it will loose all application state too. This loss of state be mitigated by using rich comments, evaluating data structures that set useful application states.
add-libs
is typically called from a rich comment block or a separate dev/user.clj
file, to avoid being loaded with application code.
Once hot-loaded, a library namespace can be required as if the dependency had been added to the project configuration before the REPL started.
practicalli/clojure-webapp-hotload-libraries is an example project that uses REPL driven development and hot loading of libraries to build a very simple web server using http-kit and hiccup.
Add-libs is an experimental feature
The add-libs
function is regarded as an experimental feature of clojure.tools.deps.alpha
library and is currently part of the add-libs3 branch. The add-libs implementation and function signature may change in future.
Include add-libs library dependency
To use add-libs, its library must be included as a dependency when starting the REPL. As add-libs is a development tool, it should be added via an alias, either in the project deps.edn
or user level configuration to use with all projects.
:lib/hotload
alias defined in practicalli/clojure-deps-edn adds the latest SHA commit from the add-libs3
branch of clojure.tools.deps.alpha
library as an extra dependency.
Include the :lib/hotload
alias when starting the REPL, using any of the available Clojure CLI execution options (-A
,-M
,-X
,-T
).
See Terminal REPL and Clojure Editor pages for examples.
Edit the project deps.edn
configuration and add an :lib/hotload
alias for the clojure.tools.deps.alpha.repl
library. Or add an alias to the user level configuration for use with any Clojure CLI project.
The add-libs
code is on a separate add-libs3 branch, so requires the SHA from the head of add-libs3 branch
:lib/hotload
{:extra-deps {org.clojure/tools.deps.alpha
{:git/url "https://github.com/clojure/tools.deps.alpha"
:git/sha "e4fb92eef724fa39e29b39cc2b1a850567d490dd"}}}
Alias example from practicalli/clojure-deps-edn
Using add-libs to hotload libraries
There are several approaches taken for hotloading libraries, including: