Re: [ClojureScript] Re: How to require a library only if it is on the classpath?
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. On Wed, 13 Feb 2019, 11:10 Yuri Govorushchenko 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. > > -- > 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 > clojurescript+unsubscr...@googlegroups.com. > To post to this group, send email to clojurescript@googlegroups.com. > Visit this group at https://groups.google.com/group/clojurescript. > -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
Re: [ClojureScript] Re: How to require a library only if it is on the classpath?
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. -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
Re: [ClojureScript] Re: How to require a library only if it is on the classpath?
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. On Tue, Feb 12, 2019 at 9:52 AM Yuri Govorushchenko wrote: > > 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. > > -- > 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 > clojurescript+unsubscr...@googlegroups.com. > To post to this group, send email to clojurescript@googlegroups.com. > Visit this group at https://groups.google.com/group/clojurescript. > -- @henryw374 -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
[ClojureScript] Re: How to require a library only if it is on the classpath?
> 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. -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
[ClojureScript] Re: How to require a library only if it is on the classpath?
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. On Monday, February 11, 2019 at 11:40:35 PM UTC+1, Yuri Govorushchenko wrote: > > > 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/. > -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
[ClojureScript] Re: How to require a library only if it is on the classpath?
> 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/. -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
[ClojureScript] Re: How to require a library only if it is on the classpath?
Thanks, I appreciate the comments Chris and Thomas. I had not seen lazy-require -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
[ClojureScript] Re: How to require a library only if it is on the classpath?
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. On Friday, February 8, 2019 at 9:43:03 AM UTC+1, Henry Widd wrote: > > 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 > -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.
[ClojureScript] Re: How to require a library only if it is on the classpath?
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 https://github.com/clojure-goes-fast/lazy-require 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 -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at https://groups.google.com/group/clojurescript.