This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-01-09
Channels
- # announcements (28)
- # babashka (8)
- # bangalore-clj (1)
- # beginners (123)
- # boot (1)
- # bristol-clojurians (1)
- # calva (3)
- # cider (30)
- # clj-kondo (42)
- # cljs-dev (5)
- # clojure (260)
- # clojure-dev (11)
- # clojure-europe (7)
- # clojure-india (1)
- # clojure-italy (2)
- # clojure-losangeles (5)
- # clojure-nl (5)
- # clojure-portugal (15)
- # clojure-uk (51)
- # clojurescript (69)
- # cursive (6)
- # data-science (21)
- # datascript (17)
- # datomic (1)
- # emacs (29)
- # figwheel-main (11)
- # fulcro (89)
- # graphql (5)
- # hoplon (2)
- # hugsql (6)
- # jobs (11)
- # juxt (1)
- # leiningen (7)
- # luminus (1)
- # malli (3)
- # off-topic (64)
- # pathom (32)
- # project-updates (1)
- # re-frame (9)
- # reagent (10)
- # reitit (21)
- # ring (5)
- # ring-swagger (1)
- # shadow-cljs (8)
- # spacemacs (6)
- # xtdb (4)
Sorry to come in with such a newbie question, but would anyone please know why I would have references to window
and document
when I generate .js files with the compiler option set to :nodejs?
if the CLJS code includes references to window
or document
, they will not be removed
the :nodejs
target for shadow-cljs simply does some extra work for you to optimize it correctly for Node.js. It won’t remove code that won’t work in Node.js
This is code that was in separate .clj and .cljs files. I already bring the code in as a library. But it needed an update, so in the process I decided to merge them into a .cljc file (The two original files were almost the same anyway). But now I can’t run the test, because when I try it tells me that I have references to document and window 
You can maybe compile with :optimizations :none, :pretty-print true
and then grep the output for references to window
or document
.
I’ve usually found it straightforward to translate from js to cljs with those settings.
@quoll actually, did you by chance change the optimization level for your code? IIRC some of the optimizations will attempt to write a script tag to the dom.
I’m not sure what you mean by “translate from JS to cljs”. Do you mean, so that I can find the code that generated the reference?
:optimizations :none
is a bit difficult:
Assert failed: :nodejs target with :none optimizations requires a :main entry
(not (and (= target :nodejs) (= optimizations :none) (not (contains? opts :main))))
This is the code that’s reported (reformated by hand):
goog.testing.TestCase.prototype.getName = function() { return this.name_ };
goog.testing.TestCase.maxRunTime = 200;
goog.testing.TestCase.protectedSetTimeout_ = goog.global.setTimeout;
goog.testing.TestCase.protectedClearTimeout_ = goog.global.clearTimeout;
goog.testing.TestCase.protectedDate_ = Date;
goog.testing.TestCase.protectedPerformance_ = window.performance && window.performance.now ? performance : null;
goog.testing.TestCase.currentTestName = null;
It’s that protectedPerformance_
value that causes the errorThe original code was all .clj
. To make it compatible with ClojureScript, he copied the file to .cljs and modified it. (so far, so good)
In my process of merging the .clj
and .cljs
files, into a single .cljc
I had also hoped to merge the tests.
I just looked at this .cljs
test file, and I see that he has incorporated:
(:import [goog.testing TestRunner TestCase])
The code then creates these objects:
(def tr (TestRunner.))
(def test (TestCase. "The Project"))
(defn test-always []
(parser-result? 5 (p/always 5) "")
(parser-result? 5 (p/always 5) "abc"))
(.add test (TestCase.Test. "test-always" test-always))
(defn test-nxt []
(parser-result? 5 (p/nxt (p/always 3)
(p/always 5)) ""))
(.add test (TestCase.Test. "test-nxt" test-nxt))
ah ok, yeah I was thinking, “surely cljs doesn’t depend on something that can’t be ported to node”
I just caused myself grief by trying to make it work along the way. (I’m thinking that if I check the library out clean, then the ClojureScript tests still don’t work. I forgot to check them clean)
Can def
s be marked for running by the compiler? e.g. statically run this
(def v1 {:key :val})
(def v2 {:key2 :val2})
(def v3 (merge v1 v2))
so that the program startup doesn't get hung up with some small calculations?I'd like to have the compiler replace the merge with {:key :val :key2 :val2}
by running the code during compilation. I have a large cljs app that's beginning to slow down from some more complex code in defs like this
Some static config stuff is all being generated. It's not a ton of time, maybe <50ms right now, but I'd like to have a solution before it gets bigger.
Everything can be done at compile time. It's just like writing clojure @U5P29DSUS.
unlike on JVM where compiler runs at runtime, so there is no difference from that point of view
That's the best solution I guess. I was hoping for a cleaner solution because I have some chains that are longer. That would require maybe everything being passed to a macro and the macro evaling to a map with the results being put in individual defs
maybe something like https://github.com/tonsky/datascript
Thanks! Cool projects, but not applicable to my issue here. https://clojurians.slack.com/archives/C03S1KBA2/p1578541758132000
Anyone have suggestions on what to read and where to start for deploying CLJS to AWS Lambda?
What kind of tooling are you planning to use? I mean are you planning to use Serverless Framework, SAM, or setup the infra yourself? Depending on your preferences there are some existing tools and examples.
In the past I’ve used https://github.com/nervous-systems/serverless-cljs-plugin with success but at the moment my go-to-choice is using shadow-cljs
to compile CLJS -> JS and then upload the code and infrastructure with Serverless Framework. I have a small example project here https://github.com/vharmain/serverless-healthcheck
There’s some discusssion in https://clojureverse.org/t/cljs-aws-lambda/2183 and also a bit inactive #aws-lambda
@U6N4HSMFW thank you so much for all this great info! Very helpful
How can i write serialized java objects into a file and read them back into java objects in clojure? any serialization library for this?
you can just use Java serialization via Java interop
you can find some basic code in the Clojure test suite https://github.com/clojure/clojure/blob/master/test/clojure/test_clojure/serialization.clj#L12-L30
that doesn't handle the file part, but that's just a matter of layering in a FileInputStream / FileOutputStream in the chain
@cmcfarlen right, maybe there's a way to exclude those nses, but I don't know how to do that - none of the various things I tried work. AOT compilation in Clojure basically transitively compiles everything you need. Far as I know you can't link together independently AOTed things - or it's just outside of my experience how to accomplish that.
your other option is to just avoid the AOTed ClojureScript dep, this means a longer startup time - but if your workflow is heavily REPL driven then maybe that doesn't matter for you