Skip to content

Commit 9791871

Browse files
authored
docs: Add section on extension API entrypoint limitations (#1743)
1 parent ac340b3 commit 9791871

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

docs/guide/essentials/config/entrypoint-loaders.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ WXT does several pre-processing steps to try and prevent errors during this proc
1010
2. Use `@webext-core/fake-browser` to create a fake version of the `chrome` and `browser` globals expected by extensions.
1111
3. Pre-process the JS/TS code, stripping out the `main` function then tree-shaking unused code from the file
1212

13-
However, this process is not perfect. It doesn't setup all the globals found in the browser and the APIs may behave differently. As such, **_you should avoid using browser or extension APIs outside the `main` function of your entrypoints!_**
13+
However, this process is not perfect. It doesn't setup all the globals found in the browser and the APIs may behave differently. As such, **_you should avoid using browser or extension APIs outside the `main` function of your entrypoints!_** See [Entrypoint Limitations](/guide/essentials/extension-apis#entrypoint-limitations) for more details.
1414

1515
:::tip
1616
If you're running into errors while importing entrypoints, run `wxt prepare --debug` to see more details about this process. When debugging, WXT will print out the pre-processed code to help you identify issues.

docs/guide/essentials/extension-apis.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,63 @@ Alternatively, if you're trying to use similar APIs under different names (to su
7575
//
7676
});
7777
```
78+
79+
## Entrypoint Limitations
80+
81+
Because WXT imports your entrypoint files into a NodeJS, non-extension environment, the `chrome`/`browser` variables provided to extensions by the browser **will not be available**.
82+
83+
To prevent some basic errors, WXT polyfills these globals with the same in-memory, fake implementation it uses for testing: [`@webext-core/fake-browser`](https://webext-core.aklinker1.io/fake-browser/installation/). However, not all the APIs have been implemented.
84+
85+
So it is extremely important to NEVER use `browser.*` extension APIs outside the main function of any JS/TS entrypoints (background, content scripts, and unlisted scripts). If you do, you'll see an error like this:
86+
87+
```plaintext
88+
✖ Command failed after 440 ms
89+
90+
ERROR Browser.action.onClicked.addListener not implemented.
91+
```
92+
93+
The fix is simple, just move your API usage into the entrypoint's main function:
94+
95+
:::code-group
96+
97+
```ts [background.ts]
98+
browser.action.onClicked.addListener(() => {
99+
/* ... */
100+
}); // [!code --]
101+
102+
export default defineBackground(() => {
103+
browser.action.onClicked.addListener(() => {
104+
/* ... */
105+
}); // [!code ++]
106+
});
107+
```
108+
109+
```ts [content.ts]
110+
browser.runtime.onMessage.addListener(() => {
111+
/* ... */
112+
}); // [!code --]
113+
114+
export default defineContentScript({
115+
main() {
116+
browser.runtime.onMessage.addListener(() => {
117+
/* ... */
118+
}); // [!code ++]
119+
},
120+
});
121+
```
122+
123+
```ts [unlisted.ts]
124+
browser.runtime.onMessage.addListener(() => {
125+
/* ... */
126+
}); // [!code --]
127+
128+
export default defineUnlistedScript(() => {
129+
browser.runtime.onMessage.addListener(() => {
130+
/* ... */
131+
}); // [!code ++]
132+
});
133+
```
134+
135+
:::
136+
137+
Read [Entrypoint Loaders](/guide/essentials/config/entrypoint-loaders) for more technical details about this limitation.

0 commit comments

Comments
 (0)