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

Error when importing on vite #1106

Open
Niedzwiedzw opened this issue Jan 28, 2022 · 4 comments
Open

Error when importing on vite #1106

Niedzwiedzw opened this issue Jan 28, 2022 · 4 comments

Comments

@Niedzwiedzw
Copy link

Niedzwiedzw commented Jan 28, 2022

image

it's this code that creates that error

import blackspotjs from 'blackspotjs';
// ..
const b = await blackspotjs();

this package was built with latest wasm-pack with --target web option.

@Niedzwiedzw
Copy link
Author

rustwasm/wasm-bindgen#2779 cross-posting the issue partially solved but still in progress on wasm-bindgen repo

@stefnotch
Copy link

I did some more digging while attempting to import brotli-wasm. It might be interesting
httptoolkit/brotli-wasm#8 (comment)

@hazelmeow
Copy link

hazelmeow commented May 7, 2024

For me, Vite was showing the error: The request url "[...]/my-crate/pkg/my_crate_bg.wasm" is outside of Vite serving allow list.

I fixed it by importing my_crate_bg.wasm as a static asset and then passing the URL of it to init.

package.json:

{
  "dependencies": {
    "my-crate": "link:../my-crate/pkg"
  }
}

JS:

import init, { ... } from "my-crate";
import wasmUrl from "my-crate/my_crate_bg.wasm?url";

init(wasmUrl).then(() => { ... });

Seemed to work with either vite-plugin-wasm or vite-plugin-wasm-pack.

@ntninja
Copy link

ntninja commented Jul 18, 2024

@hazelmeow: Thanks for this! Seems like a very clean way to make wasm-pack’s web target interop with Vite using Vite’s native capabilities.

The recommended way nowadays appears to be to use the default wasm-pack build --target bundler mode and then add the vite-plugin-wasm and vite-plugin-top-level-await plugins to Vite support loading the resulting bundle however.

To change existing code to this approach, the following steps are needed:

  1. Add the extra dependencies:
    npm add --save-dev vite-plugin-wasm vite-plugin-top-level-await
  2. Add the plugins to vite.config.js:
    import wasm from "vite-plugin-wasm";
    import topLevelAwait from "vite-plugin-top-level-await";
    
    export default defineConfig({
      // …
      plugins: [
        wasm(),
        topLevelAwait(),
        // …
      ],
      // …
    };
  3. Update the import code to only import the needed values as init will now be run automatically:
    import {  } from "my-crate";  // or: import * as wasm from "my-crate";
    
    // Code that was inside init(wasmUrl).then(() => { … })

ntninja added a commit to ntninja/rustpad that referenced this issue Jul 18, 2024
…ding WebAssembly component

Fixes dev-mode. This makes use of native Vite capabilities to load the
WebAssembly component and appears to be the recommended approach nowadays.

See: rustwasm/wasm-pack#1106 (comment)
ntninja added a commit to ntninja/rustpad that referenced this issue Jul 19, 2024
…ding WebAssembly component

Fixes dev-mode. This makes use of native Vite capabilities to load the
WebAssembly component and appears to be the recommended approach nowadays.

See: rustwasm/wasm-pack#1106 (comment)
ntninja added a commit to ntninja/rustpad that referenced this issue Jul 19, 2024
…ding WebAssembly component

Fixes dev-mode. This makes use of native Vite capabilities to load the
WebAssembly component and appears to be the recommended approach nowadays.

See: rustwasm/wasm-pack#1106 (comment)
ekzhang added a commit to ekzhang/rustpad that referenced this issue Jul 20, 2024
…ding WebAssembly component (#75)

* Switch to `wasm-pack build --target bundler` and Vite plugins for loading WebAssembly component

Fixes dev-mode. This makes use of native Vite capabilities to load the
WebAssembly component and appears to be the recommended approach nowadays.

See: rustwasm/wasm-pack#1106 (comment)

* Fix package-lock.json not matching up

* Mention updated port for dev-environment with newer Vite

* Fix release builds by pinning @swc/core to 1.6.*

Apparently @swc/core 1.7.0 (recently released) has a regression that breaks
vite-plugin-top-level-await:
Menci/vite-plugin-top-level-await#52

* Commit difference between package-lock.json generated by NPM 10 in Docker environment vs NPM 9 locally

---------

Co-authored-by: Eric Zhang <ekzhang1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants