question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

import.meta.url not available in dependencies, wasm-pack modules unusable in dev mode?

See original GitHub issue

Describe 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pixelsparkcommented, Mar 13, 2022

So apparently the package is ‘optimized’ and put in node_modules/.vite but the referenced WASM-file is not. It is still available however as node_modules/@webonnx/wonnx-wasm/wonnx_bg.wasm. I fixed the issue by just excluding the package in optimizeDeps:

import { defineConfig } from "vite";

export default defineConfig({
  optimizeDeps: {
    exclude: ["@webonnx/wonnx-wasm"],
    esbuildOptions: {
      target: "es2020",
    },
  },
});
1reaction
bluwycommented, Mar 12, 2022

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

The `wasm-bindgen` Guide - Rust and WebAssembly
Introduction. This book is about wasm-bindgen , a Rust library and CLI tool that facilitate high-level interactions between wasm modules and JavaScript.
Read more >
Cannot use 'import.meta' outside a module - Stack Overflow
Need to get the current working directoy because I have resources in there that I need for the app to run. Need the...
Read more >
A First Look at PyScript: Python in the Web Browser
In this tutorial, you'll learn about PyScript, a new framework that allows for running Python in the web browser with few or no...
Read more >
Rust+WASM+Vite使用记录 - 掘金
import.meta.url not available in dependencies, wasm-pack modules unusable in dev mode? · Issue #7287 · vitejs/vite (githu…
Read more >
Changelog - esbuild - Breword 文档集合
When this bug occurred, the generated ESM code contained an export and import for a symbol that didn't exist, which caused a module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found