wxt: Clarify code outside of `defineBackground` as related error

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

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 18 (14 by maintainers)

Most upvoted comments

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.

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

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.

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

Image

Try

const foo = “bar”;

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

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

Image

Try

const foo = "bar";

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