Fork me on GitHub
#clojurescript
<
2017-11-26
>
souenzzo01:11:59

Is possible require a js function on cljs? I know and I'm using require from npm deps, but when I try to require a "my project js file", compiler warns about "no goog.provide"....

roti10:11:51

@souenzzo probably the best way to find out is to have a look at the foreign libs feature (https://clojurescript.org/guides/javascript-modules), or at some libraries that make use of it (http://cljsjs.github.io/)

souenzzo10:11:40

Thanks. I'm looking for js modules.

roti10:11:47

@souenzzo or you could pack your js function in a google closure namespace (in which case you need to do the "goog.provide")

joshkh13:11:07

is there a way in clojurescript to iterate over the properties of a javascript object? something equivalent to: var obj = {prop1: 1, prop2: 2}; for (prop in obj){ if (obj.hasOwnProperty(prop)) {console.log(prop);}}

joshkh13:11:52

ah, i should have thought to look at js->clj which uses the js-keys function. neat.

joshkh13:11:47

thanks, thheller. plenty of useful functions in there as well.

ajs15:11:26

is using js-invoke the recommended way to call a js method on a js object that works in advanced opts?

noisesmith15:11:03

it's for when the name of the method is not syntactically valid

ajs15:11:27

well i tried for example (.someMethod obj arg1 arg2) and the .someMethod is munged. I switched to js-invoke and it works fine

noisesmith15:11:40

to make sure something works with advanced compilation, you use externs (or one of the new fangled things I don't understand yet)

ajs15:11:30

in my case, the .someMethod is a brower API so its not a library i'm bringing in. yet Closure still munges it, so using js-invoke and the method name as a string literal solved it

noisesmith15:11:08

externs prevent munging, even with browser apis

ajs15:11:28

well, is there a downside to using js-invoke rather than writing an externs for a browser api?

noisesmith15:11:23

it does a bunch of data transformations you wouldn't otherwise need

noisesmith15:11:45

combined with the fact that externs are simple to write, and the syntax is much uglier, I would opt for defining an extern

ajs15:11:50

looks like google provides externs files for browser apis, so i'll go with that

ajs15:11:02

in a pinch good to know that js-invoke can work before digging up externs files though

noisesmith15:11:46

you don't have to dig them up, they are very easy to write

noisesmith15:11:55

but it is cool that google provides some

ajs15:11:22

well, it's not always obvious what the js class is for a browser api. i can call methods on svg paths for example without knowing the exact JS class needed to write the extern. so digging up that info does take a bit of time

ajs15:11:04

for example i'd have to know its SVGPathElement.prototype.getPointAtLength = function(opt_distance){}; which is not obvious at first

p-himik15:11:11

@ajs You can use https://github.com/binaryage/cljs-oops - it doesn't require you to write externs and to know JS classes.

p-himik16:11:16

@joshkh You should ask the author to get a definitive answer, but I'd say it's because these objects are already perfectly accessible from within CLJS - there's no need for interop.

joshkh16:11:09

yes good point, someone explained the other day that core javascript props/functions don't require externs because they're ignored in the closure compiler. still seems odd to specifically rule it out though.

joshkh16:11:15

thanks, p-himik!

pandeiro17:11:53

what's the CLJS analog for JS libs whose usage requires an import SomeModule from 'some-package'?

pesterhazy18:11:30

@pandeiro the ES5 equivalent

pandeiro18:11:56

Thanks @pesterhazy -- I was running into issues related to loading modules in react native, so not actually require/import related

pesterhazy18:11:39

Yeah, don’t forget re-natal use-component

pandeiro18:11:13

I did enable-auto-require, which maybe uses that under the hood?

pesterhazy18:11:38

That’s new

tomaas23:11:57

the snippet's way works even with :advanced compilation, but I don't understand why. With no externs file referenced why does it work?