NipNipBud
NipNipBud2mo ago

HTTP import equivalent to JSR?

Is there a way to use an HTTP import similar to a JSR? wasm-pack generated JS file has this line which makes Vite resolve to some really strange path.
module_or_path = new URL('polyvis_wasm_bg.wasm', import.meta.url);
module_or_path = new URL('polyvis_wasm_bg.wasm', import.meta.url);
But packages on JSR have this line and are working just fine. Is there a way to HTTP import a package, or is publishing to JSR the only way?
GitHub
Dependency resolves to broken path · Issue #84 · denoland/deno-vi...
deno.json ... "imports": { "vite": "npm:vite@^7.1.2", "@deno/vite-plugin": "npm:@deno/vite-plugin@^1.0.5", "polyvis_wasm.js": "https...
7 Replies
jeff.hykin
jeff.hykin2mo ago
https://esm.sh/jsr/@USERNAME/PACKAGE_NAME@VERISION/ should do the trick
NipNipBud
NipNipBudOP2mo ago
I tried esm.sh, but it seems to be the same problem as HTTP import from GitHub directly. Because new URL('some.wasm', import.meta.url); is a runtime dependency, Deno does not download it to cache, and when Vite bundles using the local cache, it couldn't find the wasm file. For JSR specifier the whole package is downloaded so Vite can find the file. I'm not sure if I'm missing something? I guess the question should be: Is there a way to host a JSR package elsewhere and get the jsr: import behavior in Deno?
GitHub
Caching and loading non-importable resources · Issue #5987 · deno...
Problem I often find myself needing to load non-JavaScript non-TypeScript assets, and various problems arise: Local module (in git repo) has to use fs but remote module has to use fetch. The assets...
jeff.hykin
jeff.hykin2mo ago
Sorry I'm a bit confused on the setup, and maybe I made a bad recommendation. - you're using jsr with Vite, which works - you want to use https instead, but swapping the https import for the jsr one doesnt work with Vite Is that the situation?
NipNipBud
NipNipBudOP2mo ago
yea I think
jeff.hykin
jeff.hykin2mo ago
Okay, its possible this is a Vite problem rather than a deno problem. If you do deno bundle [esm URL] > file.js and then import that file.js does it work?
NipNipBud
NipNipBudOP3w ago
No... because the some.wasm file is still not downloaded alongside the file.js. Basically I needed the NPM github: specifier behavior where it blindly downloads everything in the repo, even if Deno doesn't see it in an import statement (can't use import for wasm in web because WASM EMS Integration is not in browsers). And as far as I can tell this behavior is only reserved for jsr:. My setup is a wasm-pack generated package hosted on a GitHub repo or as a tarball, which doesn't work in Deno as explained. But if I use Bun with the github: specifier it works. Looking at JSR, there are a bunch of wasm-pack generated package with the same fetch(new URL('some.wasm', import.meta.url)); way of getting the wasm file, so I assume the reason they work is because Deno treats JSR differently and downloads all files (I didn't really test them).
jeff.hykin
jeff.hykin2w ago
Does the source code (before bundling) import the wasm just like any other ES import? (I'm guessing yeah)
can't use import for wasm in web
Yes, but actually esm.sh (not deno bundle) can handle that. I mean deno bundle tries to handle it too, but yeah. Esm can do it by transforming the source code (you can import typescript on the web fron esm cause it translates it on the fly) For context esm.sh tries to guess if you're on Deno or the Web. If its on Deno I think it assumes the "you downloaded the whole github repo" that you're talking about. But if its on web, I'm not sure. You can force the URL to be the Web version by adding ?target=esnext to the end of the URL. If that doesn't work, and the wasm is being imported as a normal ES import, then I think thats a bug on esm.sh. If you want to just send me a link to the source code I'll try some stuff.

Did you find this page helpful?