Fork me on GitHub
#clojurescript
<
2017-04-18
>
pseud11:04:19

So - I am using moment.js wrapped by cljsjs and in writing a small wrapper, I've realised I'd like to actively check that an incoming argument is a moment.js object - in Clojure I could write something like: [^String arg] or write a precondition check. What would I do in ClojureScript ?

hlolli11:04:05

@pseud there's I think js/isMoment? function packed in moment.js that you can use.

pseud11:04:28

@hlolli cool 🙂 that solves my current predicament, but I'm actually also interested in the general case, what do we do ? JS uses 'typeof' for such checks, is there a decent way of writing such code in ClojureScript in general ?

hlolli11:04:13

yes you can use a cljs/clj function type. You can create your own types. You may want to look into clojure.spec too.

urbank11:04:10

What's the advantage of using cljsjs instead of :foreign-libs ?

hlolli11:04:43

you can use externs from cljsjs and is most likely going to be a faster process using cljsjs than struggling to add externs yourself, otherwise if you set up :foreign-libs right with externs, there's no advantage, same effect.

hlolli11:04:44

well, adding that they alsa minify and uglyfy for advanced compilation, you'd have to do that yourself (giving that the lib in question doesn't provide .min.js version)

urbank12:04:52

@hlolli Sorry, what are externs for? Advanced compilation?

hlolli12:04:41

@urbank bingo, along with some other types of goog.closure compilations, tough Im mostly familiar with :advanced.

pseud12:04:52

@urbank can't say I have much experience, but ease of use I think. Anything cljsjs wrapped is as easy as adding the dependency to your build.boot (or project.clj if you're a lein-heathen :P), then adding the require line somewhere in a cljs file and you're basically off to the races.

urbank12:04:09

Ok, thanks guys! And yeah @pseud , currently a lein-heathen, not opposed to using boot though. Mostly just glad when it compiles, so I can leave that config file alone (for now) 😛

thheller12:04:06

@urbank cljsjs is just :foreign-libs already packaged for you

urbank12:04:12

@thheller I figure it's better that I learn how to do that myself, for when something isn't already packaged or goes out of date

pseud12:04:54

@urbank that argument could be made for substituting libraries for your own code, and languages for your own, and so on 🙂 It's up to you to decide what becomes an abstraction and how many problems you want to solve yourself 🙂

pseud12:04:41

(of course, I see your point still, it's a tradeoff, that's what makes it hard)

urbank12:04:18

@pseud True, but I feel like encountering a library that hasn't been cljsjs-fied or is out of date is bound to happen 🙂

thheller12:04:19

@urbank you could also take a look at the closure library date stuff. it pretty much provides everything moment does but integrates way better and supports :advanced

urbank12:04:55

@thheller Oh, I think someone else was asking about momentjs - not me

thheller12:04:22

oh hehe yeah 🙂

pseud12:04:34

what's the point of clojure spec's fdef ? If I check against my specs using pre & post-conditions, I will get failures at the point of invocation, but having defined a function spec doesn't really prevent the function returning a non-compliant value (and I assume the same is true for providing wrong input)

danielstockton12:04:52

There is #clojure-spec @pseud

pseud12:04:06

Ah, right

gonewest81816:04:42

