Fork me on GitHub
#clojurescript
<
2020-03-17
>
Maik Wild08:03:20

Hi, maybe someone can nudge me in the right direction. I’m working with #inst literal https://cljs.github.io/api/syntax/inst-literal I can’t find how to format these. Is there a tool I’m missing? I want to (for example: Display the time in the local time zone)

Maik Wild08:03:51

I could always parse the string, but that seems to be going the wrong direction.

p-himik09:03:14

Not sure what you mean. CLJS will parse #inst for you. Upon evaluation, you'll get a js/Date object that you can format any way you like.

p-himik09:03:31

cljs.user=> (.toLocaleString #inst "2020-03-17")
"3/17/2020, 2:00:00 AM"

sully09:03:02

clojurescript itself doesn't provide any more operations for #inst . #inst is just a protocol-extended javascript Date, so you can use any methods provided by Date (long winded examples https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone) or some libraries that work on Dates. However, you might see by those examples that Date has a pretty nasty API for doing anything practical, and Date is all you'll get for doing time-related operations (no time durations or periods). There are other libraries that make time-related code much easier, the prime examples being https://github.com/henryw374/cljc.java-time and https://github.com/juxt/tick. Both present a more idiomatic interface to a https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html style library, so you'll get a lot more utility and readable code.

Maik Wild09:03:31

Thank you. That’s exactly what I need.

fricze14:03:25

Hi! What’s the current state of ClojureScript advanced compilation and creating small bundles? Can you create a hello world that compiles to 2-3 lines of JS, or is it still a dream?

Vishal Gautam15:03:06

If you are looking to write a simple hello world function, then you might we well right it in JS…

fricze15:03:03

right, that was exemplary question though 😉 the reality is: some data fetching, few event listeners and few DOM changes

nwjsmith15:03:24

It's state-of-the-art. Supports code-splitting (https://clojurescript.org/guides/code-splitting) and has dead-code elimination built-in.

nwjsmith15:03:23

If you're looking specifically at the size of a compiled (println "Hello, world!") I'm not sure, but for any practical application it's going to be hard to beat

fricze15:03:07

I know it’s state of the art 🙂 I work in Clojure/ClojureScript codebase with some little JS parts

fricze15:03:42

I was curious is it possible to generate all code from ClJS and can I compete with compiled pure JS size 😉

p-himik15:03:05

FWIW even with a single CLJS file that has nothing but the ns definition and with the :fn-invoke-direct true compiler option specified, the output file is still 91KB.

fricze15:03:10

k, thanks!

hindol15:03:33

Hi, I am completely new to CLJS but familiar with Clojure. How do I consume this TypeScript library in CLJS? https://github.com/ethereumjs/rlp No CLJSJS version available.

p-himik15:03:04

If you're using Shadow-CLJS, you can just install it via NPM/Yarn and consult this documentation section: https://shadow-cljs.github.io/docs/UsersGuide.html#_using_npm_packages

hindol15:03:18

Yes, I am using shadow. I thought it is only for hot reload. Looks like I have a lot to learn. Thanks!

👍 4
dpsutton15:03:25

Shadow won’t work with cljsjs even if a port for this one existed

hindol15:03:49

Hmm, interesting. I am completely new to CLJS. What are the de facto tools used by developers now-a-days?

p-himik15:03:53

Different people still use different things. There was a Clojure survey recently - the percentages were mentioned there.

p-himik15:03:20

I have migrated from Lein+Figwheel to tools.deps+Shadow-CLJS, haven't looked back.

hindol15:03:25

Okay, here's another beginner level question. I created a shadow-cljs project today with lein new shadow-cljs +reagent and looks like the project has no project.clj. Only a shadow-cljs.edn. Where does Lein/deps.edn fit in? Do you use deps.edn just for running some commands?

p-himik15:03:02

I use deps.edn for all dependencies. Seems like you just need to read through the documentation of Shadow-CLJS. :)

