- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 358
Labels
contribution welcomegood first issueWant to contribute to WXT? This is a good place to startWant to contribute to WXT? This is a good place to start
Description
Feature Request
Suppose I have
// code
export default defineBackground(() => {
// code
});
it will throw an error that doesn't clarify that I must not have code outside of defineBackground
Is your feature request related to a bug?
Not related to a particular bug, though this issue could be classified as a bug
What are the alternatives?
Knowledge of the situation according to the documentation, nothing else
Additional context
Having this issue clarified will benefit both WXT devs who are new to the framework and devs like me, who explore using an agentic AI to code the extension (e.g. OpenHands)
Such an agent can often use errors to modify the code so that the error disappears
Metadata
Metadata
Assignees
Labels
contribution welcomegood first issueWant to contribute to WXT? This is a good place to startWant to contribute to WXT? This is a good place to start
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
nishu-murmu commentedon Apr 22, 2025
@avi12 Can you show me an example I'm not able to reproduce it.
Is it something like this? Cause this is working just fine.
avi12 commentedon Apr 22, 2025
Try
nishu-murmu commentedon Apr 23, 2025
@avi12 I checked for this code as well. It is working for me.
avi12 commentedon Apr 23, 2025
@nishu-murmu According to that logic, this code should be perfectly valid:
avi12 commentedon Apr 23, 2025
Suppose I want to do
the WXT compiler will yell at me
nishu-murmu commentedon Apr 23, 2025
I ran every command, from
pnpm compile
,pnpm build
,pnpm postinstall
I'm not seeing any errors.
And listeners are also working just fine.
nishu-murmu commentedon Apr 23, 2025
Yeah, this is perfectly valid.
avi12 commentedon Apr 23, 2025
This one failed https://github.com/avi12/auto-detect-new-youtube-videos/blob/background-script-fails/src/entrypoints/background.ts
nishu-murmu commentedon Apr 23, 2025
Can you try separating the code in different files and importing them in the root file.
Create
background
folder entrypoint.Something like this?
avi12 commentedon Apr 23, 2025
To be fair, the project I linked is a puppet project that I let AI code it
But in general, it's good advice to code split
Besides that, I'd assume that the branch I linked failed to compile, am I right?
nishu-murmu commentedon Apr 24, 2025
Let me clone and check it myself.
nishu-murmu commentedon Apr 25, 2025
@avi12 I checked the code.
Problem is with
browser.action
API, I've checked with other APIs like storage API, those are working fine.@aklinker1 can chime in as to why that is happening
avi12 commentedon Apr 25, 2025
Another question is why do all of the
chrome
APIs need specific implementations inbrowser
nishu-murmu commentedon Apr 25, 2025
I don't have much idea regarding this, @aklinker1 has much more idea regarding this.
I'm assuming all the
chrome
APIs are monkey patched tobrowser
aklinker1 commentedon Apr 27, 2025
I polyfill the node environment with a
chrome
andbrowser
global from@webext-core/fake-browser
.Ideally all APIs would have an in-memory implementation, for testing, but I haven't implemented all of them.
So that's why some APIs don't throw an error, like storage, and some do, like the action APIs.
zampettim commentedon Jun 11, 2025
I definitely need the browser.action.XXX methods added to fake-browser (as I see there is a PR for), since my tests failing as a result. Just adding comment to help make clear this is needed.
aklinker1 commentedon Jun 14, 2025
@zampettimWe can add them, but it's also possible for you to add mock implementations yourself. Just modify the
fakeBrowser
object in a setup file:This is necessary because sometimes you might need to specifically mock things with Vitest in the future. Just for reference.
aklinker1 commentedon Jun 14, 2025
@avi12 Does this section clarify things?
https://deploy-preview-1743--creative-fairy-df92c4.netlify.app/guide/essentials/extension-apis.html#entrypoint-limitations
See #1743