A Webpack loader for compiling Ruby to JavaScript.
Opal is a Ruby to JavaScript source-to-source compiler that makes it possible to run Ruby code in JavaScript engines, including browsers. This module aims to automate the process of compiling to JavaScript via Webpack.
Features:
- Bundler integration for easy management of gem dependencies
- Configurable load paths
Install the Opal gem:
gem install opal
Or add it to your Gemfile:
gem 'opal'
Add the module to your project's dependencies:
npm install --save-dev opal-webpack-builder
Add the loader to your project's webpack.config.js:
module.exports = {
module: {
rules: [
{
// opal-webpack-bundler will compile and include ruby files in the pack
test: /\.rb$/,
use: [
{
loader: 'opal-webpack-bundler',
options: {
// Set useBundler to false if your project does not have a Gemfile
useBundler: true,
gems: [],
paths: []
}
}
]
}
]
}
}
Import your Ruby code the same way as any other asset, e.g.:
import './example.rb'
useBundler
: Automatically make bundled gems availablegems
: An array of additional gems to make availablepaths
: An array of additional load paths to make available
- Ruby code should include
require 'opal'
to import the Ruby corelib. - If you use Bundler, your Gemfile should be in your project's root directory (the same directory as package.json).
- If your project imports multiple Ruby scripts, each one is compiled separately, so if you have the same paths required in multiple files, the required code might get compiled and loaded multiple times. It's best to import a single Ruby script that loads all of your required libraries from one place.
- Refer to the Opal documentation for more information, including how to reference Ruby code from JavaScript and vice versa.
- Source maps are not supported.
require_relative
does not always work as expected (see opal/opal#1634).
- Provide examples.