These are chat archives for opal/opal

23rd
Jun 2016
Sean Scally
@scally
Jun 23 2016 04:20
Is it possible to use module.exports in opal? I've tried backticking it like module.exports = MyRubyView but that throws an error in the compiled js: "import and export may only appear at the top level". Googling various opal/export/modules failed, so I thought I might ask here :)
Jamie Gaskins
@jgaskins
Jun 23 2016 14:36
@scally I don't think so. The entire file gets wrapped in a closure by the compiler.
Jared White
@jaredcwhite
Jun 23 2016 14:53
@elia Blog post is ready to roll for 0.10 final release opal/opal.github.io#37
Sean Scally
@scally
Jun 23 2016 17:22
@jgaskins thanks for the response; is there any alternative approach I could use if I wanted to write something in opal that I could easily import from JS without having to wrap everything in $'s?
Jamie Gaskins
@jgaskins
Jun 23 2016 17:30

@scally Ah, I think I see what you mean. You want to be able to do something like this?

class Foo
  def bar
  end
end

`module.exports = #{Foo}`

And then elsewhere in JS:

import Foo from './foo';

var f = Foo.new();
foo.bar();
Forrest Chang
@fkchang
Jun 23 2016 17:34
@scally help me understand the situation, so isn't module.exports common.js/node modules -- i.e. it doesn't make sense outside of node - I'm not sure how code like that gets compiled to work on the client via browerify/webpack, etc.. I guess u want to do something like what @jgaskins posted?
Jamie Gaskins
@jgaskins
Jun 23 2016 17:39

@fkchang I figured he wanted to convert the compiled Ruby to a JS-like object because of this:

wanted to write something in opal that I could easily import from JS without having to wrap everything in $'s

Sean Scally
@scally
Jun 23 2016 17:49
@jgaskins Yes, you got it in your example
Jamie Gaskins
@jgaskins
Jun 23 2016 17:56
@scally Currently, there's no way to convert a Ruby class into an object without $ method prefixes. AFAIK, the $ prefix exists purely so you can have methods and ivars with the same names (like attr_readers). That might be the biggest obstacle to getting something like this working.
Sean Scally
@scally
Jun 23 2016 18:02
OK, got it. Appreciate your response. :)
FWIW, my use case is React Native; I've been building a RN app with ES6 and added a gulp task to convert Coffeescript/CJSX to JS, using the backticks in Coffeescript to import/export as needed to emit JS that Coffeescript didn't support, and was hoping I could extend the same methodology to use RN with Opal.
Jamie Gaskins
@jgaskins
Jun 23 2016 18:03
No worries. I like to make stuff usable easily and I think it would be a lot of fun to be able to use something like that. Right now, it's easy to call out to JS from Ruby, but going the other direction still has a fair bit of friction.
Sean Scally
@scally
Jun 23 2016 18:05
I saw there's a Github sample project for RN/Opal but it seems outdated & I have some concerns that the methodology it used (skipping the npm ecosystem) made it harder to update as RN changed or interop with the rest of the npm ecosystem
I was looking for a way to approach RN/Opal in a way that still used the npm infrastructure instead of rake/bundler
Jamie Gaskins
@jgaskins
Jun 23 2016 18:05
The one @wied03 posted?
Sean Scally
@scally
Jun 23 2016 18:06
This is the one I meant: https://github.com/zetachang/opal-native
Looking over the https://github.com/wied03/react_native_sample one now... This might be closer to what I'm looking to do, thanks for pointing me in that direction. Looks like using a webpack server approach with opal-webpack?
Forrest Chang
@fkchang
Jun 23 2016 20:01
@jgaskins I'm thinking maybe a library that converts opal classes/objects into something that looks more native Javascript like could be in order, it would be limitd where one couldn't have a property and method w/the same name. That could be a good too for publicity, if we made a kick but opal lib that was easily usuable by JS. I think that was a selling point of coffeescript, easily usuable by JS because it's just syntactic sugar
Yehonathan Sharvit
@viebel
Jun 23 2016 20:21
@dixpac were you able to require a gem with opal, compile to javascript and run the javascript?
Mitch VanDuyn
@catmando
Jun 23 2016 20:53
@viebel - i have done that...