Fork me on GitHub
#clojurescript
<
2019-10-04
>
roklenarcic11:10:12

If I have a combined front-end / back-end project, should I be marking all cljs dependencies in project.clj with :compile or :provided?

hackeryarn13:10:50

Typically you setup a separate cljsbuild task that will produce it's own build into an output folder. I would suggest looking at some sample template and setting up your project accordingly. https://clojurescript.org/guides/project-templates

pez11:10:49

Is there a way to load namespaces, as with Clojure (use '[some-ns] :reload)?

pez11:10:08

In my use case I need to load the current namespace.

pez11:10:44

OK, seems that require does what I want here. duckie

hackeryarn13:10:50

Typically you setup a separate cljsbuild task that will produce it's own build into an output folder. I would suggest looking at some sample template and setting up your project accordingly. https://clojurescript.org/guides/project-templates

borkdude18:10:28

any examples of CLJS libs that are compiled to JS and then offered as a lib on NPM?

delaguardo21:10:28

api.cljs is probably the most interesting for you - https://github.com/xapix-io/axel-f/blob/master/src/axel_f/api.cljs that’s how I shaped out api for JS runtime

borkdude21:10:57

cool. is output-wrapper a CLJS thing or a Shadow thing?

borkdude21:10:33

niice. your .circleci config is also very helpful.

borkdude21:10:25

this is exactly what I was looking for, thanks a lot

delaguardo21:10:07

your are welcome)

borkdude21:10:36

fyi, this is what I'm trying to JS-ify: https://github.com/borkdude/sci/issues/92

delaguardo21:10:41

:thumbsup: will have a deeper look to it, already spotted some places that can make outgoing bundle smaller. Just need a day or two for proper PR

borkdude14:10:47

I used your project as a template. If you have any guidance/PRs to reduce the size of the asset, more than welcome. No hurry.

borkdude18:10:54

I guess mori is one example. Should you compile to advanced or what is the best way here?

thheller18:10:13

depends on what you intend the target to be? if its going to be used in browser builds you pretty much need advanced

borkdude18:10:51

yeah, browser and node

thheller18:10:26

yeah nobody is gonna want to include 2mb of JS which you'd probably get with :simple

thheller18:10:43

fine for node but not the browser

borkdude18:10:13

are there any example repos I could look at except mori?

borkdude18:10:25

I'm not too familiar with the whole npm/JS side of things

thheller18:10:12

depends on what you are trying to do exactly I guess? shadow-cljs has :node-library support, although intented for node the release artifact works in browsers too https://shadow-cljs.github.io/docs/UsersGuide.html#target-node-library

thheller18:10:50

CLJS always bundles cljs.core so it isn't great for libraries since :advanced always presumes it optimizes the whole program not just parts of it

thheller18:10:10

fine if you include a single library written that way. not great otherwise.

borkdude18:10:22

I'll maybe try to deploy sci to npm, so you can just use it from JS directly instead of using it from CLJS: https://github.com/borkdude/sci the advanced build is around 350kb, not that great for a lean browser app, but could be useful

thheller18:10:51

yeah size if mostly the concern for the browser based stuff

borkdude18:10:40

thanks for the pointers

lilactown19:10:05

datascript is another example of a library that supports pure JS

iha222:10:13

Hey, I having some trouble importing from a cljc file. I am using @shadow-cljs. When I try and import macros from the file, it gives me an error saying that the macroexpanding of ns does not confrom to spec. I’m not sure what I am doing wrong.

andy.fingerhut22:10:58

Sounds like that error may be because of an incorrect ns form. Do you know which file it is complaining about, and if so, what that ns form looks like now?

iha222:10:51

This is the form it is complaining about

(ns components.nav.nav-component
  (:require [components.header.header-component :refer [header-component]]
            [main.routing.route-paths :refer [navigation-links]]
            [components.nav.nav-item-component :refer [nav-item-component]]
            [main.theme :refer-macros [use-style]]
            ))

