withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.82k stars 2.49k forks source link

Unknown file extension ".wasm" in endpoint #12104

Closed ambrt closed 1 month ago

ambrt commented 1 month ago

Astro Info

Astro                    v4.15.10
Node                     v20.4.0
System                   Linux (x64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/svelte
                         astro-auth

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

In both minimal installation (on stackblitz) and on my PC (with @astrojs/cloudflare), adding npm install @cf-wasm/photon and importing it in endpoint throws error:

[ERROR] Unknown file extension ".wasm" for /home/user/project/node_modules/@cf-wasm/photon/dist/esm/lib/photon_rs_bg.wasm
  Stack trace:
    at new NodeError (node:internal/errors:405:5)
    at defaultGetFormat (node:internal/modules/esm/get_format:142:36)
    at DefaultModuleLoader.load (node:internal/modules/esm/loader:263:26)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at DefaultModuleLoader.getJobFromResolveResult (node:internal/modules/esm/loader:156:34)

Error is same when using Cloudflare adapter and minimal Astro repo.

What's the expected result?

Astro should load ".wasm" from @cf-wasm/photon (with/without Cloudflare adapter)

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-s3fqit?file=src%2Fpages%2Fphoton.ts,package.json&on=stackblitz

Participation

ascorbic commented 1 month ago

When running astro dev it uses the Vite dev server, which uses Node. This means Cloudflare-only code can be hard to run. In this case it's because Vite can't resolve wasm without an extra plugin, or with an '?init param on the import. I downloaded your repo and it works fine as-is if I run npm run build and then npx wrangler pages dev ./dist. To get it working in dev you can change the import to import * as photon from '@cf-wasm/photon/node';