Clojurians
#lumo
<
2018-05-29
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

borkdude15:05:18

I do (:require [moment]) and sometimes I get the error:

No such namespace: moment, could not locate moment.cljs, moment.cljc, or JavaScript source providing “moment”

borkdude15:05:21

but not always

borkdude15:05:13

This is the classpath I feed into lumo:

src:/home/borkdude/.m2/repository/org/clojure/clojure/1.9.0/clojure-1.9.0.jar:/home/borkdude/.m2/repository/org/clojure/tools.cli/0.3.7/tools.cli-0.3.7.jar:/home/borkdude/.m2/repository/org/webjars/npm/moment/2.22.1/moment-2.22.1.jar:/home/borkdude/.m2/repository/org/webjars/npm/nodemailer/4.6.5/nodemailer-4.6.5.jar:/home/borkdude/.m2/repository/org/clojure/spec.alpha/0.1.143/spec.alpha-0.1.143.jar:/home/borkdude/.m2/repository/org/clojure/core.specs.alpha/0.1.24/core.specs.alpha-0.1.24.jar

borkdude15:05:57

It’s this script that I’m using. Locally it works fine, on my server no. I’m using a newer lumo version on my server. https://github.com/borkdude/balcony/blob/master/lumo/scripts/balcony.cljs

borkdude15:05:17

Execute by ./balcony.cljs

borkdude15:05:14

I removed the -K option as well. I installed lumo 1.8.0 on my server and get:

No such namespace: moment, could not locate moment.cljs, moment.cljc, or JavaScript source providing “moment” in file cljs/tools/cli.cljs

borkdude15:05:53

When I try:

(def moment (node/require "moment"))
I get:
Cannot find module ‘moment’

borkdude15:05:07

I’m so close…

borkdude15:05:43

ah wait, locally it was still using my node_modules folder probably…

borkdude15:05:47

so I need to find a way how to get these webjars working, probably using foreign libs or something

borkdude15:05:22

To summarize: can I load javascript from a jar file in lumo, preferably inside the script and not in some external file

richiardiandrea16:05:01

@borkdude you are not compiling down with browserify anymore? Because I though it would just concatenate all the js deps with that so no need for carrying it with you

borkdude16:05:31

@richiardiandrea I’m trying a different approach now, the one that I also took with clj. Using webjars it might be possible to avoid the build step.

richiardiandrea16:05:48

If not then the customary method is to carry the node_modules folder with you

borkdude16:05:04

so I’m not loading any deps via npm anymore

richiardiandrea16:05:10

But I don't think the folder is read from inside the jar

borkdude16:05:50

Well you can load .cljs files from a jar, why not .js files?

richiardiandrea16:05:16

Uhm maybe :foreign-libs

richiardiandrea16:05:09

Not sure at this point, I never really used :foreign-libs. It should maybe be possible given the cljsjs project is doing exactly that

borkdude16:05:07

now the thing I have to know is, can I load foreign libs from the repl

richiardiandrea16:05:08

Never explored that path, let's wait for @anmonteiro. I would say you should be able to

borkdude16:05:41

maybe I should use cljsjs instead, I didn’t even think of that…

borkdude16:05:08

repro:

#!/usr/bin/env bash

":";momentjs='cljsjs/moment {:mvn/version "2.22.0-0"}'
":";src=$(clojure -Spath -Sdeps "{:deps {$momentjs}}")

"echo" "$src"

"exec" "lumo" "-c" "$src" "$0" "[email protected]"

(ns balcony.core
  (:require
   [moment]))

(println (moment))

borkdude16:05:19

doesn’t work btw

borkdude16:05:10

I think the approach of bundling up to a single file is already cool, just seeing if I can get this approach to work

richiardiandrea16:05:33

Yep I think it is nice to explore for sure

richiardiandrea16:05:08

:foreign-libs are declared as compiler options so I was definitely wrong there

borkdude16:05:03

can I read files from the classpath from the REPL in lumo? if so, I think it’s a small step to loading the file?

borkdude16:05:20

there is no classpath of course

borkdude16:05:25

but you know what I mean

futuro16:05:01

Read in what way?

futuro16:05:49

Ah, and to just give it a filename of something that exists on the classpath. Hmm...

borkdude16:05:10

yeah and then eval it in node

futuro16:05:12

That's a good question; I've never tried, but presumably there's a way to find files inside the classpath...?

richiardiandrea16:05:46

Yes you can I think, the facilities are all there because this is what -D does

futuro16:05:00

Ah, then eval it. I'm missing something I'm sure, but wouldn't require fit the bill there?

borkdude16:05:25

yeah, definitely

richiardiandrea16:05:26

The only thing I don't know is if require can read from jars at the moment in the REPL

borkdude16:05:31

maybe read back a little :slightly_smiling_face:

futuro16:05:02

Ha, my bad.

futuro16:05:05

:slightly_smiling_face:

richiardiandrea16:05:28

But you need to declare them somehow

richiardiandrea16:05:55

There should be a deps.cljs in the jar

anmonteiro16:05:14

@borkdude node/require only looks in node_modules

anmonteiro16:05:28

if you have the moment cljsjs thingy, you have to require it by its name

anmonteiro16:05:34

(require 'cljsjs.moment) or something

anmonteiro16:05:42

then use js/Moment or whatever global it exposes

richiardiandrea16:05:22

Cool Antonio saves me from rabbit holing ;)

anmonteiro16:05:25

let me reiterate that none of this is specific to Lumo at all

anmonteiro16:05:30

this is how it works in JVM CLJS too..

