Skip to content

Adding import map support #6141

Closed
@shanet

Description

@shanet

Problem Statement

Short version: I want to load the Sentry JavaScript library via an Import Map, but it doesn't seem to be supported.


Longer version: I'm working on a new Rails project where I'm trying to do away with Webpack and crowd entirely. This has so far been possible with the use of the importmap-rails gem which adds support for JavaScript import maps to Rails. See for more details, but in short import maps allow the use of ES6 modules directly in the browser without the need for a backend Webpack-style compilation which greatly simplifies the amount of backend work needed to maintain a difficult to work with build system like that (I'm not a big fan of those if you can't tell!). Native browser support for import maps is nearly available with Firefox and Safari being the last major holdouts: https://caniuse.com/import-maps.

JSPM has import-map compatible versions of all NPM packages available for use to varying degrees of compatibility depending on the package. In theory these should be able to be served from one's website either by linking directly to their CDN or by downloading the packages locally and serving them yourself.

In regards to Sentry, I tried to do this a few different ways but was unsuccessful with all, except sort of one.

  1. For Sentry, the CDN-hosted version from JSPM works decently well (which gave me encouragement that it could work!).
  2. However, I don't want to rely on a third-party CDN so I tried to download it's files for the Sentry package and serve them myself. This failed with a bunch of 404 errors. After digging into this, I found out that the problem was that the files generated by JSPM were not complete. When loading from their CDN the missing modules would load from their CDN. However, when served locally these modules were truly missing and would result in 404 errors. This may be more of an issue with JSPM than Sentry, I'm not sure.
  3. I also tried to use the NPM package directly. It's somewhat hacky with Importmaps, but for other packages I've used Yarn to download the package locally, then create a symlink to the relevant node_modules directory and tell the import map to load all modules from that directory. I got decently far with this approach but it fell down when working with Sentry's browserPolyfill modules as I didn't have a way to get the load path correct for being loaded via an import map.
  4. From here I tried downloading the built bundle files from Sentry's CDN and serving those. However, this failed because I couldn't get the bundle to have the correct exports available to my own Sentry module which was attempting to import from @sentry/browser. I also tried importing directly from a URL like import * as Sentry from https://browser.sentry-cdn.com/7.17.4/bundle.tracing.min.js but this failed with the same issue.
  5. Finally, since Sentry no longer publishes built bundles in the NPM package I tried building one myself. I didn't think it would be any different from the CDN version but I gave it a shot anyway if I could have more control over the exports. This too failed with the same issue as the CDN version above. There might be more here to explore but at this point it was nearing 3am and I was about tapped out for the day.

What I ended up doing was using Sentry-javascript outside of my importmaps system. I downloaded the CDN bundle and am serving that directly as a standalone JS file with its own <script src=... tag in my head. This works fine for me since I had to set the user ID context in my view templates anyway. But it would be nice to have Sentry served like the rest of my third-party JS libraries via an import map.

Solution Brainstorm

So... possible solutions: I'm not much of a JavaScript person so really I'm not sure what it would take to make Sentry compatible with an import map. From my attempts yesterday it would seem the issue lies with getting the proper import paths set so they'll load in a browser instead of in a backend build system like Webpack. Exactly what needs to happen for that, I'm not sure.

It's also possible that what JSPM is doing to build an import map compatible version is 95% of the way there and the paths just need adjusted so the downloaded version for self-hosting has all of the necessary files.

Anyway, it would be great to have support here since in general I've really enjoyed using import maps and not having to deal with Webpack/other build tools anymore and the more of that adoption we can drive, the better in my opinion.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions