Fork me on GitHub
#clojurescript
<
2018-07-21
>
j1mr10rd4n01:07:44

has anyone successfully applied the “gradual migration to node_modules” path implied at the end of https://clojurescript.org/news/2017-07-30-global-exports? my app uses a library that depends on cljsjs.react and i’d like to provide that dependency via node_modules instead.. my compiler options include

:npm-deps            {:react "16.4.1"}
:install-deps         true
and i’ve added a project dependency on a react-npm-shim artifact I created that contains the following in deps.cljs
{:foreign-libs
 [{:file "react/cjs/react.development.js",
   :file-min "react/cjs/react.production.min.js",
   :provides ["cljsjs.react"],
   :global-exports '{cljsjs.react React}}]
 :npm-deps {:react "16.4.1"} 
}
but cljsbuild is failing to compile the .cljc file that requires [cljsjs.react] with java.lang.IllegalArgumentException: contains? not supported on type: clojure.lang.Cons (using clojurescript “1.10.339")

j1mr10rd4n04:07:49

further info - the exception is being thrown from the dep-has-global-exports? function in the analyzer https://github.com/clojure/clojurescript/blob/b1ade48e21f9e7f78d9db74559ce4dd5846d0c94/src/main/clojure/cljs/analyzer.cljc#L782

Josh Horwitz03:07:23

I’m building a website for a non-profit I am a part of, and I was hoping of writing it in ClojureScript. The only requirement is that it have a CMS or some other way for someone who is not very technical to update the website. Any ideas on what to use ?

genRaiy12:07:25

dumb nodejs question - how do I set an process env var from CLJS? I know the nodejs process API and have tried various options and can’t find the correct incantation. Any help would be appreciated - thanks 🙂

thheller12:07:26

@raymcdermott (goog.object/set js/process.env "FOO" "BAR")

genRaiy12:07:39

thanks @thheller - excellent. Any tips on how I can I discover these things independently?

thheller12:07:13

not really. basic JS interop I guess

thheller12:07:24

its odd to set env vars at runtime though since .. you know .. they are supposed to come from the env 😉

4
j1mr10rd4n13:07:51

it looks like there’s also a cljs compiler option you can set at build time :process-shim https://clojurescript.org/reference/compiler-options#process-shim

pesterhazy13:07:00

is that even possible? I mean what does it mean to change the env variables for a running process?

pesterhazy13:07:32

@j1mr10rd4n that's for something different

j1mr10rd4n13:07:50

ah ok thanks! i’m still getting my head around all the moving parts

pesterhazy13:07:16

certainly in Bash, when you "change" an env var (export MYVAR=value) you don't update the existing var, as visible by ps -p <PID> -wwwwE

pesterhazy13:07:03

you only make that var available locally (as a Bash var, not as an env var) and for subprocesses as an env var

pesterhazy13:07:27

although I can't find a reference, I think that unix env vars are immutable

mfikes14:07:47

Yeah, you can

man putenv
A use case is when you know something else in your process is going to read that environment variable and you need to set it in order to affect that other thing.

mfikes14:07:42

Here is an example: Planck controlling JavaScriptCore via an environment variable: https://github.com/planck-repl/planck/blob/master/planck-c/main.c#L307

pesterhazy15:07:31

Thanks for the pointer

pesterhazy15:07:40

You learn things about Unix even after years

j1mr10rd4n13:07:47

i’ve got a quick question that i’m looking for some help with - it’s about how to do this: “For example, a user could create a cljsjs.react artifact with a declared :npm-deps on React” mentioned in https://clojurescript.org/news/2017-07-30-global-exports i’ve created an artifact with a deps_cljs file that defines global exports but how should that artifact declare its npm dependencies? i took at a guess at putting :npm-deps in the deps_cljs file but that seems to be causing a compiler error

j1mr10rd4n13:07:44

i’m not sure if i should expect it to work or if i’ve found a bug

pesterhazy13:07:57

what do you want to do exactly?

j1mr10rd4n13:07:49

my app uses a library that depends on cljsjs-react, i’d like to satisfy that dependency by providing react as a node_module and shim the cljsjs-react namespace - the penultimate paragraph on https://clojurescript.org/news/2017-07-30-global-exports seems to indicate this is a supported strategy

pesterhazy13:07:18

what I do is create empty files declaring those namespaces: https://github.com/pesterhazy/double-bundle

pesterhazy13:07:02

then just set window.React to your preferred React version: https://github.com/pesterhazy/double-bundle/blob/master/library.js#L1

pesterhazy13:07:37

note that the cljsjs.react namespace (like other cljsjs nss) doesn't really provide anything; libraries like React typically grab the library from a well-known location, i.e. window.React

pesterhazy13:07:23

I think the same should be possible using the global exports feature

pesterhazy13:07:23

instructing the cljs compiler to download the library from NPM (using :npm-deps) is orthogonal to this AFAIK

pesterhazy13:07:07

(note that :npm-deps still has some rough edges, which is why I use the "double bundle" approach, which is foolproof)

j1mr10rd4n13:07:25

yeah i feel like i might have found an edge…

thheller13:07:44

@j1mr10rd4n @pesterhazy the :global-exports are only relevant when using (:require ["react" :as r]). they are not related to cljsjs.react.

j1mr10rd4n13:07:28

your double-bundle appears to be what is referenced in the 5th paragraph on that page “For many projects this meant using tools like Webpack to package up all such dependencies into a single foreign library. While expedient, this approach leads to an unidiomatic style, and furthermore creates an obstacle should users try to migrate these dependencies to direct consumption from node_modules down the road.” https://clojurescript.org/news/2017-07-30-global-exports

thheller13:07:04

@j1mr10rd4n if you just want to use npm and not worry about all this use #shadow-cljs

j1mr10rd4n13:07:47

that had occurred to me but that paragraph in the news article was tantalising

pesterhazy13:07:52

@j1mr10rd4n hehe right, I don't think that's entirely relevant anymore, given that the double bundle approach is documented in an official guide now: https://clojurescript.org/guides/webpack

pesterhazy13:07:52

(It really doesn't lead to an unidiomatic style for deps like React, so I wouldn't worry about that part.)

pesterhazy13:07:50

Double bundle or shadow-cljs are both good approaches for pragmatic cljs teams that use a ton of NPM dependencies (which at this point will be most non-trivial projects).

j1mr10rd4n13:07:15

excellent advice, thanks for the pointers!

aaron5121:07:35

In a Clojurescript React Native app, we are persisting serialized data on users' phones with pr-str. What's the best way to handle migrations on the data? Do we need to hand-roll a migration solution (and keep track of versions, etc)?