Insular
Insular is a Ruby gem that enables rendering JSX components in Ruby applications through the Deno runtime. It provides a bridge between Ruby and JavaScript/JSX, allowing you to render Preact components on the server side while writing your application logic in Ruby.
How It Works
Insular runs a Deno-based server that handles JSX rendering. Your Ruby application sends component information (path, name, and props) to this server, which:
- Dynamically imports the requested JSX component
- Renders it to an HTML string using Preact
- Returns the rendered HTML to your Ruby application
This architecture allows you to:
- Write UI components in JSX/Preact
- Use them seamlessly within Ruby applications
- Take advantage of the Deno runtime's module caching and performance
Installation
Add this gem to your application's Gemfile:
bundle add insularOr install it directly:
gem install insularUsage
Starting the Rendering Server
Before rendering components, you need to start the Insular Deno server:
bundle exec insular serverThe server accepts the following environment variables:
-
INSULAR_SERVER_HOST- Server host (default:localhost) -
INSULAR_SERVER_PORT- Server port (default:3001)
Alternatively, pass flags directly:
bundle exec insular server --host 0.0.0.0 --port 3001Rendering Components
Use the HTTPRenderer to render JSX components from your Ruby application:
require 'insular'
# Render a component with props
html = Insular::HTTPRenderer.render(
'./tmp/components', # Path to components directory
'Name', # Component name (without .tsx)
{ value: 'World' } # Props to pass to the component
)
puts html
# => <span class="ok">Hello World</span>Component File Structure
Place your JSX components in a directory accessible to the Deno server. Components should export a function:
// tmp/components/Name.tsx
export function Name({ value }: { value: string }) {
return (
<span class="ok">
Hello {value}
</span>
)
}Using the Embedded Renderer
For more direct control, you can use the embedded renderer which integrates the Deno runtime directly:
require 'insular'
html = Insular::EmbeddedRenderer.render(
'./tmp/components',
'Name',
{ value: 'World' }
)Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/insular/insular.
License
The gem is available as open source under the terms of the MIT License.