borkdude16:05:34

Can I load js from an npm webjar from the REPL though?

richiardiandrea17:05:08

I think it needs to have the deps.cljs file?

borkdude20:05:19

ah:

cljs.user=> (require ’[lumo.classpath :as cp])
nil
cljs.user=> (cp/classpath-jarfiles)
(“/Users/Borkdude/.m2/repository/org/clojure/clojure/1.9.0/clojure-1.9.0.jar”
 “/Users/Borkdude/.m2/repository/org/clojure/tools.cli/0.3.7/tools.cli-0.3.7.jar”
 “/Users/Borkdude/.m2/repository/org/webjars/npm/moment/2.22.1/moment-2.22.1.jar”
 “/Users/Borkdude/.m2/repository/org/webjars/npm/nodemailer/4.6.5/nodemailer-4.6.5.jar”
 “/Users/Borkdude/.m2/repository/org/clojure/spec.alpha/0.1.143/spec.alpha-0.1.143.jar”
 “/Users/Borkdude/.m2/repository/org/clojure/core.specs.alpha/0.1.24/core.specs.alpha-0.1.24.jar”)

borkdude20:05:20

(cp/filenames-in-jar “/Users/Borkdude/.m2/repository/org/webjars/npm/nodemailer/4.6.5/nodemailer-4.6.5.jar”)
...

borkdude20:05:38

it’s a start… if the jar would have the minified version, it would probably work:

(.runInThisContext vm (.-source (js/$$LUMO_GLOBALS.readSource “META-INF/resources/webjars/nodemailer/4.6.5/lib/nodemailer.js”)))

borkdude20:05:21

I’m beginning to see why people just install yarn on their servers, because this is quite involved. In theory, we could make module loading work with npm webjars as well

richiardiandrea20:05:43

Yes I think we actually could, maybe open an issue @borkdude?

borkdude20:05:36

Maybe first discuss if this is a good idea. The benefit would be avoiding npm and still being able to write a script with deps, like above, without polluting the script dir with node_modules, package.json etc

richiardiandrea20:05:44

Personally I am specifically trying to port all the maven deps I use to npm

borkdude20:05:50

and there is one caveat. if you do something like

(.runInThisContext vm (.-source (js/$$LUMO_GLOBALS.readSource "META-INF/resources/webjars/nodemailer/4.6.5/lib/nodemailer.js")))
that file will do require(./mailer), I don’t know if we could trick node into reading the right file from the jar there

richiardiandrea20:05:01

I found that my company is more js oriented so we are trying not to use JVM stuff

borkdude20:05:04

for popular things we could always write a cljsjs, but then the repo already should have a minified version: e.g. https://github.com/moment/moment/tree/develop/min

borkdude20:05:48

and if it already has a minified version, it’s very easy to load it in node via webjars

richiardiandrea20:05:58

Yeah, that has always been the deal breaker for me, why do I need another package

richiardiandrea20:05:21

Yeah cljs could be greatly simplified if its only target is node

borkdude20:05:39

I think cljsjs was mainly built with browser in mind

borkdude20:05:34

maybe it’s not so hard to automate though, the minification step, so cljsjs could also be purposed for node

richiardiandrea21:05:04

Given also that lumo does not know maven, I am not so convinced it is a good idea tbh ... You never know of course ;)

anmonteiro21:05:29

CLJSJS should include minified files

anmonteiro21:05:44

or rather, I know for a fact they do

richiardiandrea21:05:36

Oh that's good to know, I actually thought they could be there already

borkdude21:05:54

@anmonteiro yes, but the examples I’ve looked at so far just get the minified file from e.g. http://registry.npmjs.org/moment/-/moment-2.22.0.tgz but for example http://registry.npmjs.org/nodemailer/-/nodemailer-4.6.5.tgz has no minified file

borkdude21:05:24

they just sift/move the files

anmonteiro21:05:15

Node.js packages are not minified

borkdude21:05:17

(sift :move {#“package/firebase-([a-z\-]*).js” “cljsjs/development/firebase-$1.inc.js”
                #“package/firebase=([a-z\-]*).js.map” “cljsjs/development/firebase-$1.inc.js.map”
                #“package/externs/” “cljsjs/common/“}
         :include #{#“^cljsjs”
                    #“^deps.cljs”})

borkdude21:05:31

I think this firebase thing is used from node as well

richiardiandrea21:05:41

The community effort is smaller if the community is small. I guess.

borkdude21:05:14

anyway, I see why people say things like: yarn install on the server. because another way is quite complicated

richiardiandrea21:05:21

Yeah, I am quite happy with it. JS tooling is smooth imho

borkdude21:05:26

what’s the benefit of yarn again though? it keeps a repo from which it copies deps, instead of downloading them all the time?

borkdude21:05:53

I don’t know enough about JS tooling. I hear mixed stories about it. Some people do everything to avoid it.

borkdude21:05:17

why doesn’t yarn make symbolic links to the cache dir though?

borkdude21:05:09

comes with it’s own set of problems

richiardiandrea21:05:57

They say yarn has reproducible dep management

richiardiandrea21:05:16

Dunno if it's true still though

richiardiandrea21:05:22

Npm usually catches up quickly

borkdude21:05:41

because of the lock file right?

richiardiandrea21:05:21

I think not because of that, probably the algo is different or something, not sure though

borkdude22:05:43

btw, the npm webjar idea might still be feasible with a tool like this: https://unix.stackexchange.com/questions/168807/mount-zip-file-as-a-read-only-filesystem mount the jar/zip into node_modules…