Register

Originally, CoffeeScript had a coffeescript/register module, which would automatically compile CoffeeScript files on the fly.
Originally, Babel later adopted it, producing the
@babel/register module, which
will automatically compile .es6
, .es
, .jsx
, .mjs
, and .js
files the
fly.
Now, Ruby2JS has adopted the idea, and in fact uses the
same hook, and makes available
the @ruby2js/register
module, which will automatically compile .rb
files on the fly.
Demonstration
Let’s start with the simple and somewhat canonical greet function in Ruby:
# greet.rb
def greet(name)
puts "Hello #{name}!"
end
Now add a main program, in JavaScript, which registers and configures Ruby2JS, and then calls out to the above script:
// main.js
require('@ruby2js/register')({
options: {
eslevel: 2021,
autoexports: 'default',
filters: ['cjs', 'functions']
}
})
const greet = require('./greet')
greet('World')
All that’s left is to install the module and go:
yarn add @ruby2js/register
node main.js
Enjoy!