npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-plugin-erb

v1.1.3

Published

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

Readme

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.