local_time: Does not appear to work in Rails 7 using import-map (new Rails default)

Howdy, I’m building an app from scratch in Rails 7 Alpha. This uses import maps.

In the import map, I have pin "helpers/local-time", which works as expected. I can import the file no problem. I import thusly:

import "helpers/local-time"
import LocalTime from "helpers/local-time"

I get the following error:

SyntaxError: Importing binding name 'default' cannot be resolved by star export entries. 
http://localhost:3000/assets/es-module-shims.min.js

I know that’s in the Shim, not in local time, but the error traces to the following location. I’m not a JS guy, so not quite sure how to correct in

function processScript(e) {
    if (e.ep)
      return;
    if (null !== e.getAttribute("noshim"))
      return;
    if (!e.src && !e.innerHTML)
      return;
    e.ep = true;
    const t = ar > 0;
    const s = sr > 0;
    t && ar++;
    s && sr++;
    const a = topLevelLoad(e.src || `${r}?${Ne++}`, getFetchOpts(e), !e.src && e.innerHTML, !c, t && tr).catch((e => {
      setTimeout((() => {
        throw e
      }));
      pe(e)
    }));
    t && (tr = a.then(readyStateCompleteCheck));
    s && a.then(domContentLoadedCheck)
  }

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Ok, the problem seems to come from the js module that is bundled with this gem. The asset pipeline serves that one instead of the one in vendor/javascript and it seems to have problems with the ESM loading.

My workaround to still use importmaps and downloaded js modules was to rename the file to avoid this conflict.

  • $ bin/importmaps pin local-time --download
  • rename vendor/javascript/local-time.js to vendor/javascript/local-time-cdn.js
  • change pin in importmap.rb to pin "local-time-cdn"
  • load LocalTime from that module instead
import LocalTime from "local-time-cdn"
LocalTime.start()

Tested in Firefox 96 and Chrome 97

Edit: To illustrate the issue, here is the js module from this gem: https://raw.githubusercontent.com/basecamp/local_time/main/app/assets/javascripts/local-time.js And the js module that is loaded via importmaps from jspm: https://ga.jspm.io/npm:local-time@2.1.0/app/assets/javascripts/local-time.js

Thanks for quick turnaround – works as expected after version bump.

I’ve just released the newest version. This should now work without having to rename anything. So:

❯ rails new test_app
❯ cd test_app
❯ bundle add local_time
❯ bundle install
❯ bin/importmap pin local-time # works now per https://github.com/rails/importmap-rails/pull/234#issuecomment-1892668735
#=> Pinning "local-time" to vendor/javascript/local-time.js via download from https://ga.jspm.io/npm:local-time@3.0.2/app/assets/javascripts/local-time.es2017-esm.js

Then follow the readme to import and start the library. Let me know if you run into trouble, preferably creating a new issue if unrelated to this one.

With downloading the new default in importmap-rails - https://github.com/rails/importmap-rails/pull/217 - I suspect people using this lib are going to bump into this issue more. Thanks for the workaround guidance! Would love to see version bump with changes, but noted on priorities.

Hey @vietqhoang, I plan to spend my upcoming cooldown tying up loose ends in this library — including cutting a new release. So no promises, but it might happen soon.

In the meantime, you can vendor the JS library (bundled versions are checked into the repo here), and you can point your Gemfile version to the main branch for the Ruby library.

EDIT: I should add the disclaimer that you should test that your app works correctly while running on main before shipping to production.

EDIT 2: This work didn’t make the cut this last cooldown. Maybe next one. Sorry! Can’t make any promises.