Skip to content

Clarify code outside of defineBackground as related error #1569

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

Open
avi12 opened this issue Apr 10, 2025 · 15 comments
Open

Clarify code outside of defineBackground as related error #1569

avi12 opened this issue Apr 10, 2025 · 15 comments
Labels
contribution welcome good first issue Want to contribute to WXT? This is a good place to start

Comments

@avi12
Copy link
Contributor

avi12 commented Apr 10, 2025

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

@aklinker1 aklinker1 added contribution welcome good first issue Want to contribute to WXT? This is a good place to start labels Apr 21, 2025
@nishu-murmu
Copy link
Contributor

@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.

const foo = () => {
  console.log(browser.runtime.getURL("/"));
};

export default defineBackground(() => {
  console.log("Hello background!", { id: browser.runtime.id });
  console.log(foo());
});

@avi12
Copy link
Contributor Author

avi12 commented Apr 22, 2025

Try

const foo = "bar";

export default defineBackground(() => {
  console.log(foo);
});

@nishu-murmu
Copy link
Contributor

nishu-murmu commented Apr 23, 2025

Try

const foo = "bar";

export default defineBackground(() => {
console.log(foo);
});

@avi12 I checked for this code as well. It is working for me.

Image

@avi12
Copy link
Contributor Author

avi12 commented Apr 23, 2025

@nishu-murmu According to that logic, this code should be perfectly valid:

console.log("background");

export default defineBackground(() => {});

@avi12
Copy link
Contributor Author

avi12 commented Apr 23, 2025

Suppose I want to do

browser.runtime.onMessage.addListener(() => {});

export default defineBackground(() => {});

the WXT compiler will yell at me

@nishu-murmu
Copy link
Contributor

Suppose I want to do

browser.runtime.onMessage.addListener(() => {});

export default defineBackground(() => {});
the WXT compiler will yell at me

I ran every command, from pnpm compile, pnpm build, pnpm postinstall
I'm not seeing any errors.

And listeners are also working just fine.

Image

@nishu-murmu
Copy link
Contributor

@nishu-murmu According to that logic, this code should be perfectly valid:

console.log("background");

export default defineBackground(() => {});

Yeah, this is perfectly valid.

@avi12
Copy link
Contributor Author

avi12 commented Apr 23, 2025

@nishu-murmu
Copy link
Contributor

Can you try separating the code in different files and importing them in the root file.
Create background folder entrypoint.
Something like this?

Image

@avi12
Copy link
Contributor Author

avi12 commented 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
Copy link
Contributor

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?

Let me clone and check it myself.

@nishu-murmu
Copy link
Contributor

@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

Image

@avi12
Copy link
Contributor Author

avi12 commented Apr 25, 2025

Another question is why do all of the chrome APIs need specific implementations in browser

@nishu-murmu
Copy link
Contributor

Another question is why do all of the chrome APIs need specific implementations in browser

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 to browser

@aklinker1
Copy link
Collaborator

I polyfill the node environment with a chrome and browser 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution welcome good first issue Want to contribute to WXT? This is a good place to start
Projects
None yet
Development

No branches or pull requests

3 participants