vite does not package worker.js with `new Worker(new URL("./worker.js", import.meta.url)`
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
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
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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