These are chat archives for opal/opal

5th
Feb 2018
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:10
Hi all, I’m trying to integrate opal successively into an existing project. But I’m struggling with a basic concept: How can I export a class in opal such that I can use it from a typescript file?
# foo.rb

class Foo
  def bar
    "barbar!"
  end
end
// otherfile.ts

import { Foo } from './foo'
console.log((new Foo).bar());
Elia Schito
@elia
Feb 05 2018 18:14
@fiedl all Opal classes are defined on Opal, which is a property of window, e.g. window.Opal.Array, that is the same as what MRI does because essentially every ruby constant is also a global
so the TS code would something like this: console.log(Opal.Foo.$new().$bar()); but I'm guessing, I don't know TS
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:17
Thanks @elia. Trying this I get TS2304: Cannot find name 'Opal‘.
Elia Schito
@elia
Feb 05 2018 18:18
maybe, assuming you have an opal.js somewhere you need to add import { Opal } from 'opal'
@fiedl is there a way to require js stuff that doesn't use modules?
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:23
I’m not sure, but I’ve seen something like this: import * as showdown from 'showdown‘; instead of const showdown = require(‚showdown‘).
Elia Schito
@elia
Feb 05 2018 18:24
then probably require('opal') will suffice
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:30
I think, it’s import * as Opal from 'opal‘;. This reduced the amount of errors significantly :)
Elia Schito
@elia
Feb 05 2018 18:31
:smile:
@fiedl are you using sprockets, webpacker or something else?
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:32
ERROR in ./node_modules/opal/lib/opal-node.js
Module not found: Error: Can't resolve 'fs' in 'node_modules/opal/lib'
webpack
well, more precisely, I’m using ionic, which uses webpack
Elia Schito
@elia
Feb 05 2018 18:33
ok, I'm not too familiar with any of those, how are you fetching the opal code? also which opal version?
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:33
unit testing with karma
opal 0.6.4
Elia Schito
@elia
Feb 05 2018 18:35
oh my, :)
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:36
I’m using the opalrb-loader from https://www.npmjs.com/package/opal-webpack
Ooops, I’ve just seen on https://github.com/opal/opal/releases that the current release is 0.10.5. But I just did npm install opal :)
Elia Schito
@elia
Feb 05 2018 18:37
latest version is 0.11, need to check releases
@fiedl maybe you can start experimenting installing the gem and compiling via the CLI
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:40
https://npms.io/search?q=opal has opal as first result with version 0.6.4.
Elia Schito
@elia
Feb 05 2018 18:41
opal --compile --arity-check foo.rb > foo.js should work
@fiedl yeah the npm package is outdated
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:41
:-)
Elia Schito
@elia
Feb 05 2018 18:42
I think there's someone using webpacker with more recent versions but I don't remember who it was…
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:43
To be honest, I’m not sure how to get the current version then.
Elia Schito
@elia
Feb 05 2018 18:44
gem instal opal
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:45
I guess, then I would have to tell webpack somehow which opal to use, right?
Elia Schito
@elia
Feb 05 2018 18:47
I think an easy (temporary) way could be to opal --compile --arity-check foo.rb > path/to/dir/watched/by/webpacker/foo.js
and then relauch the command everytime the sources change
Sebastian Fiedlschuster
@fiedl
Feb 05 2018 18:47
:/

I’ve found some pointers at the readme of https://www.npmjs.com/package/opal-webpack. I’ll report back when I’ve got something working …

thanks again!

Elia Schito
@elia
Feb 05 2018 18:48
yeah, unfortunately that's the best I can do, keep me posted!