Closed valterkraemer closed 9 months ago
Could you confirm whether your code works with Webpack's asyncWebassembly
?
I guess the module you imported in your worker code is using document
. I don't think my plugin will inject that.
Could you confirm whether your code works with Webpack's
asyncWebassembly
?
No, don't really know how I would do that. I btw don't expect you to look into this, and am totally fine with you closing this as "won't do" or whatever. More thought it could be helpful for others.
I guess the module you imported in your worker code is using
document
. I don't think my plugin will inject that.
When not using format: es
it fails in this code because document
isn't defined:
(function() {
"use strict";
(async () => {
var v = "" + new URL("assets/imagequant_bg-29aff33d.wasm", document.currentScript && document.currentScript.src || new URL("assets/worker-e8eb1697.js", document.baseURI).href).href,
A = async (n={}, _) => {
let t;
if (_.startsWith("data:")) {
const l = _.replace(/^data:.*?base64,/, "");
let o;
if (typeof Buffer == "function" && typeof Buffer.from == "function")
o = Buffer.from(l, "base64");
...
and I could find similar code in https://github.com/Menci/vite-plugin-wasm/blob/541d7e46e89c13a3fd07061e33a88a615ee0d059/src/wasm-helper.ts#L11-L15
If using format: es
it becomes:
(async () => {
var j =
"" + new URL("assets/imagequant_bg-29aff33d.wasm", import.meta.url).href,
O = async (_ = {}, n) => {
let o;
if (n.startsWith("data:")) {
const d = n.replace(/^data:.*?base64,/, "");
let t;
if (typeof Buffer == "function" && typeof Buffer.from == "function")
t = Buffer.from(d, "base64");
...
To my understanding it's this line https://github.com/Menci/vite-plugin-wasm/blob/541d7e46e89c13a3fd07061e33a88a615ee0d059/src/index.ts#L48 that becomes the one containing document
.
Also this doesn't happen during vite dev
, but only when vite build
+ vite preview
.
I encountered exactly the same issue with wasm-pack + worker + sveltekit in production.
Adding format: "es"
to vite resolves the issue.
Thanks @valterkraemer for raising the issue and providing a workaround!
Oh it's a bug in vite-plugin-top-level-await. Let me fix it in that plugin...
I am at least getting
document is not defined
error if I'm not setting this isvite.config.ts
.I am importing wasm into a Web Worker where document isn't defined.
Code repo.
Using: