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

Background service worker : document is not defined #392

Closed
lionelhorn opened this issue Feb 1, 2024 · 6 comments · Fixed by #402
Closed

Background service worker : document is not defined #392

lionelhorn opened this issue Feb 1, 2024 · 6 comments · Fixed by #402
Labels
bug Something isn't working

Comments

@lionelhorn
Copy link
Contributor

Describe the bug

In background context : If imported code from third party librariries are using document, the background script may fail to start or load with Uncaught ReferenceError: document is not defined error.

To Reproduce

https://github.com/lionelhorn/wxt-wasm-vlcn-repro

Steps to reproduce the bug using the reproduction:

  1. Install dependencies: pnpm i
  2. Start dev mode: pnpm dev
  3. Open chrome://extensions/
  4. Reload wxt-wasm-vlcn-repro extension
  5. Open errors

Expected behavior

Background script / service worker to start without errors.

Screenshots

image

Environment

  System:
    OS: Windows 11 10.0.22631
    CPU: (32) x64 AMD Ryzen 9 7950X 16-Core Processor
    Memory: 33.11 GB / 63.15 GB
  Binaries:
    Node: 18.18.2 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.14.0 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (121.0.2277.83)
@lionelhorn lionelhorn added the pending-triage Someone (usually a maintainer) needs to look into this to see if it's a bug label Feb 1, 2024
@lionelhorn
Copy link
Contributor Author

Tried to add document in define in wxt.config.ts but it doesn't seem to help.

https://github.com/lionelhorn/wxt-wasm-vlcn-repro/blob/0965998a3f8f673240b0ff0fb87af5fba79ed5ba/wxt.config.ts#L34

@lionelhorn lionelhorn changed the title Background service worker : Document is not defined Background service worker : document is not defined Feb 1, 2024
@lionelhorn
Copy link
Contributor Author

The function initWasm im calling in background.ts needs a module built using emscripten

https://github.com/vlcn-io/js/blob/b1574592f87067c8d6ddc269d9ad26018cfde05b/packages/crsqlite-wasm/src/index.ts#L1

And apparently emscripten has these document ..
emscripten-core/emscripten#7222

So I don't have much control over this.

@lionelhorn
Copy link
Contributor Author

@aklinker1 aklinker1 added bug Something isn't working and removed pending-triage Someone (usually a maintainer) needs to look into this to see if it's a bug labels Feb 2, 2024
@aklinker1
Copy link
Collaborator

aklinker1 commented Feb 2, 2024

Thanks for the report, I'm adding ESM support to the background (#398), so it should be working after that is in.

I'll make sure to re-test this issue once that is merged.

@lionelhorn
Copy link
Contributor Author

lionelhorn commented Feb 2, 2024

Thanks for the update. Will look into it.
Added a question here #398 (comment). Not sure if you get the mention notifications.

@aklinker1
Copy link
Collaborator

aklinker1 commented Feb 3, 2024

Fix released in v0.16.2. Here's a working background.ts after upgrading:

import initWasm from "@vlcn.io/crsqlite-wasm";
import wasmUrl from "@vlcn.io/crsqlite-wasm/crsqlite.wasm?url";

export default defineBackground({
  main() {
    initSqlite();
  },
});

async function initSqlite() {
  const sqlite = await initWasm(() => wasmUrl);

  const db = await sqlite.open("database.db");
  await db.exec(
    "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY NOT NULL, name TEXT)"
  );

  const stmt = await db.prepare(
    "INSERT OR IGNORE INTO users (name) VALUES (?)"
  );
  await stmt.run(db, `John Do. Born at ${new Date().toTimeString()}`);

  const users = await db.execA<[bigint, string]>("SELECT * FROM users");
  console.log("Saved users", users);
}

Here's the full code based on your reproduction: working-wasm-example.zip

lionelhorn added a commit to lionelhorn/wxt-wasm-vlcn-repro that referenced this issue Feb 4, 2024
- WXT dev fixed the blocking document undefined issue. See wxt-dev/wxt#392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants