Maplibre and Rails 7 importmaps

122 Views Asked by At

in config/importmap.rb:

pin "maplibre-gl", to: "https://ga.jspm.io/npm:[email protected]/dist/maplibre-gl.js"

However, trying to use this library: import * as maplibregl from "maplibre-gl" (or a range of variants of this), results in the error: Could not resolve "maplibre-gl"

Edit based on Alex's feedback: It seems I can reference the import map imported library from a page or partial. However, I can't reference it from a Stimulus controller. I've adjusted the question to reflect this.

Does anybody know how to use import maps to reference (and use) MapLibre from a Stimulus controller? And, why would it be different from there?

Restarting the server (bin/dev) has no effect.

My guess is that MapLibre is not exporting its modules etc correctly. But validating that, or working around this problem, is probably beyond me.

I would expect that maplibre could be included as a script in the head section of the html document. But, that doesn't feel like the most sensible way to resolve this.

Thanks in advance.

1

There are 1 best solutions below

0
renen On

I think the reason I was struggling is that the import prefixes the imported module with default.

So, import as follows:

import * as maplibregl from "maplibre-gl"

Reference as:

new maplibregl.default.Map

Note the use of default.