Re: [ClojureScript] Re: How to require a library only if it is on the classpath?

2019-02-13 Thread 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 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?

2019-02-12 Thread Yuri Govorushchenko
> 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?

2019-02-12 Thread Thomas Heller
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?

2019-02-11 Thread Yuri Govorushchenko
> 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?

2019-02-10 Thread Henry Widd
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?

2019-02-08 Thread Thomas Heller
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?

2019-02-08 Thread Christopher Small

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.