This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-12
Channels
- # adventofcode (67)
- # announcements (8)
- # babashka (46)
- # beginners (154)
- # calva (5)
- # cider (9)
- # clara (5)
- # clj-kondo (34)
- # cljdoc (31)
- # cljsrn (4)
- # clojure (146)
- # clojure-europe (5)
- # clojure-italy (3)
- # clojure-losangeles (2)
- # clojure-nl (149)
- # clojure-spec (22)
- # clojure-uk (73)
- # clojured (6)
- # clojurescript (95)
- # clojureverse-ops (3)
- # cryogen (7)
- # cursive (12)
- # data-science (1)
- # datomic (9)
- # docker (1)
- # emacs (1)
- # figwheel-main (1)
- # hyperfiddle (1)
- # jobs (3)
- # malli (29)
- # nrepl (2)
- # off-topic (61)
- # pathom (6)
- # pedestal (1)
- # planck (1)
- # reitit (19)
- # shadow-cljs (52)
- # spacemacs (5)
- # tools-deps (24)
- # vim (30)
- # yada (6)
I’m building some scripts for node.js using simple optimisations. I can’t figure out under which conditions my node_modules deps are included in the output js file. How do I control this? Is it expected that they are included, or should I be bundling node_modules with my js file when deploying?
I’m using lein-cljsbuild, but it looks like I should probably switch to a build.clj
file.
hmmm, if you use js/require
to require node modules, they shouldn’t go into the bundle then
not sure about normal :require
with :target :nodejs
@cfleming they are never included. you should use a separate tool like https://github.com/zeit/ncc for that.
That was my impression from all the doc I could find, but somehow it’s definitely working for me sometimes at the moment. I’m only deploying a single script which requires node files, and they’re in there.
if you include only the built-in node packages (eg. fs
, http
, etc) then there is nothing to include from node_modules
maybe you somehow ended up uising :npm-deps
which would run the code through the closure-compiler and most likely break it?
No, I’m using lein-cljsbuild and lein npm, but nothing like that. It definitely somehow includes nodemailer, rollbar etc.
Anyway, as part of modernising my project all that has broken, so at least now I know it should never have worked 🙂
you just might have those included globally or somehow accessible via the normal resolve rules when running the code
node will check your global installs and /a/b/c/node_modules
/a/b/node_modules
/a/node_modules
/node_modules
wherever you run the generated script
so if you run the code in /a/b/c/script.js
there are a bunch of location it'll check
I just tried ncc, and that also didn’t pick up my nodemailer dep. Is package.json required for that? For some reason lein npm never creates one.
I think it only processes your require
, but those need to be actual require
calls, so only js/require
. if you use cljs.nodejs/require
then they won't be picked up.
Hi. How can I create a valid json from a map? I'd expect the keywords to be come strings, but the stay keywords:
(clj->js {:title "Miss", :first "Freya", :last "Taylor"}))
=>#js {:title "Miss", :first "Freya", :last "Taylor"}
that’s just how it’s printed
Not really, it return this #js map as the result and http response parser promptly fails on the keywords: )
#js {"title" "Miss", "first" "Freya", "last" "Taylor"}
Are you sure that's not expected?
(. js/console log
(clj->js {:title "Miss", :first "Freya", :last "Taylor"}))
Then, on the browser's console
> ({title: "Miss", first: "Freya", last: "Taylor"}).title
< "Miss"
> ({title: "Miss", first: "Freya", last: "Taylor"})["title"]
< "Miss"
If something expects a proper JSON string (like an HTTP response parser probably would) and you give it a JS object, that won't work.
if you want to be super duper sure of what the data structure is on js, print it using JSON.stringuify on the console.log
(js/console.log (js/JSON.stringify (clj->js {:title "Miss", :first "Freya", :last "Taylor"}))))
this is what chrome shows
thanks for the replies. on node.js this js/JSON.stringify code results in
{\"title\":\"Miss\",\"first\":\"Freya\",\"last\":\"Taylor\"}"
which makes it promptly fail : )I guess the same way you do in js
so if I google for js catch global error
my first result is https://stackoverflow.com/questions/951791/javascript-global-error-handling
that suggests doing
<script type="text/javascript">
window.onerror = function() {
alert("Error caught");
};
</script>
I actually don't know how to do that, so I google cljs assign
and find https://stackoverflow.com/questions/7577011/javascript-interop-assignment-in-clojurescript
(set! (.-property foo) 5)
so putting those two together I suppose
(set! (.-onerror window) (fn [] (js/alert "Error caught"))
I haven't tested it but that sounds reasonable enough
this is mostly stack overflow driven but the second google search also showed https://cljs.github.io/api/cljs.core/setBANG which lends some extra confidence
Sets js-var to val using the JavaScript = operator.
is the benefit of cljs-bean mostly performance?
cool!
@feikas cljs-http.client
is quite popular and is a cljs lib. you can use node-fetch
and request
Do any of the Google Closure optimizations rely on how you structure your modules?
Hi everyone,
I need a way to parse HTML markup to hiccup on a node.js app. On the client side I used hickory for the job, which unfortunately doesn't play nice on Node because it depends on the DOM API (see here: https://github.com/davidsantiago/hickory/issues/17). I get code completion for the various hickory functions, but they end up undefined at run time. I tried (set! js/DOMParser (.-DOMParser (nodejs/require "xmldom")))
as suggested at https://github.com/davidsantiago/hickory/pull/33; however, the nodejs
namespace is undefined and I do not know how to access it. Has anyone gotten hickory to work on Node.js? Any other suggestions as to how I may convert HTML to hiccup? Many thanks!
I have seen a couple libs and intelliji can try to do this as well. Probably with cursive. Not sure anything is flawless. If its not your app then re-writting it might not be a bad idea anyway as its a time to review everything.
IntelliJi doing it would allow me to past HTMl as hiccup straight into the code, not aactually have my app do the conversion, right? Not 100% sure I understand what you mean by rewriting… rewrite my app? What need to do is fetch some HTML-strings from a DB and operate on them before rendering… I could of course operate on them as mere strings but it would be much easier to convert them to hiccup first… Or do you mean rewriting hickory? In that case I don't feel qualified for that (yet)… Thanks anyway!
ignore the rewrite comment.
i didn't understand what you were asking.
i think hiccup has a html->hiccup string converter.
i mean, it can go the other way for sure 🙂
Thanks for the idea… but I seems I'm out of luck here… hiccups readme.md says nothing about such a function and the only completions offered by Emacs are : Possible completions are: hiccups.core/defelem hiccups.core/defhtml hiccups.core/html hiccups.core/html4 hiccups.core/html5 hiccups.core/xhtml
@UMWM02TED try (js/require "xmldom")
instead of nodejs/require
Thanks! Yes I have [xmldom "0.1.27"]
in my npm dependencies. Also I have already thought here might be a typo as you suggested… (set! js/DOMParser (.-DOMParser (js/require "xmldom")))
does indeed evaluate.
But maybe I've put it in the wrong place… the weird thing is that evaluating (not calling) hickory.core/as-hiccup
gives an object whereas hickory.core/parse-fragment
gives me nil
Hi again… so the problem surfaces in two ways: If there's any namespace requiring hickory, node will refuse to run my app, because of
ReferenceError: Node is not defined
at hickory$core$node_type (/media/lapdaten/ARBEITSSD/dev/violinas_macchiato/target/out/hickory/core.cljs:37:3)
If I strip hickory from my sources (albeit not the from the project. clj) before starting running the app in node everything is fine until I try to use it. With figwheel running I can put hickory back into a nes form and thus require it. But then only certain functions like as-hiccup
are available whereas others are not (such as parse-fragment
)
Is there any place I can put the changing of the DOM-Parser to alleviate this? Evaluating it at the REPL or putting it into the script prior to any hickory-calls doesn't seem to change anything.
Even more thanks for looking at it then. I'm still rather new to ClojureScript (and Web Development in general) so maybe this is just too much for me now… I was trying to port somehting I had written for the client side to the server and stumbled across this problem… It is a bit surprising that albeit hiccup beinf so central for ClojureScript there should be no way of parsing HTML to it on node…
Anyway, for my problem at hand I should be able to handle it by working on the the string directly with regexes… so be it for now…
I guess I'll take this question to SO, maybe in the long term there will be a solution
A lot of old libs don't assume node. Many CLJS devs come from Clojure, and don't really consider server side CLJS usage
I see… does make sense of course… unfortunately the JVM is unavailable on my deployment space so I have no choice
I have put this as a questio on SO now since it seems to be non-trivial and relevant to other people… any further suggestions should better go there: https://stackoverflow.com/questions/59312825/clojurescript-hickory-on-nodejs-parse-html-to-hiccup
What are the debugging options for code thats been advanced Google Closure Compiled?
Or maybe just as relevent, is there a way to get more information or warnings on your cljs->js compiled code?