Fork me on GitHub
#clojurescript
<
2020-04-16
>
Dominic Pearson12:04:03

hi! i am trying in vain to use a npm module with a new cljs project. this is my first time using the cljs ecosystem, and am working to port my library to the runtime. could someone help me with some basics?

Dominic Pearson12:04:37

https://gitlab.com/dspearson/exp here's the skeleton project as it stands currently

Dominic Pearson12:04:19

https://gitlab.com/dspearson/exp/-/blob/master/project.clj - i added lein-npm, and added :npm {:dependencies [[bytebuffer "5.0.1"]]}

Dominic Pearson12:04:39

this is the package that i'd like to be using: https://www.npmjs.com/package/bytebuffer

Dominic Pearson12:04:55

(my clojure lib uses java bytebuffers heavily, so was looking for a nice compat layer)

Dominic Pearson12:04:44

the issue i see is, when doing lein fig:build:

[Figwheel] Validating figwheel-main.edn
[Figwheel] figwheel-main.edn is valid \(ツ)/
[Figwheel] Compiling build dev to "resources/public/cljs-out/dev-main.js"
internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module '@cljs-oss/module-deps'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at [eval]:8:13
    at Script.runInThisContext (vm.js:122:20)
    at Object.runInThisContext (vm.js:329:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at evalScript (internal/bootstrap/node.js:590:27)

Dominic Pearson12:04:00

i must be missing something, or doing something horribly wrong.

Dominic Pearson12:04:44

just for completion: works fine for node.

> var ByteBuffer = require("bytebuffer");
undefined
> var bb = new ByteBuffer();
undefined
> bb
ByteBuffer {
  buffer: <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>,
  offset: 0,
  markedOffset: -1,
  limit: 16,
  littleEndian: false,
  noAssert: false }

p-himik14:04:11

Just my 5c - if you plan to use NPM modules often, you may find shadow-cljs much better than other tools.

Dominic Pearson14:04:42

thankfully I just discovered cljsjs, and it seems packaged. means i don't learn what i did wrong, but i can now access the functions as expected, so the outcome is fine.

p-himik14:04:00

cljsjs is a ticking bomb though in terms of maintenance of your own project. A moment will come when you stumble upon a poorly maintained library or just a non-existing one.

p-himik14:04:42

It may very well be that you did nothing wrong. Maybe it's just that Figwheel and the default CLJS compiler don't support that particular module for whatever reason.

Dominic Pearson14:04:59

fair enough. the cljs ecosystem has even less documentation than clojure does, which is expected for something with fewer users i suppose. i guess it's also a fast-moving target? at least for my use case, i just want to implement a compatibility shim around JVM ByteBuffers so that I can use the same implementation for working on them in clj and cljs, without rewriting too much. originally was going to implement the stuff myself, but then found a npm module that did 90% already.

p-himik14:04:08

What documentation do you find missing?

p-himik14:04:32

It's not that fast moving, no. Much more stable than regular JS. :)

Dominic Pearson14:04:00

mostly best-practice beginner stuff. this is day 0 for me getting started with cljs. there does not appear to be any obvious way for me to start a skeleton project (i experimented with several before getting to a repl, and still was unsure whether i was doing the correct thing.) i admit i am not a clojure expert, and some of the java interop stuff is still a little over my head, but it took me some time to realise that i should probably not be doing stuff here https://clojurescript.org/news/2017-07-12-clojurescript-is-not-an-island-integrating-node-modules and stumbling across cljsjs, which you suggest i also probably avoid. took me some time to realise that cljsjs imports do not follow the regular require :as syntax (or i did it incorrectly)

p-himik14:04:30

In what languages do you have experience?

Dominic Pearson14:04:35

lots of non-obvious stumbling blocks that i only chip away at when reading blog posts or thorugh experimentation. maybe i'm missing something, but it's an adventure to bootstrap at least. not really criticism, just hard nonobvious work. i'm using a lot of stuff that when getting started is not really explained. https://clojurescript.org/guides/project-templates is a perfect example - would be an improvement if there were snippets about why one would choose any of those options. today much is "left as an exercise for the reader".

Dominic Pearson14:04:54

c, scheme, perl, common lisp, java, erlang

Dominic Pearson14:04:00

0 previous js experience

Dominic Pearson14:04:21

i suppose a lot of people trying clojurescript for the first time come from a javascript background? i dunno. but for me, this is my first excursion into that ecosystem, coming instead from the jvm, just as a way to make my lib available to both runtimes

Dominic Pearson14:04:55

so my learning path will be, get cljs bootstrapped, import required javascript stuff, then make a clj+cljs project, experiment with reader conditionals, then hopefully take all that knowledge and apply it to my actual project and hope for the best

p-himik14:04:25

Regarding those templates - IMO it wouldn't make sense to add a deeper overview of the options simply because the links can be followed where a guaranteed up-to-date descriptions could be found. If you copy the descriptions over, they will inevitably get stale an confuse people. Regarding what to choose - it doesn't really matter. Templates just help developers to skip some steps. The thing about novices is that there are no steps that can be skipped - a novice either don't need those steps at all or has to go through them manually to understand what's going on. With that being said, you probably should start with no template at all. Or maybe the simplest template you can find. Or maybe, if you prefer adapting some already working code to your needs, just find one of many CLJS App showcases out there. "TODO App" is a good one, especially if you want to tackle UI. Regarding best practices - a common way of doing things in many languages is using all encompassing frameworks and adopting some paradigm. A common way of doing things in CLJ(S) world is to pick your own libraries to mix and match to the desired extent. Of course, there are big libraries, paradigms, and even frameworks out there, but far less invasive than in other ecosystems. And that's roughly why it's hard to find best practices - because people just find what's best for them. A sane default to get you started is probably to stick to the most popular libraries within any particular niche. Even if you commit to some set of libraries, it's usually not that hard to switch to a different one. Regarding getting started in general - a very trivial advice, but I cannot stress this enough: read the documentation. Do not skip right to the examples. You may skip stuff that you won't need at all (React Native, for example), but don't skip the main stuff. Choose a build tool, find its documentation, and read through it while trying things out. Yet again, my recommendation is shadow-cljs, in this case because its documentation is stellar. If you do that, you will be better off than 90% of beginners.

Dominic Pearson15:04:02

well, i have read the documentation, my point is that much is not documented in a way that is either authoritative or discoverable. i am not a novice, have experience professionally with clojure, and am explicitly only looking at cljs to port a clj-only open lib i wrote to support both runtimes. i'll take notes on the learning process and publish something highlighting possible areas for improvement once the work is complete. i solved the issue myself regardless, so this thread serves little further purpose (though the original issue that i asked for help with persists, i found a workaround). i'll check out shadow-cljs at a later point, since i'm now already at the stage where i can continue writing actual project-relevant code. thanks for the input .

Oliver George12:04:35

I'm playing around with the new https://github.com/clojure/clojurescript-site/blob/april-release-target-bundle/content/guides/webpack.adoc. I'd like my reagent component to be displayed in a React Storybook. Things are mostly working but I can't workout how to generate the export default ... to expose my component to the story.

fricze13:04:52

@dnolen I’ve started using Krell and it’s quite brilliant. everything seems to work. one thing I wonder about is your development style and connecting to REPL. do you connect with Krell REPL from Emacs/Cider of some other IDE/editor? or just keep it running in terminal?

dnolen13:04:59

@andrzej.fricze there's some bugs around Android which I'm fixing today so I wouldn't go crazy w/ it quite yet - the REPL is just a standard Clojure-style REPL

👍 8
dnolen13:04:10

it can work anywhere as long as the tooling supports that style of REPL

dnolen13:04:28

there's no plans to build any kind of nREPL support - that's up to somebody else to figure out and not in Krell itself - though I'm not against providing hooks if it's reasonable

👍 4
dnolen13:04:01

to me tools that don't support plain REPLs and only nREPL are a huge problem

👍 12
fricze14:04:40

I see, thanks for explanation :thumbsup:

dvingo13:04:48

What's an idiomatic way to access a global JS variable that may not be defined? For example, determining if "window" is available. If you just use js/window you'll get a ReferenceError on node.js Right now I'm using:

(= (js* "typeof window") "undefined")
I was also considering wrapping the js/window form in a try/catch, but went with the above approach instead. I'm curious if there are other solutions.

Derek Passen14:04:25

I think this is what exists? is for

dpsutton14:04:26

Big refactor and extension of inf-clojure happening. will make working with the non-nrepl tooling much nicer from emacs. if you couple with clojure-lsp you largely get a cursive-like

👀 8
Nassin17:04:10

inf-clojure already works with a socket repl for me, is this something different?

dpsutton17:04:51

making sure it doesn't throw weird errors on completions for planck, the startup was weird. and the code base was pretty scrambled. you can follow here: https://github.com/dpsutton/inf-clojure/tree/dispatch https://github.com/dpsutton/inf-clojure/tree/dispatch

Nassin18:04:52

nice, yeah its needs some love

Nassin18:04:05

I recently switched to cider because pretty printing to a popup buffer is such a convenient feature.

Nassin18:04:27

I had a workaround in inf-clojure but it wasn't as nice

dpsutton18:04:03

that's a good idea. should be implementable in inf-clojure

dvingo14:04:58

@dpassen1 thanks! that works

👍 4
dnolen20:04:05

Heya, I've made a new tool for React Native - my company Vouch supported this open source development effort - we used re-natal for a couple of years and while useful - in the end it stuck out in our workflow - not very Clojure-y

dnolen20:04:02

it uses the :bundle target from ClojureScript master - the goal here was to be ultra-minimal - if you're familiar with the ClojureScript Quick Start, you can make a React Native app

dnolen20:04:46

we don't try to paper over the React Native CLI at all - it just the stuff that Clojure(Script) programmer should care about - a REPL, and a hot loader

✔️ 20
dnolen20:04:45

it makes no assumptions about what ClojureScript library you may want to use - but I made a beginning to end tutorial for Reagent https://github.com/vouch-opensource/krell/wiki/Reagent-Tutorial

👍 28
dnolen20:04:01

should be trivially adaptable to any other ClojureScript React binding

dnolen20:04:20

we're still collecting feedback and bug reports - so no artifact yet - after a couple of weeks of kicking the tires we'll push something to Clojars and you won't have to use the git dep

aisamu21:04:08

Out of curiosity: is there a story behind the name? (since I'll be reading that often, calling krell.main...)

dnolen21:04:49

also if you're into modular synthesis, Todd Barton has a famous generative patch called the "Krell Patch", this comes from how the original soundtrack was created - https://en.wikipedia.org/wiki/Bebe_and_Louis_Barron

👌 20
eudis22:04:54

is there a way to have shadow-cljs release app produce a app.map source map, for production builds?