iha222:10:29

the main.theme is the macros that it seems to dislike, and calva also can’t find it’s location when I try to go to definition

iha222:10:51

This is what is in the source

iha222:10:05

(ns main.theme
  (:require #?(:cljs [stylefy.core :as s])))

#?(:cljs (defmacro use-style [style] (s/use-style ~style {:class `(->> ~style meta :name)})))

andy.fingerhut22:10:16

Others probably know better, but perhaps ClojureScript only supports user-defined macros in files ending with .clj suffix?

iha222:10:30

I don’t know

iha222:10:31

It seems to dislike importing from cljc files period

andy.fingerhut22:10:47

If that form (:require #?(:cljs [stylefy.core :as s])) is ever evaluated by Clojure, I bet it might cause problems, since it would expand to (:require)

iha222:10:58

oh really?

iha222:10:05

WHat should it be instead?

iha222:10:31

Everything I found online suggests that is how you properly import from clojurescript libraries

andy.fingerhut22:10:44

I haven't tried this, but my first guess would be to move the #?(:cljs ...) so it is around the entire (:require [...]) expression

Brandon Olivier22:10:06

hi I'm working on including an existing react component library into a shadow-cljs/reagent project, but whenever I require the component, it gives me a Cannot convert a Symbol value to a string. Anybody know what that's about?

Brandon Olivier23:10:12

I figured out some of the mystery. Since these are declared as objects, you have to call (.render Component (clj->js {}))

thheller10:10:22

it happens when you try to print react elements

thheller10:10:38

ie. (prn (react/createElement "div" nil "foo"))

andy.fingerhut22:10:09

I'm shooting from the hip here, so may not be giving the best advice.

andy.fingerhut22:10:54

In Clojure (not ClojureScript) REPL:

user=> (ns foo.bar (:require))
Syntax error macroexpanding clojure.core/ns at (REPL:1:1).
((:require)) - failed: Extra input spec: :clojure.core.specs.alpha/ns-form

iha222:10:22

It looks like you were right

andy.fingerhut22:10:23

Similarly for this:

user=> (ns foo.bar (:require #?(:cljs [a b])))
Syntax error macroexpanding clojure.core/ns at (REPL:1:1).
((:require)) - failed: Extra input spec: :clojure.core.specs.alpha/ns-form

andy.fingerhut22:10:41

What you tried would probably work fine if there were other namespaces inside the (:require ...) form that did not have #?(:cljs ...) around them, because then it would turn into something that did not have an empty (:require) expression

iha222:10:39

@andy.fingerhut and the import I intended on bringing in would still work?

andy.fingerhut22:10:11

Not sure. I'd say give it a shot and see what happens 🙂

iha222:10:45

OK. Well I have one more question is you have a few more seconds,

iha222:10:00

OK! So the advice you gave me worked, but when I import in another file I get an undeclared var problem.

andy.fingerhut22:10:39

That isn't a question 🙂

iha222:10:55

It’s not?

andy.fingerhut22:10:06

It is a statement of fact, with no question.

iha222:10:41

I can do better

andy.fingerhut22:10:52

If your question is "what is wrong with my code?" then I can't see your code, so have no information to go on.

iha222:10:55

(ns components.nav.nav-component
  (:require [components.header.header-component :refer [header-component]]
            [main.routing.route-paths :refer [navigation-links]]
            [components.nav.nav-item-component :refer [nav-item-component]]
            [main.theme :refer-macros [use-style]]))

; (def nav-link-container )
(def article-list-view-style {:width "80%"
                              :list-style-type "none"})

(def nav-link-container [:ul {:style {:list-style-type "none"}}])

(js/console.log (use-style #'article-list-view-style))

iha222:10:07

That’s the code

iha222:10:18

------------------------^-------------------------------------------------------
 Use of undeclared Var components.nav.nav-component/use-style

iha222:10:27

That’s the error.

iha222:10:09

So even though there is require error, the use-style doesn’t seem to get registered or something.

andy.fingerhut22:10:15

I am not sure what is wrong. I would suggest trying putting the macro definition into a file with .clj suffix, but again, that is a guess, so perhaps a (short) waste of time.

andy.fingerhut22:10:22

Warnings are better than errors 🙂

andy.fingerhut23:10:27

This page of documentation about ClojureScript suggests that it should be possible to define macros in a .cljc file: https://clojurescript.org/about/differences#_macros

iha223:10:23

`The required namespace “utils.macros” is not available, it was required by “components/nav/nav_component.cljs”. “utils/macros.clj” was found on the classpath. Should this be a .cljs file?`

iha223:10:37

Yeah I looked at that page and didn’t get much from it. It just explained when macros and functions are processed.

andy.fingerhut23:10:10

It gives several variations of :require and :require-macros subexpressions inside of ns forms that are documented as supported.

iha223:10:59

I went through all those.

andy.fingerhut23:10:04

I do not know for sure what might be wrong with yours. The only difference I see between it and the examples on that page are that page examples have :as alias before :refer-macros, but I do not know if that is needed, or optional.

iha223:10:31

Thanks though

iha223:10:00

All the things you have said before got me through quite the roadblock 😀

andy.fingerhut23:10:08

I have fairly little experience with ClojureScript-specific issues at this point (much more with Clojure), so just because I don't know the answer, I'd guess someone else on this channel with more cljs experience could figure it out quickly.

andy.fingerhut23:10:28

If your code is in a public Github repository or something similar, you could point people at it when asking for help.

iha223:10:57

I think it’s more my inexperience. I’ve been trying for quite a while so I might be just too tired to figure it out.

iha223:10:28

I’ll take a rest and try again. I do appreciate all the help though.

iha223:10:37

I realized I don’t understand the #? well.

andy.fingerhut23:10:04

Are you familiar with #ifdef in C/C++? It is somewhat similar to that.

andy.fingerhut23:10:47

Basically the entire expression becomes nothing if it is read in by Clojure, if there is no :clj subexpression, and it becomes nothing if it is read in by ClojureScript, if there is no :cljs subexpression.

iha223:10:49

Yes. What I don’t understand is why the conditional didn’t work when nested.

andy.fingerhut23:10:12

Because (ns foo.bar (:require)) is not legal Clojure syntax

iha223:10:41

But it’s is being read by clojurescript so it should still evaluate to something

iha223:10:23

Now you are saying this, I guess what is happening now is that nothing in that file is being evaluated

andy.fingerhut23:10:41

Again, someone else could say more, but the ClojureScript compiler is written in Clojure, and is involved much more with files containing macros. I would guess Clojure is much more directly involved with macro definition files than .cljs files.

andy.fingerhut23:10:07

They probably have to be legal for both Clojure and ClojureScript compilers.

andy.fingerhut23:10:39

Otherwise the #?(:cljs ) could be eliminated and it would still work

iha223:10:29

Still faulty.

andy.fingerhut23:10:37

If nothing in a file is being evaluated, it shouldn't give you a syntax error

andy.fingerhut23:10:49

If it gives you an error, something is reading it and trying to interpret its contents.

andy.fingerhut23:10:32

I wasn't suggestion that you should remove the #?(:cljs ) by the way. But ask yourself this: why do examples you have seen include that? Why did they bother adding it if it was unnecessary?

andy.fingerhut23:10:53

My guess (not certain knowledge) is that it is necessary, because the Clojure compiler is also evaluating the files defining macros.

iha223:10:18

Yeah your right. Any I’ll keep trying stuff

andy.fingerhut22:10:00

If it still gives problems, I would try putting the macro definition in a .clj file to see if it makes a difference, but only if it doesn't work as you have just changed it to.

iha222:10:57

The clj files get a warning from shadow-cljs. I tried putting my macro in there but it came up with the aforementioned warning.