Web Workers: "_<name> not defined"
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
In the web-worker-setup example, the worker runs just fine, but at the end, it throws and error:
Uncaught ReferenceError: _content is not defined
at worker.ts:1:32
at worker.ts:1:32
This is because WXT is adding a return value to all IIFE files. Here's the base64 encoded worker file:
KGZ1bmN0aW9uKCkgewogICJ1c2Ugc3RyaWN0IjsKICBjb25zb2xlLmxvZygiSGVsbG8gZnJvbSB3b3JrZXIiKTsKICBfY29udGVudDsKfSkoKTsKX2NvbnRlbnQ7Ci8vIyBzb3VyY2VNYXBwaW5nVVJMPWRhdGE6YXBwbGljYXRpb24vanNvbjtjaGFyc2V0PXV0Zi04O2Jhc2U2NCxleUoyWlhKemFXOXVJam96TENKbWFXeGxJam9pZDI5eWEyVnlMVU5SZDBOQ05uQndMbXB6SWl3aWMyOTFjbU5sY3lJNld5SXVMaTlsYm5SeWVYQnZhVzUwY3k5amIyNTBaVzUwTDNkdmNtdGxjaTUwY3lKZExDSnpiM1Z5WTJWelEyOXVkR1Z1ZENJNld5SmpiMjV6YjJ4bExteHZaeWhjSWtobGJHeHZJR1p5YjIwZ2QyOXlhMlZ5WENJcE8xeHVJbDBzSW01aGJXVnpJanBiWFN3aWJXRndjR2x1WjNNaU9pSTdPMEZCUVVFc1ZVRkJVU3hKUVVGSkxHMUNRVUZ0UWpzN095SjkK
Here it is decoded.
(function() {
"use strict";
console.log("Hello from worker");
_content;
})();
_content;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ya2VyLUNRd0NCNnBwLmpzIiwic291cmNlcyI6WyIuLi9lbnRyeXBvaW50cy9jb250ZW50L3dvcmtlci50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJjb25zb2xlLmxvZyhcIkhlbGxvIGZyb20gd29ya2VyXCIpO1xuIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsVUFBUSxJQUFJLG1CQUFtQjs7OyJ9
In this case, because the worker is loaded from entrypoints/content.ts, the error is "_content not defined".
Reproduction
https://github.com/wxt-dev/examples/tree/main/examples/web-worker-setup
Steps to reproduce
pnpm dev
System Info
System:
OS: macOS 14.2.1
CPU: (8) arm64 Apple M1 Pro
Memory: 2.57 GB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.20.3 - ~/.asdf/installs/nodejs/18.20.3/bin/node
npm: 10.7.0 - ~/.asdf/plugins/nodejs/shims/npm
pnpm: 8.15.8 - ~/.asdf/installs/nodejs/18.20.3/bin/pnpm
bun: 1.1.22 - ~/.bun/bin/bun
Browsers:
Chrome: 128.0.6613.85
Safari: 17.2.1
npmPackages:
wxt: ^0.18.6 => 0.18.15
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 examples/web-worker-setup reproduction and run pnpm dev, focusing on the worker loaded from entrypoints/content.ts. Inspect the generated worker output shown in the report to trace why _content is emitted outside the IIFE. Done means the worker runs without the Uncaught ReferenceError and still logs "Hello from worker".
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100