Skip to content

Commit 32500a7

Browse files
authored
feat: Add @wxt-dev/webextension-polyfill module (#1310)
1 parent 6c9beee commit 32500a7

File tree

14 files changed

+342
-226
lines changed

14 files changed

+342
-226
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ on:
1515
- module-vue
1616
- storage
1717
- unocss
18+
- webextension-polyfill
1819
- wxt
1920

2021
jobs:

.github/workflows/sync-releases.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on:
1414
- module-svelte
1515
- module-vue
1616
- storage
17+
- webextension-polyfill
1718
- wxt
1819

1920
jobs:

docs/guide/resources/upgrading.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ WXT no longer uses the `webextension-polyfill` internally and `wxt/browser` uses
3131
To upgrade, you have two options:
3232

3333
1. **Stop using the polyfill**
34-
- No changes required, this is the default behavior of v0.20. Your extension will likely continue to work, but do some manual testing to confirm.
34+
- Replace any manual imports from `wxt/browser/chrome` with `wxt/browser`
3535
2. **Continue using the polyfill**
36-
- Install the polyfill, types (if you use typescript), and WXT's [new polyfill module](https://www.npmjs.com/package/@wxt-dev/webextension-polyfill):
36+
- Install the polyfill and WXT's [new polyfill module](https://www.npmjs.com/package/@wxt-dev/webextension-polyfill):
3737
```sh
38-
pnpm i webextension-polyfill
39-
pnpm i -D @types/webextension-polyfill @wxt-dev/webextension-polyfill
38+
pnpm i webextension-polyfill @wxt-dev/webextension-polyfill
4039
```
4140
- Add the WXT module to your config:
4241
```ts
@@ -46,7 +45,7 @@ To upgrade, you have two options:
4645
});
4746
```
4847

49-
Additionally, the `extensionApi` config has been removed. Remove it from your `wxt.config.ts` file if present:
48+
Regardless of your choice, the `extensionApi` config has been removed. Remove it from your `wxt.config.ts` file if present:
5049

5150
```ts
5251
// wxt.config.ts
@@ -55,6 +54,21 @@ export default defineConfig({
5554
});
5655
```
5756

57+
Additionally, extension API types have changed. `wxt/browser` now uses types from `@types/chrome` instead of `@types/webextension-polyfill`. You will have to migrate any type imports to use `@types/chrome`'s namespace approach:
58+
59+
<!-- prettier-ignore -->
60+
```ts
61+
import type { Runtime } from 'wxt/browser'; // [!code --]
62+
import { browser } from 'wxt/browser'; // [!code ++]
63+
64+
function getMessageSenderUrl(sender: Runtime.MessageSender): string { // [!code --]
65+
function getMessageSenderUrl(sender: browser.runtime.MessageSender): string { // [!code ++]
66+
// ...
67+
}
68+
```
69+
70+
`@types/chrome` are more up-to-date, contain less bugs, and don't have any auto-generated names. So even if you continue to use the polyfill, you will need to update your types to use these types.
71+
5872
### `public/` and `modules/` Directories Moved
5973

6074
The default location for the `public/` and `modules/` directories have changed to better align with standards set by other frameworks (Nuxt, Next, Astro, etc). Now, each path is relative to the project's root directory.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `@wxt-dev/webextension-polyfill`
2+
3+
Configures `wxt/browser` to import `browser` from [`webextension-polyfill`](https://github.com/mozilla/webextension-polyfill) instead of using the regular `chrome`/`browser` globals WXT normally provides.
4+
5+
## Usage
6+
7+
```sh
8+
pnpm i @wxt-dev/webextension-polyfill webextension-polyfill
9+
```
10+
11+
Then add the module to your config:
12+
13+
```ts
14+
// wxt.config.ts
15+
export default defineConfig({
16+
modules: ['@wxt-dev/webextension-polyfill'],
17+
});
18+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineBuildConfig } from 'unbuild';
2+
import { resolve } from 'node:path';
3+
4+
export default defineBuildConfig({
5+
rootDir: 'modules',
6+
outDir: resolve(__dirname, 'dist'),
7+
entries: [
8+
{ input: 'webextension-polyfill/index.ts', name: 'index' },
9+
{ input: 'webextension-polyfill/browser.ts', name: 'browser' },
10+
],
11+
replace: {
12+
'process.env.NPM': 'true',
13+
},
14+
declaration: true,
15+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default defineContentScript({
2+
matches: ['*://*/*'],
3+
async main() {
4+
console.log(browser.runtime.id);
5+
},
6+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script type="module" src="./main.ts"></script>
11+
</body>
12+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const root = document.getElementById('app')!;
2+
3+
root.textContent = browser.runtime.id;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as browser } from 'webextension-polyfill';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'wxt';
2+
import { addViteConfig, defineWxtModule } from 'wxt/modules';
3+
import { resolve } from 'node:path';
4+
5+
export default defineWxtModule({
6+
name: '@wxt-dev/webextension-polyfill',
7+
setup(wxt) {
8+
addViteConfig(wxt, () => ({
9+
resolve: {
10+
alias: {
11+
'wxt/browser': process.env.NPM
12+
? '@wxt-dev/webextension-polyfill/browser'
13+
: resolve(__dirname, 'browser.ts'),
14+
},
15+
},
16+
}));
17+
},
18+
});

0 commit comments

Comments
 (0)