Skip to content
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

feat: Add @wxt-dev/webextension-polyfill module #1310

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ on:
- module-vue
- storage
- unocss
- webextension-polyfill
- wxt

jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/sync-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
- module-svelte
- module-vue
- storage
- webextension-polyfill
- wxt

jobs:
Expand Down
24 changes: 19 additions & 5 deletions docs/guide/resources/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ WXT no longer uses the `webextension-polyfill` internally and `wxt/browser` uses
To upgrade, you have two options:

1. **Stop using the polyfill**
- 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.
- Replace any manual imports from `wxt/browser/chrome` with `wxt/browser`
2. **Continue using the polyfill**
- Install the polyfill, types (if you use typescript), and WXT's [new polyfill module](https://www.npmjs.com/package/@wxt-dev/webextension-polyfill):
- Install the polyfill and WXT's [new polyfill module](https://www.npmjs.com/package/@wxt-dev/webextension-polyfill):
```sh
pnpm i webextension-polyfill
pnpm i -D @types/webextension-polyfill @wxt-dev/webextension-polyfill
pnpm i webextension-polyfill @wxt-dev/webextension-polyfill
```
- Add the WXT module to your config:
```ts
Expand All @@ -46,7 +45,7 @@ To upgrade, you have two options:
});
```

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

```ts
// wxt.config.ts
Expand All @@ -55,6 +54,21 @@ export default defineConfig({
});
```

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:

<!-- prettier-ignore -->
```ts
import type { Runtime } from 'wxt/browser'; // [!code --]
import { browser } from 'wxt/browser'; // [!code ++]

function getMessageSenderUrl(sender: Runtime.MessageSender): string { // [!code --]
function getMessageSenderUrl(sender: browser.runtime.MessageSender): string { // [!code ++]
// ...
}
```

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

### `public/` and `modules/` Directories Moved

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.
Expand Down
18 changes: 18 additions & 0 deletions packages/webextension-polyfill/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `@wxt-dev/webextension-polyfill`

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.

## Usage

```sh
pnpm i @wxt-dev/webextension-polyfill webextension-polyfill
```

Then add the module to your config:

```ts
// wxt.config.ts
export default defineConfig({
modules: ['@wxt-dev/webextension-polyfill'],
});
```
15 changes: 15 additions & 0 deletions packages/webextension-polyfill/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineBuildConfig } from 'unbuild';
import { resolve } from 'node:path';

export default defineBuildConfig({
rootDir: 'modules',
outDir: resolve(__dirname, 'dist'),
entries: [
{ input: 'webextension-polyfill/index.ts', name: 'index' },
{ input: 'webextension-polyfill/browser.ts', name: 'browser' },
],
replace: {
'process.env.NPM': 'true',
},
declaration: true,
});
6 changes: 6 additions & 0 deletions packages/webextension-polyfill/entrypoints/content/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default defineContentScript({
matches: ['*://*/*'],
async main() {
console.log(browser.runtime.id);
},
});
12 changes: 12 additions & 0 deletions packages/webextension-polyfill/entrypoints/popup/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="./main.ts"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions packages/webextension-polyfill/entrypoints/popup/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const root = document.getElementById('app')!;

root.textContent = browser.runtime.id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as browser } from 'webextension-polyfill';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'wxt';
import { addViteConfig, defineWxtModule } from 'wxt/modules';
import { resolve } from 'node:path';

export default defineWxtModule({
name: '@wxt-dev/webextension-polyfill',
setup(wxt) {
addViteConfig(wxt, () => ({
resolve: {
alias: {
'wxt/browser': process.env.NPM
? '@wxt-dev/webextension-polyfill/browser'
: resolve(__dirname, 'browser.ts'),
},
},
}));
},
});
56 changes: 56 additions & 0 deletions packages/webextension-polyfill/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@wxt-dev/webextension-polyfill",
"description": "Use webextension-polyfill with WXT",
"repository": {
"type": "git",
"url": "git+https://github.com/wxt-dev/wxt.git",
"directory": "packages/webextension-polyfill"
},
"homepage": "https://github.com/wxt-dev/wxt/blob/main/packages/webextension-polyfill/README.md",
"keywords": [
"wxt",
"module",
"webextension-polyfill"
],
"author": {
"name": "Aaron Klinker",
"email": "aaronklinker1+wxt@gmail.com"
},
"license": "MIT",
"version": "1.0.0",
"type": "module",
"main": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"./browser": {
"types": "./dist/browser.d.mts",
"default": "./dist/browser.mjs"
}
},
"files": [
"dist"
],
"scripts": {
"dev": "wxt",
"check": "pnpm build && check",
"build": "buildc -- unbuild",
"prepare": "buildc --deps-only -- wxt prepare"
},
"peerDependencies": {
"webextension-polyfill": "*",
"wxt": ">=0.20.0"
},
"devDependencies": {
"@aklinker1/check": "^1.4.5",
"@types/webextension-polyfill": "^0.12.1",
"publint": "^0.2.12",
"typescript": "^5.6.3",
"unbuild": "^2.0.0",
"webextension-polyfill": "^0.12.0",
"wxt": "workspace:*"
}
}
Empty file.
4 changes: 4 additions & 0 deletions packages/webextension-polyfill/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["../../tsconfig.base.json", "./.wxt/tsconfig.json"],
"exclude": ["node_modules/**", "dist/**"]
}
Loading