Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: onResolve error from bundling module resultion when importing wasm modules from npm packages #4726

Closed
IgorMinar opened this issue Jan 8, 2024 · 1 comment · Fixed by #4748 or #4734
Labels
bug Something that isn't working wrangler Relating to the Wrangler CLI tool

Comments

@IgorMinar
Copy link
Contributor

Which Cloudflare product(s) does this pertain to?

Wrangler core

What version(s) of the tool(s) are you using?

3.22.3

What version of Node are you using?

20.10.0

What operating system are you using?

Mac

Describe the Bug

If I try to import a wasm module from an npm package in my worker using the node module resolution, for example:

import JPEG_DEC_WASM from '@jsquash/jpeg/codec/dec/mozjpeg_dec.wasm';

I get the following error when running wrangler dev:

✘ [ERROR] ENOENT: no such file or directory, open '/Users/foo/bar/src/@jsquash/jpeg/codec/dec/mozjpeg_dec.wasm' [plugin wrangler-module-collector]

    src/jsquash-transform.ts:3:26:
      3 │ import JPEG_DEC_WASM from '@jsquash/jpeg/codec/dec/mozjpeg_dec.wasm';
        ╵                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  This error came from the "onResolve" callback registered here:

    node_modules/.pnpm/wrangler@3.22.3_patch_hash=whuv2ih5kundyyu7rtaqweiv2a/node_modules/wrangler/wrangler-dist/cli.js:123256:19:
      123256 │             build5.onResolve(
             ╵                    ~~~~~~~~~

    at /Users/foo/bar/node_modules/.pnpm/wrangler@3.22.3_patch_hash=whuv2ih5kundyyu7rtaqweiv2a/node_modules/wrangler/wrangler-dist/cli.js:123256:20
    at Array.forEach (<anonymous>)
    at /Users/foo/bar/node_modules/.pnpm/wrangler@3.22.3_patch_hash=whuv2ih5kundyyu7rtaqweiv2a/node_modules/wrangler/wrangler-dist/cli.js:123255:22
    at Array.forEach (<anonymous>)

The only way I was able to get this to work was by using a relative path to the module rather than node module resolution:

import JPEG_DEC_WASM from '../node_modules/@jsquash/jpeg/codec/dec/mozjpeg_dec.wasm';

The module resolution should work the same for both js/ts and wasm files.

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

@IgorMinar IgorMinar added the bug Something that isn't working label Jan 8, 2024
@Cherry
Copy link
Contributor

Cherry commented Jan 9, 2024

I fixed this is a while back for modern packages that are using exports in package.json: #4135

For example with @resvg/resvg-wasm, they have an exports map for ./index_bg.wasm so you just import @resvg/resvg-wasm/index_bg.wasm.

It doesn't seem like jSquash does this though: https://github.com/jamsinclair/jSquash/blob/main/packages/jpeg/package.json, so you're stuck with the ../node_modules/.... paths for now.

It would probably be a good idea to try the "node" resolution algorithm on paths like this around here to better support more of the ecosystem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working wrangler Relating to the Wrangler CLI tool
Projects
Archived in project
3 participants