How to require a library only if it is on the classpath?

80 views
Skip to first unread message

Henry Widd

unread,
Feb 8, 2019, 3:43:03 AM2/8/19
to ClojureScript
I'm trying to write some code (to run at compilation time) that will only do a require if a library is present. Initially I thought of trying something like this:

(defmacro include-if-present []
 
(when (io/resource "foo/bar.cljc")
   
'(require 'foo.bar)))


Now I understand that 'require' in cljs is intended as a repl-only thing  which expands to an ns form: https://clojurescript.org/guides/ns-forms#_the_require_and_require_macros_macros

So, rather than 'cljs.core/require', then perhaps what'd work it to be able to hook in at the point the ns forms as read initially. 

Any ideas please let me know.

Thanks,
Henry

Christopher Small

unread,
Feb 8, 2019, 1:08:50 PM2/8/19
to ClojureScript

I realize this doesn't directly answer you're problem, as you're asking about how you might do this in ClojureScript, but you may still be interested to take a look at


If you figure out a solution, it could be helpful for that library to have a cljc implementation of the underlying idea here.

Good luck

Chris

Thomas Heller

unread,
Feb 8, 2019, 4:59:54 PM2/8/19
to ClojureScript
What you are trying to do in include-if-present is not possible in CLJS.

require is completely static and you can't do dynamic require outside the REPL.

Henry Widd

unread,
Feb 10, 2019, 3:16:29 AM2/10/19
to ClojureScript
Thanks, I appreciate the comments Chris and Thomas. I had not seen lazy-require

Yuri Govorushchenko

unread,
Feb 11, 2019, 5:40:35 PM2/11/19
to ClojureScript
> Now I understand that 'require' in cljs is intended as a repl-only thing  which expands to an ns form: https://clojurescript.org/guides/ns-forms#_the_require_and_require_macros_macros

It should be OK to have several requires at the top of the file. Source: https://anmonteiro.com/2016/10/clojurescript-require-outside-ns/.

Thomas Heller

unread,
Feb 12, 2019, 3:52:43 AM2/12/19
to ClojureScript
That still does not allow you to do anything you couldn't do via the ns form directly. require outside ns is a misleading topic and is still completely static. It was just added to allow compiling scripts that don't have a ns form. You still can't do conditionals or run any other code inbetween them.

The reason that dynamic require is not possible in CLJS is that there is no sync/blocking IO in Browser JS. In Clojure you can simply do the work and return once everything is loaded properly. In JS you can't do that because you have to go async/non-blocking for the IO to load the files. So all of the remainder of the program would continue running and loading only start once the program yielded control back to the runtime. In addition to that the Closure Compiler doesn't really support dynamic requires in :advanced optimizations.

Yuri Govorushchenko

unread,
Feb 12, 2019, 4:52:18 AM2/12/19
to ClojureScript
> That still does not allow you to do anything you couldn't do via the ns form directly. require outside ns is a misleading topic and is still completely static.

I guess it's not a blocker for OP because the intention was to do a conditional require during compilation (and not during execution in browser)?

Of course, I'm not sure what real root problem it solves, maybe there is a more idiomatic solution possible.

Henry Widd

unread,
Feb 13, 2019, 4:56:51 AM2/13/19
to clojur...@googlegroups.com
To add a bit more context about the original problem:

library A depends on library B.

library B in this case modifies the printer. A does not use any functions from it. 

That is the default setup, but some users will not want the printer modified when using library A so I was hoping that they could exlcude it from the dependencies and library A would not require it in that case.

Another approach that will work for me is having the user set a property, similar to how you do with e.g. -Dclojure.spec.compile-asserts=false, but I thought classpath exclusion might be good, if there was a way it could be made to work.

--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/cUbKSQ_bm0I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.


--
@henryw374

Yuri Govorushchenko

unread,
Feb 13, 2019, 6:10:29 AM2/13/19
to ClojureScript
After quickly trying this out it seems impossible to have a "dangling" require after ns form, probably because (quoting the article I linked to in previous comment):

A file can have one of:
  • a namespace declaration, or
  • (possibly several) require forms, or
So it looks like indeed there's no way to do a conditional require and library A should provide some other way to allow user decide whether printing should be affected. Some solutions off the top of my head:

1) For "property-based" configuration you could try using :closure-defines compiler feature.
2) There's also an :external-config pattern which I find easier to work with (example: https://gist.github.com/metametadata/bb00917e09463f6ce04f0e50ccc0740a). But it seems to not be recommended anymore after shared AOT cache was introduced (see https://clojurescript.org/news/2018-03-28-shared-aot-cache).
3) https://github.com/binaryage/env-config (which I haven't tried).
4) Printer could be modified only on demand instead of doing a global side effect on requiring libA ns. E.g. add an explicit `libA.core/enhance-printer!` function. This is the approach I saw in libs which affect Clojure test reporting (ultra and humane-test-output) where they expose `activate!` function.

Henry Widd

unread,
Feb 14, 2019, 4:44:29 AM2/14/19
to clojur...@googlegroups.com
I appreciate the suggestions here. Modifying the reader does need to be the default so that rules out 4.

 This setting should apply for both clojure and cljs. jvm-opt would work and may be the only option? hence why spec/compile-assertions is configured that way.

--
Reply all
Reply to author
Forward
0 new messages