import.meta.url not available in dependencies, wasm-pack modules unusable in dev mode?
See original GitHub issueDescribe the bug
I have built a package (@webonnx/wonnx-wasm) containing a WebAssembly module from Rust using wasm-pack. The source is here, command line used to build:
RUSTFLAGS=--cfg=web_sys_unstable_apis wasm-pack build --target web -d `pwd`/target/pkg --out-name wonnx --scope webonnx ./wonnx-wasm
I tried building a very simple app using Vite to test the package. Upon opening the app however, I get the following error:
Uncaught (in promise) TypeError: Failed to construct 'URL': Invalid URL
at init (wonnx.js:422:17)
at run (main.js:5:9)
at main.js:32:1
The issue appears to be with this line in the part of the wonnx-wasm package that loads the WebAssembly module:
input = new URL('wonnx_bg.wasm', import.meta.url);
Further digging revealed that import.meta.url
is undefined here. It is defined when I try to log it from the app code. (So: Vite is likely not defining it for dependencies?).
Interestingly running npm run build
makes it work just fine! (You will likely still see the error TypeError: Cannot read properties of undefined (reading 'requestAdapter')
in your browser because the package attempts to use WebGPU, that’s obviously not a problem in Vite). In the bundle the offending line compiles to:
_=new URL("/assets/wonnx_bg.d1675c64.wasm",self.location));
So my question is (1) is this behaving as expected, and (2) if so, how am I supposed to be using packages like this in dev mode (e.g. from wasm-pack) with Vite / is there anything wasm-pack should change?
Reproduction
https://github.com/pixelspark/wonnx-wasm-test
System Info
System:
OS: macOS 12.2.1
CPU: (10) arm64 Apple M1 Max
Memory: 25.01 GB / 64.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.14.0 - ~/.brew/bin/node
npm: 8.3.1 - ~/.brew/bin/npm
Browsers:
Chrome Canary: 101.0.4939.0
Edge: 99.0.1150.36
Firefox: 97.0.2
Firefox Nightly: 99.0a1
Safari: 15.3
Safari Technology Preview: 15.4
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
So apparently the package is ‘optimized’ and put in
node_modules/.vite
but the referenced WASM-file is not. It is still available however asnode_modules/@webonnx/wonnx-wasm/wonnx_bg.wasm
. I fixed the issue by just excluding the package in optimizeDeps:I think a warning is good for the interim, but I haven’t took a deeper look of why this happens in the first place and whether it can be fixed within esbuild (or Vite), so we can fix it without any warnings in the first place. Feel free to hack a PR out if you want though, it makes sense to me that
esbuild
warnings after pre-bundling should be printed out (Not sure if that could get noisy 🤔)I’ll close this issue then in favour of #5270 so we can keep the discussion in one place. Thanks for looking into this!