Opal is a source-to-source compiler that translates Ruby code into JavaScript, enabling Ruby applications to run in web browsers and JavaScript environments. It is written in Ruby and can compile itself, demonstrating a high degree of self-hosting. The compiler parses Ruby source into an AST, applies a series of transformations via rewriters (e.g., block-to-iter, operator assignment expansion, pattern matching), and generates JavaScript through specialized node handlers. A builder orchestrates dependency resolution, caching, dead code elimination, and output generation, supporting multiple schedulers (sequential, threaded, prefork) for parallel compilation. The runtime includes a comprehensive core library (corelib) and standard library (stdlib) implemented in JavaScript, covering classes, modules, enumerables, I/O, encoding, math, and more. Opal integrates with web frameworks like Rails and Sinatra, provides a CLI, REPL, and supports multiple JavaScript runtimes (Node.js, Deno, Bun, QuickJS, GJS, browsers via CDP). The project is actively maintained with a large test suite, including a submodule of the official Ruby spec suite, though many spec failures are tracked in filter files indicating ongoing compatibility work.
opal-rails is a Rails engine that integrates the Opal Ruby-to-JavaScript compiler into the Rails asset pipeline. It provides a build-based workflow where Opal entrypoints (Ruby files) are compiled into JavaScript assets via Rake tasks (`opal:build`, `opal:watch`, `opal:clobber`), replacing the older Sprockets request-time compilation. The gem includes generators for initial setup and asset scaffolding, a template handler for `.js.opal` views with optional controller assigns, and Haml/Slim filters for inline Opal code. It hooks into Rails' `assets:precompile` and test preparation tasks to ensure Opal assets are built automatically. The architecture is centered around a `BuilderRunner` that wraps `Opal::Builder`, an `OutputsManifest` that tracks built files, and a `WatchRunner` that uses the `listen` gem for incremental rebuilds during development. The codebase is well-structured with clear separation of concerns, dependency injection, and comprehensive test coverage, making it a solid example of a Rails engine that extends the asset pipeline.