These are chat archives for opal/opal

3rd
Apr 2018
G. Gibson
@mistergibson
Apr 03 2018 00:08
I got a weird one - after compiling my .js file got a 'unrecognized token "try"'
I'll file a report if it persists - checking for pilot error
eiko
@eiko
Apr 03 2018 01:03
@elia thank you, i'll check back here tomorrow!
Jamie Gaskins
@jgaskins
Apr 03 2018 11:45
@cerdiogenes If you want a raw JS object from a Ruby hash, call to_n on it (may need to require "native" if it doesn't work off the bat)
Opal can't represent Ruby hashes as POJOs because JS requires keys to be strings but Ruby hashes can have any object as a key.
Kadu Diógenes
@cerdiogenes
Apr 03 2018 11:48
@jgaskins after some time I discovered the to_n
thanks!
The split between ruby and javascript is starting to get clear in my mind :smile:
I'm almost happy with the following code:
require 'opal'
require 'native'
require 'json'

def entrypoint(event, context, callback)
  response = {
    statusCode: 200,
    body: event
  }.to_n
  callback.call(`null`, response)
end

`module.exports.opal = Opal.Object.$entrypoint`
it's works in the first request
but from the second and on opal loading code is executed again and it's breaks
@catmando said that opal-rails adds some checks to avoid this, but I didn't find where it does this
Jamie Gaskins
@jgaskins
Apr 03 2018 11:55
I wonder if it's caused by the difference between Webpack/Require.js-style import / require (storing JS modules in variables) and Ruby's require which relies on side effects (injecting Ruby classes/modules into a global namespace).
This is hacky (and probably error-prone), but try this for your first line:
require 'opal' if `typeof Opal === 'undefined'`
Kadu Diógenes
@cerdiogenes
Apr 03 2018 11:58
I already tried this
It only put this inside the if self.$require("opal");
The problem appear to be with the big function that creates Opal being executed twice
Elia Schito
@elia
Apr 03 2018 12:33
@cerdiogenes this answer seems relevant https://stackoverflow.com/a/43490794/601782
eiko
@eiko
Apr 03 2018 15:33
i'm still struggling with things like opal-jquery and opal-browser. it seems like i need to actually download the gem source so i can use opal to compile certain files?
Elia Schito
@elia
Apr 03 2018 15:41
@eiko not sure I understand, but it's surely better to have opal installed as a gem and use it to compile
Torsten Rüger
@rubydesign
Apr 03 2018 15:43
eval used to work for me (last year) and doesn’t anymore. Anybody know how to get it back?
Mitch VanDuyn
@catmando
Apr 03 2018 15:47
you have to require the compiler.... let me see if I can find that
Torsten Rüger
@rubydesign
Apr 03 2018 15:50
@catmando I think i found it: require 'opal-parser’
Mitch VanDuyn
@catmando
Apr 03 2018 15:52
require 'opal/compiler'
is what we use
Torsten Rüger
@rubydesign
Apr 03 2018 15:53
@catmando thanks
eiko
@eiko
Apr 03 2018 16:17

@elia well let me start at the beginning. following this tutorial (https://github.com/opal/opal-browser) i made a file called ./opal/browser.rb with the following contents:

require "opal"
require "browser"

alert "hello, opal."

i then compiled it using the following CLI command:

opal -I ./opal --gem paggio --compile opal/browser.rb > browser.js

i created a basic html file which included the script:

<script src="browser.js"></script>

but when i opened up the file in my browser, i got an error that the "alert" method is undefined.

Torsten Rüger
@rubydesign
Apr 03 2018 16:32
@catmando doesn’t seem to want to work for me. But ok, i have a workaround with instance_variable_set
eiko
@eiko
Apr 03 2018 16:34
okay, i got alert to fire by manually compiling both opal and opal-browser with Opal::Builder.build("opal") and Opal::Builder.build("opal-browser") but now i'm getting a warning that opal is being loaded twice, presumably because my script calls require "opal"
eiko
@eiko
Apr 03 2018 17:05

i cannot for the life of me get opal and opal-browser built any other way. even if i run:

opal -I . --gem paggio --gem opal --gem opal-browser --require opal --require opal-browser --compile opal/browser.rb > browser.js

i still get an error:

LoadError: can't find file "browser"

which can only be resolved by manually including opal.js

eiko
@eiko
Apr 03 2018 17:16
ultimately i got it to work by manually building opal and opal-browser, then using --no-opal to prevent opal from being required automatically. i'll be able to move forward with my project using this setup, but i'm still curious if anyone else needs to do it this way and why the documented way doesn't work