Integrate opalrb with sprockets on sinatra

109 Views Asked by At

I'm wanting to integrate Opal into my next project and decided to just move onto sprockets from sinatra-asset-pack, however I can't find anything on how to integrate opal into sprockets rather than just setting up sprockets and opal separately.

How do I set up sprockets and integrate Opal.rb with it?

Bearing in mind I don't run it as a rackup file, this is due to our environment so it's just not viable, instead I bundle my app as a gem and run it via executable.

Or perhaps just point me in the right-direction to do this if some resource on this already exists and I just wasn't able to find it, that would be fab as well.

1

There are 1 best solutions below

0
On BEST ANSWER

I asked on the opal github page and they pointed me to a place in their own code where they do what I'm asking, I'll post a link and once I do it myself I'll post the code I've created.

Here's the link to the code on github, the highlighted section is the bit you want to read, be aware that it's rails specific, which to be honest is kind of throwing me.

EDIT:

Ok, so I've been able to make some progress and now I have sprockets running with opalrb, sort of. The browser responds with 200 so it's found the file, the mime type is also correct... the problem is that the file it gets is blank, nothing has been rendered. For any one interested in my progress, here's a gist with how far I've gotten.

EDIT:

Ok, so I now have sprockets providing assets and opal rendering out java script files from ruby source code. However none of the code I'm putting into the pre-compiled ruby file is actually doing anything le sigh.

Still, Here's a link to the repo containing the code so far.

EDIT:

Turns out, to get the the opal stuff to run you need to run a command to bootstrap the opal code; I Created a helper to do this for me:

def opal_boot_code_for(file)
    "<script>#{Opal::Processor.load_asset_code(settings.sprockets, file)}</script>"
end