js.erb files with esbuild in Rails 7

274 Views Asked by At

I would like to package a js.erb file with esbuild. In my app, a user clicks on a 'Buy' button, which is connected to a Stimulus controller. The Stimulus action redirects to Stripe, but needs a public key. I want to read that key from the Rails credentials at esbuild-time, rather than hard-cding it into the js-file. Akin to

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  redirect(event) {
    // event.preventDefault();

    const stripe = Stripe("<%= Rails.application.credentials[:stripe][:public] %>");

    stripe.redirectToCheckout({...});
}

My esbuild line in package.json:

 "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",

I tried without success

Sprockets.register_mime_type 'application/javascript', extensions: ['.js.erb']
0

There are 0 best solutions below