Hot Reloading
Browse docs
The concept of hot reloading is popular for rapid development where you want your code to update as you change it, while preserving things like open file handles, web servers, etc.
You don’t want your entire website to reload whenever you edit your helper function.
This is why lde ships hot reloading via lde run --hot, which watches your src tree, and patches in only changed files.
Quickstart
- Create a project and cd into it:
lde new ./hello-hot- Replace
src/init.lua:
local greet = require("hello-hot.greet")
print(greet("world"))- Make
src/greet.lua:
return function(name)
return "hello, " .. name
end- Run it:
lde run --hot- Change the greeting in
src/greet.luaand save.
--hot vs --watch
The difference between these two is simple.
--watch re-runs your main entrypoint on any file changing, even a single helper file.
--hot only replaces the changed modules.
Outside a package
--hot works for loose scripts too:
lde ./test.lua --hotThe current directory is watched and require() caches are patched the same way.
package.hot
This table is added to the package library only under --hot runs.
It can be used to register callbacks that run and are passed the module path whenever a file is hotreloaded.
if package.hot then
package.hot.accept(|m| -> print("module reloaded", m))
endThe m here is the module’s require() path.
It runs before your entrypoint runs, but after lde rebuilds your package (build.lua runs, if exists)
package.hot.poll()
This function exists to pump the hotreloading loader, so that even in entirely blocking code, such as an event loop doing while true do end, you can still support hotreloading.
while running do
handleEvents()
if package.hot then package.hot.poll() end
endLimitations
- A program that blocks will not reload. For example, just a
while true do endloop will block forever as control is never relinquished to lde. You can fix this withpackage.hot.poll()as seen above.