Skip to content

Installation

This guide covers the installation process for opal-vite in different scenarios.

Requirements

RequirementVersion
Node.js18+
Ruby3.0+
pnpm / npmLatest

Install Packages

1. Vite Plugin (npm)

bash
# Using pnpm (recommended)
pnpm add -D vite-plugin-opal

# Using npm
npm install -D vite-plugin-opal

2. Ruby Gem

bash
# Direct install
gem install opal opal-vite

# Or add to Gemfile
bundle add opal opal-vite

Gemfile

ruby
source 'https://rubygems.org'

gem 'opal', '~> 1.8'
gem 'opal-vite', '~> 0.2'

Then run:

bash
bundle install

Configuration

vite.config.ts

typescript
import { defineConfig } from 'vite'
import opal from 'vite-plugin-opal'

export default defineConfig({
  plugins: [
    opal({
      // Ruby source directories
      loadPaths: ['./app/opal'],

      // Enable source maps for debugging
      sourceMap: true,

      // Enable debug output (optional)
      debug: false
    })
  ]
})

Plugin Options

OptionTypeDefaultDescription
loadPathsstring[]['./app/opal']Directories containing Ruby source files
sourceMapbooleantrueGenerate source maps for debugging
debugbooleanfalseEnable debug output
compilerOptionsobject{}Additional Opal compiler options

Project Setup

1. Create Directory Structure

bash
mkdir -p app/opal/controllers

2. Create Entry Point

ruby
# app/opal/application.rb
require 'opal'

puts "opal-vite is working!"

3. Create JavaScript Loader

javascript
// src/main.js
import './app/opal/application.rb'

4. Update index.html

html
<!DOCTYPE html>
<html>
<head>
  <title>My Opal App</title>
</head>
<body>
  <script type="module" src="/src/main.js"></script>
</body>
</html>

5. Start Development Server

bash
pnpm dev

Using with Bundler

If you have a Gemfile, the plugin automatically uses bundle exec to run the compiler:

bash
# Automatically detected
bundle exec ruby -r opal/vite/compiler ...

Verify Installation

After starting the dev server, open the browser console. You should see:

opal-vite is working!

Next Steps

Released under the MIT License.