babashka / sci

Configurable Clojure/Script interpreter suitable for scripting and Clojure DSLs
Eclipse Public License 1.0
1.21k stars 86 forks source link

Support npm style string require #645

Closed djblue closed 2 years ago

djblue commented 2 years ago

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

When using ClojureScript, it is common to use the following syntax:

(ns my-app (:require ["react" :as react]))

This denotes a javascript dependency, usually from npm.

Describe the solution you'd like

By using an explicit get here, it would allow npm dependencies to participate in the name resolution algorithm.

Describe alternatives you've considered

None so far.

Also, I'd be more than happy to put up a PR if you think this is a good idea.

borkdude commented 2 years ago

@djblue Yes, an explicit get would be good, PR welcome. I'm curious how you are going to use this: are you going to convert the React module to a Clojure map and put it into the namespaces config?

djblue commented 2 years ago

Yup, I'm doing something like:

(defn- import-npm [m]
  (reduce-kv
   (fn [m k v]
     (assoc m (symbol k) v))
   {}
   (js->clj m)))

(def namespaces
  (merge
   {"react" (import-npm react)}
   ...))

For exposing npm deps to portal sci extensions.

borkdude commented 2 years ago

@djblue ok, that's cool. Are you including react before-hand or dynamically while portal is running?

If you need to do anything async in the future (ES modules are going to force us to do that probably) then I recommend taking a look at what nbb is doing. I can tell you more about this, if you end up needing that some time.

borkdude commented 2 years ago

@djblue Btw, I think it would be good to have at least one test for this to prevent future regressions.