(copying a question from the http://expo.io slack channel, because it’s probably a generic clojurescript thing)

gonewest81816:04:47

hey, clojurescript people: I started tinkering around with the default project that results from lein new exponent and this is my first look at expo, react, re-frame, and clojurescript. If I add [cljsjs/socket-io "1.6.0-0"] to my dependencies and restart everything including the simulator, emacs, cider, figwheel, and exp — and then try to (require 'cljsjs.socket-io) I get the error goog.require could not find : cljsjs.socket_io. Clearly I don’t understand this tooling (and the downside to lein templates is that one doesn’t get the learning experience of putting it all together). Any clues? what am I missing?

spinningtopsofdoom16:04:53

Is (require 'cljsjs.socket-io) being tried in the repl or on a cljs file?

gonewest81816:04:34

In the cljs.user namespace

dominicm16:04:31

@gonewest818 the exponent stuff may be a red herring, probably worth checking in chestnut 🙂

gonewest81816:04:01

@dominicm alas, until I actually understand things, it’s just trading one kind of magic for another…

dominicm16:04:48

@gonewest818 chestnut is a standard cljs setup (for the browser). I'd try socket-io there. If it still errors, the problem is with the pkg, otherwise it's with exponent.

spinningtopsofdoom16:04:09

I'd go through the quick start first (https://clojurescript.org/guides/quick-start) if this your first look at clojurescript

gonewest81816:04:34

fair enough, rtfm

spinningtopsofdoom16:04:34

Here's a guide through some clojurescript basics (https://github.com/magomimmo/modern-cljs)

plexus16:04:39

@gonewest818 getting the clojurescript compiler options right can be tricky, which is exactly why people like to start with something like Chestnut. Yes it deprives you of a certain learning experience, but at least it prevents people from getting so frustrated they drop off before achieving anything

plexus16:04:15

reading the quick-start is a great starting point to get your essentials down. I would also try generating a chestnut project separately and copy over the :cljsbuild stanza from project.clj

gonewest81816:04:38

No arguments. As a newcomer, I had hoped exponent would play that role in my ramp-up experience, but perhaps not.

plexus16:04:21

I'm not familiar enough with exponent to comment on that... to give you some useful feedback at least, the error you're getting could be due to a missing/wrong :asset-path option

plexus16:04:02

if you really want to grok those compiler options and if you have a few dollars to spare I can heartily recommend this Lambda Island episode on the subject https://lambdaisland.com/episodes/clojurescript-compiler (shameless plug)

gonewest81816:04:55

@plexus - thanks, perhaps someday. I’ll start from the clojurescript guides and the modern tutorials as suggested.

plexus16:04:05

(or just register and watch it on the one week free trial)

gonewest81816:04:19

@dominicm Using chestnut I can add the project dependency and (require 'cljsjs.socket-io) in the browser repl. Suggests the packaging is okay.

dominicm16:04:45

@gonewest818 If you're getting into this to learn, I will mention that exponent is a pretty new area afaik, so things may still be a little wonky. I'd recommend building up a cljs app from scratch 🙂.

tech_hutch19:04:03

Does :refer-clojure :rename work in CLJS? I'm still getting warnings when compiling.

tech_hutch19:04:42

I'm using the latest or close to the latest version.

anmonteiro19:04:04

oh you mean :rename inside :refer-clojure

anmonteiro19:04:09

I don’t recall if I implemented that

tech_hutch19:04:36

The article says it should work

anmonteiro19:04:43

yeah looking at the source it should be there

tech_hutch19:04:06

Okay. I'll try updating CLJS real quick.

tech_hutch19:04:30

(I haven't set up my project with lein yet, so I'm just downloading the jar file manually.)

anmonteiro19:04:21

there might be a bug somewhere

tech_hutch19:04:25

@anmonteiro I'm still getting the warnings.

tech_hutch19:04:49

Wait, never mind.

tech_hutch19:04:15

I just compiled again, and there aren't any.

grav20:04:35

When doing a compilation with optimizations :advanced, I end up with a 300 kb js file. My deps are cljs and reagent. I know I’m pulling in React, but the final file size still sounds a bit large to me?

grav20:04:13

Btw, [org.clojure/core.async “0.2.395” :exclusions [org.clojure/tools.reader]] was supplied by the figwheel template. Is it needed for anything? And if not, is it automatically removed by the advanced compilation?

dnolen20:04:24

@grav if it’s going to be gzipped that size doesn’t seem interesting

grav20:04:35

But then why optimize at all?

dnolen20:04:54

what you’re saying doesn’t make sense

dnolen20:04:59

it would be way bigger than 300k

dnolen20:04:27

you do realize that React by itself is nearly 150k?

dnolen20:04:37

production minified React

grav20:04:32

No, I wasn’t aware. In that context 300k does not seem like a lot. And you’re right, it’s gzip’ed. It’s just not obvious in Chrome’s devtools

dnolen20:04:16

Chrome should show the gzipped payload if you have gzipping configured

grav20:04:26

Ah, nginx wasn’t configured for gzipping anything but html. Now it went down to ~80k. Thanks for the pointers 🙂

dnolen20:04:52

@grav awesome! 🙂