wxt-dev / wxt-dev/wxt

vite does not package worker.js with `new Worker(new URL("./worker.js", import.meta.url)`

Open
#2,032 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
10.5k
Forks
564
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

If I use

const worker = new Worker(new URL("./worker.js", import.meta.url), {
  type: "module",
});

worker.js will not be packaged.

This is the recommended way to package a Worker according to vite: https://v3.vitejs.dev/guide/features.html.

Reproduction

Archive.zip

Steps to reproduce

pnpm install.

From the output, you can see that worker.js and sqlite3.wasm are not bundled.

The reason is that in defineImportMeta.mjs shipped by wxt, import.meta.url is replaced by self.location.href. This confused the bundler.

This seems to be related to #392. Personally, I don't think such substitution makes any sense on wxt level given such severe side effects, which confused vite's recommend pattern. If a user need this workaround for some packages, they may apply it themselves in their config. Once applied, it is hard to find and remove this define.

System Info
System:
    OS: Linux 6.18 EndeavourOS
    CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
    Memory: 84.63 GB / 125.73 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 22.21.1 - /home/yichaozhou/.nix-profile/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.9.4 - /home/yichaozhou/.nix-profile/bin/npm
    pnpm: 10.25.0 - /usr/bin/pnpm
  Browsers:
    Firefox: 146.0.1
    Firefox Developer Edition: 146.0.1
  npmPackages:
    wxt: ^0.20.6 => 0.20.13
Used Package Manager

pnpm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the defineImportMeta.mjs substitution described in the report and reproduce the behavior using the linked Archive.zip with pnpm install. Compare the build output to confirm that worker.js and sqlite3.wasm are omitted. Done means the recommended new Worker(new URL(..., import.meta.url)) pattern packages both files without breaking the intended import.meta.url behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.