How to add javascript contained in a gem, via import maps?

Suppose we have a javascript file that is shipped with a ruby gem.

I wish to add this javascript file via import map rails.

How can I do this?

# config/intializers/pagy.rb
Rails.application.config.importmap.paths << Pagy.root.join('javascripts/pagy-module')
# importmap.rb
pin 'pagy-module', to: Pagy.root.join("/javascripts/pagy-module.js"), preload: true

And use a stimulus controller:

import { Controller } from "@hotwired/stimulus"
import Pagy from "pagy-module"

export default class extends Controller {
  connect() {
    console.log("hello pagy!")
    Pagy.init(this.element)
  }
}

Is there anything that stands out as completely absurd? I don’t know what I’m doing.

any pointers would be appreciated.

Here’s a repo if you need a running start: GitHub - benkoshy/pagy-rails: Demo for pagy styling and pagy extras.