aklinker1 / vite-plugin-web-extension

Vite plugin for developing Chrome/Web Extensions
https://vite-plugin-web-extension.aklinker1.io/
MIT License
537 stars 46 forks source link

Doesn't support WASM #176

Closed michaelessiet closed 5 months ago

michaelessiet commented 6 months ago

Summary

I'm currently building a web extension that makes use of tiny-secp256k1 by bitcoinjs. Prior to making use of this all I had to add to my vite config was the vite-wasm-plugin and vite-plugin-top-level-await. However, this plugin isn't compatible with WASM in turn making it incompatible with vite-wasm-plugin.

Please is there a way around this? I have dropped the error screenshot below. The error seems to be about the bundle target being iife instead of es, but I set esnext as my target in the vite.config.js

Screenshot 2024-01-06 at 19 14 54

Environment

Screenshot 2024-01-06 at 19 19 42

aklinker1 commented 5 months ago

Could you share a reproduction so I can look into this? I have minimal experience with wasm, and it would take a long time for me to maybe reproduce this on my own.

devmattrick commented 3 months ago

I also bumped into this, but I don't think this is related to wasm. It should happen anywhere you try to use top-level await, which is not supported by iife modules. I have a simple repro available here.

Is there a reason to use iife here over es? I haven't tested it, but I would guess recent browsers would support ESM in browser extension scripts?

Edit: I'm wrong about recent browsers supporting ESM in extension scripts (at least for content scripts) [1][2].

That being said, there are workarounds to allow us to import ESMs as content and background scripts. It'd be helpful to me if it was possible to have the plugin only output ESM and I'd manually write the shim to import the module. Here's an example of that in action.