These are chat archives for opal/opal

19th
Mar 2015
Mitch VanDuyn
@catmando
Mar 19 2015 04:01
@elia re: opal-cannonbol, no errors, all tests pass. Its just seems like it is deliverying the code differently than say opal-jquery. opal-jquery comes down to the browser as a separate request, but opal-cannonbol seems to be bundled up inside the main app.
Mitch VanDuyn
@catmando
Mar 19 2015 04:22
@elia, I guess what I am not getting is what the difference is between what happens when you do a "rake release" on the cannonbol gem and the opal-cannonbol gem. Both directories look almost exactly the same, the only difference I can spot is the extra :dist rake task... does the release task somehow hook into this, to build the js file? How does this work (he said scratching his head, until he was bald :-)
Elia Schito
@elia
Mar 19 2015 08:50
@catmando rake release should be the vanilla gem building task from bundler (as in bundle gem YOURNAME)
@catmando can you explain how do you see they're different? (…browser requests, gem open, something else) seems something easily fixable anyway
Elia Schito
@elia
Mar 19 2015 09:49
@adambeynon I think I'm on the right way with requires and stuff
Adam Beynon
@adambeynon
Mar 19 2015 09:50
@elia nice! just checking out the commits now
Elia Schito
@elia
Mar 19 2015 09:51
right know I'm looking into opal-haml
Elia Schito
@elia
Mar 19 2015 11:29
@adambeynon I think it's done, just need to push the overridden javascript_include_tag to opal-rails
  def javascript_include_tag(*sources)    sources_copy = sources.dup.tap(&:extract_options!)    sprockets = Rails.application.assets    script_tags = super    sources_copy.map do |source|
      loading_code = Opal::Processor.load_asset_code(sprockets, source)
      script_tags << javascript_tag(loading_code)
    end
    script_tags
  end
not sure why gitter messes up newlines :(
Mitch VanDuyn
@catmando
Mar 19 2015 11:49
@elia, Its NOT messing up! I just don't understand how it works. I see no dfference between a gem that runs the code on the server and a gem that runs the code in opal! The only differences I see is that opal is required in the gemspec (makes sense) and the rake file has this extra "dist" task.
So how can the gem release process "know" to do the right thing with the code??
Elia Schito
@elia
Mar 19 2015 11:50
@catmando I'll show you the trick in a moment…
Mitch VanDuyn
@catmando
Mar 19 2015 11:53
yeah i saw that, but i don't get it... let me look again...
so basically let me get this:
1) the gem puts all the files into the the gem package
Mitch VanDuyn
@catmando
Mar 19 2015 11:58
nah I don't get it at all.
In one case the same files (in my case) are treated as normal files and run on the server, in the other case they become assets and served up...
Adam Beynon
@adambeynon
Mar 19 2015 12:01
@elia awesome! :metal:
Mitch VanDuyn
@catmando
Mar 19 2015 12:01
WAIT... so let me guess... there is NO difference, except that someplace some opal code does a "require opal-cannonbol", and that AUTOMAGICALLY grabs the right files, and compiles them
Adam Beynon
@adambeynon
Mar 19 2015 12:02
so for a static build, or non rails environment, we just need a manual Opal.load(‘application’) which does the bootstrapping?
Elia Schito
@elia
Mar 19 2015 12:03
@adambeynon yes, plus the mark_as_required when requiring unwrapped js files
(but corelib/runtime.js is already marked required)
@adambeynon also the Builder still works as before
@catmando the code I linked requires different files basing on the fact that it's inside opal or inside MRI
@catmando also Opal considers if RUBY_ENGINE == 'opal' in a special way, discarding the else part entirely
@adambeynon should we try rspec on it and merge? I think stubs still need to be forwarded to sprockets…
lunchtime! :spaghetti:
Mitch VanDuyn
@catmando
Mar 19 2015 12:08
@elia, yes understood... that is the same code in my gem (stolen from this file in fact.) Understand that it includes different things depending... Just did not quite believe the magic that lets opal get the files compiled etc. just because they are there... I guess I would like to understand better!
@elia... COFFEE TIME... sun just rose here :-)
Elia Schito
@elia
Mar 19 2015 12:47
@catmando whoa, what time exactly?
@catmando sorry if I restate stuff you know already, maybe the missing piece is that opal parses all require statements and precompiles all required files statically so they're ready for when they'll actually be required
Mitch VanDuyn
@catmando
Mar 19 2015 16:03
@elia, no problem. Here is what I am not understanding:
Mitch VanDuyn
@catmando
Mar 19 2015 16:10
I am not seeing how the following lines work
This message was deleted

if RUBY_ENGINE == 'opal'
require 'opal/cannonbol/cannonbol'
else
require 'opal'
require 'opal/cannonbol/version'

Opal.appendpath File.expandpath('../..', __FILE).untaint
end

Elia Schito
@elia
Mar 19 2015 16:12
if the ruby engine is opal it requires the code that should run in the browser
otherwise it will add the gem lib folder to the paths the opal compiler will look into (this happens in MRI/CRuby, not in the browser)
@catmando does it help?
Mitch VanDuyn
@catmando
Mar 19 2015 16:14
right, so when I do a require 'opal-cannonbol' in a .js.rb file in my assets folder, that is running with RUBY_ENGINE=='opal' correct?
Elia Schito
@elia
Mar 19 2015 16:14
yep
Mitch VanDuyn
@catmando
Mar 19 2015 16:14
So here is what my other "non" opal file looks like:
well it just has the ruby code in it, no require or anything
so lets say instead of the above code, i just had:
puts 'hello world'
i.e. my whole gem just consists of 1 line:
puts 'hello world'
sorry, I am not being clear... I will have to make a simple test case
but what I am not getting now is why if I just do a require 'some-gem' inside of foo.js.rb
it doesn't seem to work
Elia Schito
@elia
Mar 19 2015 16:18
you need to require the gem in MRI first, to trigger the append_path part
Mitch VanDuyn
@catmando
Mar 19 2015 16:19
let me try hang on
So is it possible to create a gem that will run both on the server and in opal?
Elia Schito
@elia
Mar 19 2015 16:22
sure
e.g. that's what volt does to some extent
Mitch VanDuyn
@catmando
Mar 19 2015 16:23
right
so if I do this:
if RUBY_ENGINE == 'opal'
require 'opal/cannonbol/cannonbol'
else
require 'opal'
require 'opal/cannonbol/version'
require 'opal/cannonbol/cannonbol' # ADD THIS LINE
Opal.appendpath File.expandpath('../..', __FILE).untaint
end
will that work?
Elia Schito
@elia
Mar 19 2015 16:28
yes
:)
Mitch VanDuyn
@catmando
Mar 19 2015 23:09
@elia, thanks for all the help... In the end it was sooo easy.
```ruby
```rubyrequire_relative 'cannonbol/cannonbol'
require_relative 'cannonbol/version'
unless RUBY_ENGINE == 'opal'
begin
require 'opal'
Opal.appendpath File.expandpath('..', __FILE).untaint
rescue LoadError
end
end
```ruby
Elia Schito
@elia
Mar 19 2015 23:12
@catmando simple questions can be very hard to answer at times :)
Mitch VanDuyn
@catmando
Mar 19 2015 23:12
yes like typing markdown into this chat window :-)
Elia Schito
@elia
Mar 19 2015 23:13
lol
Mitch VanDuyn
@catmando
Mar 19 2015 23:13
anyway if you like I will update the docs someplace appropriate, as its a good usecase (i.e. a gem that runs fine on both server or browser)
Elia Schito
@elia
Mar 19 2015 23:14
@catmando that would be awesome :sparkles:
Mitch VanDuyn
@catmando
Mar 19 2015 23:35
Do you think it makes sense to create a section on the opalrb.org docs at the end called "Configuring gems to run in opal" ?
Elia Schito
@elia
Mar 19 2015 23:36
@catmando yes, opalrb really needs those kinds of docs
Mitch VanDuyn
@catmando
Mar 19 2015 23:36
Okay will do... it should just be a quick page or so.