• Stars
    star
    5
  • Rank 2,861,937 (Top 57 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use ERB files in Vite.js projects with a Ruby backend

vite-plugin-erb

Use ERB files in Vite.js projects with a Ruby backend


Disclaimer โš ๏ธ

This library is intended for legacy projectsโ€”please do not use it on new projects.

Using ERB with JS and CSS files can lead to a fragile setup where your frontend assets are entangled with your backend setup, making it very hard to migrate to different tooling or frameworks.

The goal of this library is to ease out the transition as you gradually remove your .js.erb files.

If you need to share constants or values between the backend and the frontend, a safer and more performant approach is to instead generate JS or TypeScript from Ruby and import it as usual (or viceversa).

Why? ๐Ÿค”

When migrating to Vite.js from sprockets or similar asset pipelines, it's not unusual to have .erb files that depend on the Ruby runtime to be rendered.

This plugin allows Vite.js to understand .erb files, and render them in the context of your Ruby application.

It provides the same functionality as rails-erb-loader, but for Vite.js.

Installation ๐Ÿ’ฟ

Install the package as a development dependency:

npm i -D vite-plugin-erb # yarn add -D vite-plugin-erb

and then add the plugin to your vite.config.ts file:

import { defineConfig } from 'vite'
import Erb from 'vite-plugin-erb'

export default defineConfig({
  plugins: [
    Erb(),
  ],
})

Usage ๐Ÿš€

Once the plugin is installed, you should be able to start importing .erb files.

// app/frontend/constants.js.erb

export const railsEnv = <%= Rails.env.to_json %>
import { railsEnv } from '~/constants.js.erb'

console.log(`Running in ${railsEnv}`)

Configuration โš™๏ธ

If things are not working out of the box, you might need to tweak some the following settings.

For example:

plugins: [
  Erb({
    engine: 'erb',
    runner: 'ruby ./boot.rb',
    env: { RACK_ENV: process.env.NODE_ENV },
    extendEnv: false,
    timeout: 5000,
  }),
],

engine

The ERB template engine to use. erubi, erubis and erb are supported.

You can manually specify which one to use if needed, but they will be detected automatically.

runner

The command to run the internal ruby script.

If bin/rails is detected, Rails runner will be used, giving you access to the application environment.

You may provide ruby ./boot.rb or something similar if using other frameworks.

env

Additional environment variables to be passed to runner. Defaults to process.env.

extendEnv

Set to false if you want to override process.env instead when providing the env property.

timeout

Te Ruby process will be sent a termination signal if it doesn't return a result under the specified timeout in millis. Defaults to 10000.

A note about spring ๐ŸŒบ

By default DISABLE_SPRING: '1' is set in env because if the spring client is started from Node.js the Ruby renderer process would never finish.

This makes the rendering process significantly slower, specially in large apps.

You may provide env: { DISABLE_SPRING: '0' } to re-enable spring, but make sure to run bin/rails runner '' before starting the Vite dev server to prevent this issue, or reload the page after the first visit (which would timeout).

Acknowledgements

License

This library is available as open source under the terms of the MIT License.

More Repositories

1

vite_ruby

โšก๏ธ Vite.js in Ruby, bringing joy to your JavaScript experience
Ruby
1,270
star
2

iles

๐Ÿ The joyful site generator
TypeScript
1,074
star
3

vite-plugin-image-presets

๐Ÿ–ผ Image Presets for Vite.js apps
TypeScript
246
star
4

vite-plugin-environment

Easily expose environment variables in Vite.js
TypeScript
144
star
5

vite-plugin-full-reload

โ™ป๏ธ Automatically reload the page when files are modified
JavaScript
134
star
6

oj_serializers

โšก๏ธ Faster JSON serialization for Ruby on Rails. Easily migrate away from Active Model Serializers.
Ruby
113
star
7

js_from_routes

๐Ÿ›ฃ๏ธ Generate path helpers and API methods from your Rails routes
Ruby
98
star
8

types_from_serializers

โœ… Generate TypeScript interfaces from your JSON serializers
Ruby
92
star
9

request_store_rails

๐Ÿ“ฆ Per-request global storage for Rails prepared for multi-threaded apps
Ruby
85
star
10

vuex-stores

๐Ÿ—„ Store objects for Vuex, a simple and more fluid API for state-management.
JavaScript
62
star
11

vue-custom-element-example

An example on how to define custom elements using Vue 3
TypeScript
59
star
12

jekyll-vite

โšก๏ธ๐Ÿฉธ Use Vite.js in Jekyll as your assets pipeline
Ruby
50
star
13

vite-plugin-stimulus-hmr

โšก๏ธ HMR for Stimulus controllers in Vite.js
TypeScript
50
star
14

mongoid_includes

๐ŸŒฟ Improves eager loading support for Mongoid
Ruby
46
star
15

stimulus-vite-helpers

Helpers to easily load all your Stimulus controllers when using Vite.js
TypeScript
43
star
16

queryable

โ” Gives your queries a home and avoid tucking scopes inside your models
Ruby
42
star
17

capybara-compose

โœ… Easily write fluent integration tests with Capybara in Ruby
Ruby
32
star
18

better_settings

โš™ Settings for Ruby apps โ€“ fast, immutable, better.
Ruby
20
star
19

vite-plugin-bugsnag

Report builds and upload source maps to Bugsnag
TypeScript
18
star
20

i18n_multitenant

๐ŸŒŽ Provides a convenient way to use tenant-specific translations
Ruby
18
star
21

vite-plugin-manifest-sri

Subresource Integrity for Vite.js manifest files
JavaScript
15
star
22

resourcerer

โœจ Works like magic to dry up your controllers
Ruby
10
star
23

sublime-toggle-dark-mode

๐ŸŒš๐ŸŒž Toggle between dark and light mode in Sublime Text 4
JavaScript
10
star
24

pakiderm

๐Ÿ˜ Pakiderm will never forget the return value
Ruby
7
star
25

presenter_rails

๐Ÿ”ญ Expose your view models in a convenient way
Ruby
6
star
26

journeyman

Let your factories use your business logic, making them flexible and easier to update.
Ruby
5
star
27

jekyll-vite-minima

โšก๏ธ๐Ÿฉธ Use Vite.js in Jekyll minima theme as your assets pipeline
Ruby
3
star
28

automatic-music-transcription

Automatically exported from code.google.com
C
2
star
29

vite-plugin-xdm

Use XDM in VIte.js
JavaScript
2
star
30

fast-food-mvc

Automatically exported from code.google.com
C#
1
star
31

ElMassimo

1
star
32

vite-vue-router-hmr-repro

Vue
1
star
33

crouton

๐Ÿž Context sensitive notifications for Rails
Ruby
1
star