babashka / nbb

Scripting in Clojure on Node.js using SCI
Eclipse Public License 1.0
863 stars 52 forks source link

Add `:reload` Support #343

Open athomasoriginal opened 11 months ago

athomasoriginal commented 11 months ago

Is your feature request related to a problem? Please describe.

I would like to run nbb from a JS program which is itself running in a "watch loop". The blocker is that dependencies of the nbb scripts won't reload when changes are made to them.

Describe the solution you'd like

I'm not opinionated about the solution, but it sounds like adding support for :reload may resolve this issue.

Describe alternatives you've considered

nbb seeing a file was modified and reloading the namespace accordingly (but this is a naive approach and may not be the responsibility of nbb itself)

Additional context

see eleventy-test-cljs for a "minimal" repro of the problem. Also refer to the original slack thread.

eval commented 3 months ago

Running into this issue as well. Any ideas @borkdude ?

borkdude commented 3 months ago

Sure, :reload support can be added. Do you want me to look into this, or do you want to have any guidance?

eval commented 3 months ago

Pointers would be nice. I guess here's where the magic should happen.... I read dev.md - what are the options to do interactive dev? (using Cider).

borkdude commented 3 months ago

Yes. If the namespace is already loaded, which is discovered here:

https://github.com/babashka/nbb/blob/6485523c78e2f0639a9fcf5eade7b55bb0af5b81/src/nbb/core.cljs#L282

then we just handle the aliases etc, but don't reload the namespace. I think right there the :reload should be handled.

Note that :reload affects all the namespaces in a require, not just the libspec you write it after.

I don't use CIDER for developing nbb, just bb dev and loading a script on the command line via cli.js and writing tests.

chr15m commented 2 days ago

Not sure if this is of any help but here is a minimal single-script reloader for nbb that seems to work reliably for me. It depends on node-watch.

(ns hello
  (:require
    ["node-watch$default" :as watch]
    [nbb.core :refer [load-file *file*]]))

(print "hello")

(defonce watcher
  (watch *file* (fn [_event-type filename]
                  (js/console.log "Reloading" filename)
                  (load-file filename))))