💯 4
athomasoriginal15:03:26

To provide an alternative approach, my go-to is figwheel+clj/clojure . Here is a blog I wrote outlining what my approach looks like: https://betweentwoparens.com/start-a-clojurescript-app-from-scratch and I also created a sort of “create react (reagent) app” like tool here: https://github.com/athomasoriginal/create-reagent-app

dnolen15:03:13

@andrzej.fricze only if you restrict yourself to JS style coding

fricze15:03:21

I know I can avoid loading ie. Clojure data structures, but the minimal output is still 91kb, isn’t it? can I get smaller than that?

dnolen15:03:24

the minimal output is not 91k, though the last release had a regression

dnolen15:03:39

it's like 4-5k before gzipping, just a few hundred bytes after

fricze16:03:06

oh, 4-5k is interesting 😄

fricze16:03:52

I can work with that. how can I achieve that? do you maybe have sample project with proper config?

dnolen16:03:02

there's nothing to do

dnolen16:03:17

it just works use 1.10.520

dnolen16:03:30

don't use data structures, don't use println (use console.log only)

dnolen16:03:37

your output will be tiny

parrot 4
fricze16:03:41

awesome, thanks!

fricze16:03:56

will check that in a second

fricze16:03:27

I forgot that println might be costly

dnolen16:03:33

also another way to get smaller bundles is to stop using random JS dependencies

dnolen16:03:41

a lot of my old blog posts were ClojureScript only

dnolen16:03:00

core.async - none avoidance of the above, and the output is 20K gzipped

fricze16:03:42

that’s great 😄

fricze16:03:24

thanks once again, for some reason I was sure that 91k is the smallest possible

dnolen16:03:54

literally self hosted ClojureScript (which is not even advanced compiled) is smaller than a lot of React projects it seems

dnolen16:03:09

which isn't a problem w/ React it's a problem w/ dependencies

dnolen16:03:14

& no comprehensive DCE strategy

fricze16:03:21

so it seems that I really can use ClojureScript everywhere 🙂 just compiled minimal example to 5k :thumbsup:

fricze16:03:59

of course, have to write only JS-like code, as you said, but I still have the same syntax, editor, repl and tooling

fricze16:03:13

so it’s still better for me than JS

lukasz16:03:50

@andrzej.fricze out of interest, how did you set it all up?

fricze16:03:11

did everything according to this instruction https://clojurescript.org/guides/quick-start

👍 4
fricze16:03:24

just changed println to js/console.log

p-himik16:03:02

Interesting. I downgraded to 1.10.520 as suggested by dnolen, and got a 1.5kB file that has only the ns declaration and a single js/console.log statement. Using shadow-cljs.

👍 4
fricze19:03:44

tried that and got 637 bytes

fricze19:03:50

we’re living in good times

fricze19:03:25

(besides global warming and virus mayhem)

p-himik19:03:09

Not zipped?

p-himik19:03:47

Oh wait. I just got... 201 bytes.

p-himik19:03:56

(function(){
if("undefined"!==typeof Symbol){var a=Symbol;"object"!=typeof a||!a||a instanceof Array||a instanceof Object||Object.prototype.toString.call(a)};console.log("Hello world!");
}).call(this);
This if block looks completely useless though, but I could live with that.

fricze16:03:40

I have to try again with shadow then

fricze16:03:23

how did you manage to downgrade with shadow-cljs though?

fricze16:03:37

when I set :dependencies [[org.clojure/clojurescript "1.10.520"]] it complains

fricze16:03:55

WARNING: The org.clojure/clojurescript dependency in shadow-cljs.edn was ignored. Default version is used and override is not allowed to ensure compatibility.

p-himik16:03:44

I used :deps true and specified the CLJS version there. Not sure how severe the warning is in this case.

fricze16:03:42

:thumbsup:

jjttjj17:03:16

