shadow-cljs

https://github.com/thheller/shadow-cljs | https://github.com/sponsors/thheller | https://www.patreon.com/thheller
zhuxun2 2021-04-27T03:29:58.004700Z

Where is the best place to set NODE_ENV? Do I just put a line of (set! js/NODE_ENV (if goog.DEBUG "development" "production") in my entry point? Is there a way to say this in the shadow-cljs.edn?

andre 2021-04-27T05:35:41.007700Z

hi, can we make this work somehow with shadow https://www.npmjs.com/package/babel-plugin-assign-directive ?

Aron 2021-04-27T05:36:42.008100Z

seems like there is a NOTE saying no šŸ™‚

andre 2021-04-27T05:37:19.008400Z

yes , thats why im here šŸ™‚

Aron 2021-04-27T05:37:55.008900Z

why the link is 404 from the package site?

Aron 2021-04-27T05:38:07.009400Z

can't even read the source

andre 2021-04-27T05:38:56.010400Z

idk its outdated, its possible to add directive with a macro , im more interesting in how we can use babel plugin for generated js code with shadow

andre 2021-04-27T05:39:53.011200Z

or probably it could be not a bubel plugin, i mean we need access to js code to be able to change it in runtime

Aron 2021-04-27T05:42:40.012500Z

I am not sure I fully understand. If I generate my js code with shadow-cljs, why would I take the output of that and try using another approach to modify it? Why not just modify at the beginning

Aron 2021-04-27T05:43:19.013100Z

I know lots of tooling on how to modify js code from js, but that is in js land entirely

andre 2021-04-27T05:44:17.013600Z

we need to get this user.i_use_worklet*=*function(){varĀ _f*=*functionĀ _f(){*return*Ā console.log("HeyĀ I'mĀ runningĀ onĀ theĀ UIĀ thread");};_f._closure*=*{console*:*console};_f.asString*=*"function(){const{console}=this._closure;{returnĀ console.log(\"HeyĀ I'mĀ runningĀ onĀ theĀ UIĀ thread\");}}";global.__reanimatedWorkletInit(_f);*return*Ā _f;}(); from this user.i_use_worklet*=*function(){"worklet";*return*Ā console.log("HeyĀ I'mĀ runningĀ onĀ theĀ UIĀ thread");};

andre 2021-04-27T06:22:43.014500Z

hm actually, probably we need this only in the release build

Aron 2021-04-27T06:53:04.016600Z

I know I would write something custom with this https://www.npmjs.com/package/falafelhttps://www.npmjs.com/package/falafel šŸ˜„

thheller 2021-04-27T06:57:13.019300Z

@andre I don't know how this works at all but maybe you want to write the worklet code directly in JS and just use it from CLJS? or do you really need to write those in CLJS?

thheller 2021-04-27T06:57:59.019400Z

its an env variable so you NEVER set it in your code

andre 2021-04-27T06:58:48.020400Z

so my idea is to run re-frame event handlers in a separate thread, so yeah it should be cljs code

andre 2021-04-27T07:00:38.021100Z

but for my idea i don't need it in develop, only in production for performance reasons

thheller 2021-04-27T07:00:57.021700Z

the main issue is that :advanced will likely remove the "worklet;" thing

andre 2021-04-27T07:01:12.022300Z

yeah, i also thought about this

thheller 2021-04-27T07:01:18.022500Z

it'll also agressively inline functions and so on so the code is never what you wrote after :advanced

andre 2021-04-27T07:01:45.023Z

yeah thats true

thheller 2021-04-27T07:01:57.023300Z

also I don't have a clue how worklets actually work but I'd assume it would require a lot of serialization

thheller 2021-04-27T07:02:11.023700Z

if it all runs in a separate thread and you can't share memory in JS

thheller 2021-04-27T07:02:37.024300Z

so running re-frame event handlers seems like an absolute no go for me? too much overhead serializing the app-db back and forth?

thheller 2021-04-27T07:05:12.024800Z

> When you pass arguments, they will be copied to the UI JS context.

andre 2021-04-27T07:05:21.025Z

seems like that šŸ˜ž

thheller 2021-04-27T07:08:23.026400Z

https://www.npmjs.com/package/babel-plugin-assign-directive the github repo for this doesn't exist anymore so I think this never actually worked? can't see how this would work with any other CLJS tool either as :advanced or even :simple would be the major hurdle here

thheller 2021-04-27T07:08:55.027300Z

so the only way I see this working is with actual JS functions just called from CLJS but not passing through CLJS tooling, just metro

andre 2021-04-27T07:13:49.027500Z

yeah, thanks

thheller 2021-04-27T07:23:59.028700Z

a custom worklet-fn special form could be created to create the code you need but yeah its applications are very limited and I don't expect this to ever work for functions using CLJS datastructures anywhere. basic JS types maybe work though.

borkdude 2021-04-27T10:08:51.029100Z

borkdude 2021-04-27T10:09:03.029400Z

how should those warnings be fixed for shadow?

borkdude 2021-04-27T10:11:40.029700Z

Note that the code does work, also under advanced

borkdude 2021-04-27T10:26:29.029900Z

thheller 2021-04-27T11:36:50.031500Z

@borkdude first of all this has nothing to do with shadow-cljs, you'd get those warning for regular CLJS as well if you were to enable them. so the only difference is that shadow-cljs defaults to having them enabled automatically vs. manually. You can either use (set! *warn-on-infer* false) somewhere above those forms if you know they are safe and can be renamed (which they likely can)

borkdude 2021-04-27T11:38:20.032500Z

@thheller Adding an extra local type hint solved the warnings. I don't understand why because that type hint was already on the return tag of the function. But thanks. It was reported by a shadow-cljs users, I have never seen this warning before.

thheller 2021-04-27T11:38:22.032700Z

unsure why the return type hint is missing though. that should be ok

borkdude 2021-04-27T11:39:02.033Z

I will check out warn-on-infer, it sounds useful

thheller 2021-04-27T11:43:28.033400Z

yeah if you set that to true you should get the warnings with the regular cljs compiler too

thheller 2021-04-27T11:43:52.033900Z

basically shadow-cljs just sets that to true by default for local files (ie. files not in .jar)

thheller 2021-04-27T11:45:52.034900Z

I do remember some weird issue with return type hints but not sure. I think in some places it was a little inconsistent and (defn ^Return foo [] ...) worked but (defn foo ^Return [] ...) didn't

thheller 2021-04-27T11:46:31.035400Z

or the other way. you really don't see them used all that often :P

iGEL 2021-04-27T14:58:09.040200Z

Hello. I have another question, probably quite stupid again, but I'm banging my head against this for a few hours. I'm trying to adapt this example: https://github.com/reactchartjs/react-chartjs-2/blob/master/example/src/charts/Line.js

(ns my-app.views
  (:require ["react-chartjs-2" :refer [Line]]))

(defn chart []
  [:> Line
      {:data {:labels ["1" "2" "3" "4" "5" "6"]
              :datasets [{:label "# of Votes"
                          :data [12 19 3 5 2 3]
                          :fill false
                          :backgroundColor "rgb(255, 99, 132)"
                          :borderColor "rgba(255, 99, 132, 0.2)"}]}
       :options {:scales {:yAxes [{:ticks {:beginAtZero true}}]}}}])
But I get this error:
error when calling lifecycle function my-app.core/mount-root Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of `my-app.views.chart`.
Line seems to be undefined and I don't know why. Both "chart.js": "^3.1.1" and "react-chartjs-2": "^3.0.2" are in my package.json. I'm quite sure it was working yesterday, but don't know what I've changed

Aron 2021-04-27T14:59:59.040500Z

So what is Line?

Aron 2021-04-27T15:00:43.041200Z

oh, i can't read, sorry, you said it's undefined

iGEL 2021-04-27T15:00:43.041300Z

It should be a react component, provided by react-chartjs-2

iGEL 2021-04-27T15:00:52.041500Z

hehe

Aron 2021-04-27T15:01:14.042300Z

can you check how that component exports? sometimes people do weird things

Aron 2021-04-27T15:01:39.042700Z

although if it was working and you didn't update anything then it's probably not that

iGEL 2021-04-27T15:14:09.045400Z

I've added nouislider-react to the project, but I just removed it and it's still not working. What should I look for in the exports? I'm not too familiar with JS modules and exports, but I found this in node_modules/react-chartjs-2/dist/index.js:

var Line = React.forwardRef(function (props, ref) {
  return React__default.createElement(ChartComponent, Object.assign({}, props, {
    type: 'line',
    ref: ref,
    options: props.options || {}
  }));
});

Aron 2021-04-27T15:20:15.045700Z

there should be either export or module exports

Aron 2021-04-27T15:20:35.046100Z

but I don't think that's the issue if it was working, more likely a typo or similar issue

iGEL 2021-04-27T15:29:14.047700Z

I guess it was a caching issue. I deleted everything and restored it from git and restarted shadow-cljs. Now it's working. Thank you anyway!

iGEL 2021-04-27T15:29:31.047900Z

I thought I tried that before, but this time I removed not just the node modules, but everything

Aron 2021-04-27T15:35:02.048200Z

glad it's working šŸ™‚