I won't keep spamming asking this every day but does anyone know if there is any way to use a macro from an external cljs library in cljs.js/eval-string? I've been stuck on trying to figure this out for a few days

lilactown17:03:14

@jjttjj it depends on if the macro is defined in a CLJS namespace and if it’s compatible with self-hosted CLJS

jjttjj17:03:55

I did have it working with self-hosted js in an earlier experiment with lumo

lilactown17:03:09

you’re trying to use javelin, right?

jjttjj17:03:15

yes exactly

jjttjj17:03:27

(I have a fork I got working with lumo)

lilactown17:03:09

oh ok. so you’ve already migrated the core.clj with the macros into a .cljs namespace?

jjttjj17:03:55

Oh wait it is still a clj file

lilactown17:03:09

yeah, you can’t load a .clj file into self hosted clojurescript. all the macros need to be in a CLJS namespace, and need to not depend on any JVM Clojure constructs or behavior

jjttjj17:03:46

Cool, yeah I removed the JVM stuff, is it then just a matter of changing the extension? Trying this now

lilactown17:03:35

you’ll need to merge it with the core.cljs

jjttjj17:03:20

Also will this then be possible to run from the browser

lilactown17:03:36

if it’s self-hosting compatible, it probably will be be compatible in both lumo (self hosted on Node.js) and self-hosted in the browser

lilactown17:03:47

as long as you don’t rely on any Node.js- or lumo-specific APIs

jjttjj17:03:23

I still should have to do something like this, right? https://clojurescript.org/guides/self-hosting

lilactown17:03:03

yes, self-hosting in the browser is non-trivial

lilactown17:03:25

it’s probably easier to use the .clj macros when building in the browser

jjttjj17:03:11

you mean just use clojurescript "normally"?

jjttjj17:03:33

I'm mainly just trying to get the javelin macros inside a in browser repl ui

jjttjj17:03:56

But also good to know if this is gonna be a dead end

lilactown17:03:23

ah 😄 you’ll want to do browser self-hosting then

jjttjj17:03:40

just by following that guide I sent?

jjttjj17:03:23

I guess I'd probably have to include javelin itself to get the source or somehow reference the resource files

lilactown17:03:25

that’s a fairly basic guide. if you use e.g. shadow-cljs, it has a guide as well

lilactown17:03:45

yes, you’ll need to bundle all of the code that you’ll want to have available

jjttjj17:03:46

do you happen to know what to search for for the shadow guide?

lilactown17:03:33

I googled “shadow-cljs self-hosting” and this was the third result: https://code.thheller.com/blog/shadow-cljs/2017/10/14/bootstrap-support.html

jjttjj17:03:49

great, thanks!

jjttjj17:03:59

great, giving this a shot, thanks so much!

JAtkins17:03:25

On the topic of macros, I also have a question. I am making a calculation register that can run entirely at compile time. Alot of data is built up in an atom there though, and I would like to transfer that data to the compiled app. Where I am now.

JAtkins17:03:05

; clj
(def x (atom {}))

(defmacro reg-thing [k v]
  (swap! x assoc k v))

(defmacro get-x [] @x) 

JAtkins17:03:18

; cljs
(macros/reg-thing :a 1)
(macros/reg-thing :b 2)
(def x (macros/get-x))

JAtkins17:03:06

But this doesn't seem to work. All I get back is a nil x.

JAtkins17:03:20

Whoops, sorry. I'm an idiot. I evaluated (comment x) instead of x. The data does end up coming through. I'll check out the link.

p-himik17:03:04

What prevents you from writing everything in Clojure and just send the resulting value to CLJS via a single macro?

JAtkins17:03:14

Nothing particularly. May be a good idea. Right now the 2nd file is a cljc.

p-himik17:03:20

It doesn't really matter that it's cljc - AFAIK it's just like cljs+clj but without duplication and with potential for targets other than cljs and clj.